Table of Contents

Class SerializerExtensions

Namespace
Akavache
Assembly
Akavache.dll

Provides extension methods for serializer operations on blob caches.

public static class SerializerExtensions
Inheritance
SerializerExtensions

Methods

DeserializeWithContext<T>(byte[], IBlobCache)

Attempts to deserialize data with context and enhanced compatibility.

[RequiresUnreferencedCode("Deserialization requires types to be preserved.")]
[RequiresDynamicCode("Deserialization requires types to be preserved.")]
public static T? DeserializeWithContext<T>(byte[] data, IBlobCache cache)

Parameters

data byte[]

The data to deserialize.

cache IBlobCache

The cache.

Returns

T

The deserialized object.

Type Parameters

T

The type to deserialize to.

Exceptions

InvalidOperationException

$"Failed to deserialize data to type {typeof(T).Name}. " + $"Data length: {data.Length} bytes. " + "Please ensure the data was serialized with a compatible serializer. " + $"Error: {ex.Message}, ex.

Get(IBlobCache, string, Type)

Retrieve a value from the key-value cache and deserialize it. If the key is not in the cache, this method should return an IObservable which OnError's with KeyNotFoundException.

[RequiresUnreferencedCode("Using Get with Type requires types to be preserved for Deserialization.")]
[RequiresDynamicCode("Using Get with Type requires types to be preserved for Deserialization.")]
public static IObservable<object?> Get(this IBlobCache blobCache, string key, Type type)

Parameters

blobCache IBlobCache

The blob cache.

key string

The key to return asynchronously.

type Type

The type of object to deserialize to.

Returns

IObservable<object>

A Future result representing the deserialized object.

GetAllKeysSafe(IBlobCache)

Safely gets all keys from the cache with null-safety guards. This method provides a safe alternative to GetAllKeys() that prevents crashes on mobile platforms.

public static IObservable<string> GetAllKeysSafe(this IBlobCache blobCache)

Parameters

blobCache IBlobCache

The blob cache.

Returns

IObservable<string>

An observable sequence of keys, guaranteed to not be null.

GetAllKeysSafe(IBlobCache, Type)

Safely gets all keys for a specific type from the cache with null-safety guards. This method provides a safe alternative to GetAllKeys(Type) that prevents crashes on mobile platforms.

public static IObservable<string> GetAllKeysSafe(this IBlobCache blobCache, Type type)

Parameters

blobCache IBlobCache

The blob cache.

type Type

The type to filter keys by.

Returns

IObservable<string>

An observable sequence of keys for the specified type, guaranteed to not be null.

GetAllKeysSafe<T>(IBlobCache)

Safely gets all keys for a specific type from the cache with null-safety guards. This method provides a safe alternative to GetAllKeys() that prevents crashes on mobile platforms.

public static IObservable<string> GetAllKeysSafe<T>(this IBlobCache blobCache)

Parameters

blobCache IBlobCache

The blob cache.

Returns

IObservable<string>

An observable sequence of keys for the specified type, guaranteed to not be null.

Type Parameters

T

The type to filter keys by.

GetAllObjects<T>(IBlobCache)

Return all objects of a specific Type in the cache.

[RequiresUnreferencedCode("Using GetAllObjects requires types to be preserved for Deserialization.")]
[RequiresDynamicCode("Using GetAllObjects requires types to be preserved for Deserialization.")]
public static IObservable<T> GetAllObjects<T>(this IBlobCache blobCache)

Parameters

blobCache IBlobCache

The blob cache.

Returns

IObservable<T>

A Future result representing all objects in the cache with the specified Type.

Type Parameters

T

The type of object associated with the blob.

GetAndFetchLatest<T>(IBlobCache, string, Func<IObservable<T>>, Func<DateTimeOffset, bool>?, DateTimeOffset?, bool, Func<T, bool>?)

This method attempts to returned a cached value, while simultaneously calling a Func to return the latest value. When the latest data comes back, it replaces what was previously in the cache.

This method is best suited for loading dynamic data from the Internet, while still showing the user earlier data.

This method returns an IObservable that may return *two* results (first the cached data, then the latest data). Therefore, it's important for UI applications that in your Subscribe method, you write the code to merge the second result when it comes in.

This also means that await'ing this method is a Bad Idea(tm), always use Subscribe.

[RequiresUnreferencedCode("Using GetAndFetchLatest requires types to be preserved for serialization.")]
[RequiresDynamicCode("Using GetAndFetchLatest requires types to be preserved for serialization.")]
public static IObservable<T?> GetAndFetchLatest<T>(this IBlobCache blobCache, string key, Func<IObservable<T>> fetchFunc, Func<DateTimeOffset, bool>? fetchPredicate = null, DateTimeOffset? absoluteExpiration = null, bool shouldInvalidateOnError = false, Func<T, bool>? cacheValidationPredicate = null)

Parameters

blobCache IBlobCache

The cache to get the item.

key string

The key to store the returned result under.

fetchFunc Func<IObservable<T>>

A method to fetch a observable.

fetchPredicate Func<DateTimeOffset, bool>

An optional Func to determine whether the updated item should be fetched. If the cached version isn't found, this parameter is ignored and the item is always fetched.

absoluteExpiration DateTimeOffset?

An optional expiration date.

shouldInvalidateOnError bool

If this is true, the cache will be cleared when an exception occurs in fetchFunc.

cacheValidationPredicate Func<T, bool>

An optional Func to determine if the fetched value should be cached.

Returns

IObservable<T>

An Observable stream containing either one or two results (possibly a cached version, then the latest version).

Type Parameters

T

The type of item to get.

GetAndFetchLatest<T>(IBlobCache, string, Func<Task<T>>, Func<DateTimeOffset, bool>?, DateTimeOffset?, bool, Func<T, bool>?)

This method attempts to returned a cached value, while simultaneously calling a Func to return the latest value. When the latest data comes back, it replaces what was previously in the cache.

This method is best suited for loading dynamic data from the Internet, while still showing the user earlier data.

This method returns an IObservable that may return *two* results (first the cached data, then the latest data). Therefore, it's important for UI applications that in your Subscribe method, you write the code to merge the second result when it comes in.

This also means that awaiting this method is a Bad Idea(tm), always use Subscribe.

[RequiresUnreferencedCode("Using GetAndFetchLatest requires types to be preserved for serialization.")]
[RequiresDynamicCode("Using GetAndFetchLatest requires types to be preserved for serialization.")]
public static IObservable<T?> GetAndFetchLatest<T>(this IBlobCache blobCache, string key, Func<Task<T>> fetchFunc, Func<DateTimeOffset, bool>? fetchPredicate = null, DateTimeOffset? absoluteExpiration = null, bool shouldInvalidateOnError = false, Func<T, bool>? cacheValidationPredicate = null)

Parameters

blobCache IBlobCache

The cache to get the item.

key string

The key to store the returned result under.

fetchFunc Func<Task<T>>

A method that will fetch the task.

fetchPredicate Func<DateTimeOffset, bool>

An optional Func to determine whether the updated item should be fetched. If the cached version isn't found, this parameter is ignored and the item is always fetched.

absoluteExpiration DateTimeOffset?

An optional expiration date.

shouldInvalidateOnError bool

If this is true, the cache will be cleared when an exception occurs in fetchFunc.

cacheValidationPredicate Func<T, bool>

An optional Func to determine if the fetched value should be cached.

Returns

IObservable<T>

An Observable stream containing either one or two results (possibly a cached version, then the latest version).

Type Parameters

T

The type of item to get.

GetObjectCreatedAt<T>(IBlobCache, string)

Returns the time that the object with the key was added to the cache, or returns null if the key isn't in the cache.

public static IObservable<DateTimeOffset?> GetObjectCreatedAt<T>(this IBlobCache blobCache, string key)

Parameters

blobCache IBlobCache

The blob cache.

key string

The key to return the date for.

Returns

IObservable<DateTimeOffset?>

The date the key was created on.

Type Parameters

T

The type of object associated with the blob.

GetObject<T>(IBlobCache, string)

Get an object from the cache and deserialize it via the JSON serializer.

[RequiresUnreferencedCode("Using GetObject requires types to be preserved for Deserialization.")]
[RequiresDynamicCode("Using GetObject requires types to be preserved for Deserialization.")]
public static IObservable<T?> GetObject<T>(this IBlobCache blobCache, string key)

Parameters

blobCache IBlobCache

The blob cache.

key string

The key to look up in the cache.

Returns

IObservable<T>

A Future result representing the object in the cache.

Type Parameters

T

The type of object associated with the blob.

GetObjects<T>(IBlobCache, IEnumerable<string>)

Retrieves objects from the cache for the specified keys.

[RequiresUnreferencedCode("Using GetObjects requires types to be preserved for Deserialization.")]
[RequiresDynamicCode("Using GetObjects requires types to be preserved for Deserialization.")]
public static IObservable<KeyValuePair<string, T>> GetObjects<T>(this IBlobCache blobCache, IEnumerable<string> keys)

Parameters

blobCache IBlobCache

The blob cache to retrieve from.

keys IEnumerable<string>

The keys for the objects to retrieve.

Returns

IObservable<KeyValuePair<string, T>>

An observable that emits key-value pairs for the found objects.

Type Parameters

T

The type of items to retrieve.

GetOrCreateObject<T>(IBlobCache, string, Func<T>)

Attempt to return an object from the cache. If the item doesn't exist or returns an error, call a Func to create a new one.

For most Internet applications, this method is the best method to call to fetch static data (i.e. images) from the network.

[RequiresUnreferencedCode("Using GetOrCreateObject requires types to be preserved for serialization.")]
[RequiresDynamicCode("Using GetOrCreateObject requires types to be preserved for serialization.")]
public static IObservable<T?> GetOrCreateObject<T>(this IBlobCache blobCache, string key, Func<T> fetchFunc)

Parameters

blobCache IBlobCache

The cache to get the item.

key string

The key to associate with the object.

fetchFunc Func<T>

A Func which will return the latest value for the object should the cache not contain the key.

Returns

IObservable<T>

A Future result representing the deserialized object from the cache.

Type Parameters

T

The type of item to get.

GetOrFetchObject<T>(IBlobCache, string, Func<IObservable<T>>, DateTimeOffset?)

Attempt to return an object from the cache. If the item doesn't exist or returns an error, call a Func to return the latest version of an object and insert the result in the cache.

For most Internet applications, this method is the best method to call to fetch static data (i.e. images) from the network.

[RequiresUnreferencedCode("Using GetOrFetchObject requires types to be preserved for serialization.")]
[RequiresDynamicCode("Using GetOrFetchObject requires types to be preserved for serialization.")]
public static IObservable<T?> GetOrFetchObject<T>(this IBlobCache blobCache, string key, Func<IObservable<T>> fetchFunc, DateTimeOffset? absoluteExpiration = null)

Parameters

blobCache IBlobCache

The cache to get the item.

key string

The key to associate with the object.

fetchFunc Func<IObservable<T>>

A Func which will asynchronously return the latest value for the object should the cache not contain the key.

         Observable.Start is the most straightforward way (though not the
         most efficient!) to implement this Func.
absoluteExpiration DateTimeOffset?

An optional expiration date.

Returns

IObservable<T>

A Future result representing the deserialized object from the cache.

Type Parameters

T

The type of item to get.

GetOrFetchObject<T>(IBlobCache, string, Func<Task<T>>, DateTimeOffset?)

Attempt to return an object from the cache. If the item doesn't exist or returns an error, call a Func to return the latest version of an object and insert the result in the cache.

For most Internet applications, this method is the best method to call to fetch static data (i.e. images) from the network.

[RequiresUnreferencedCode("Using GetOrFetchObject requires types to be preserved for serialization.")]
[RequiresDynamicCode("Using GetOrFetchObject requires types to be preserved for serialization.")]
public static IObservable<T?> GetOrFetchObject<T>(this IBlobCache blobCache, string key, Func<Task<T>> fetchFunc, DateTimeOffset? absoluteExpiration = null)

Parameters

blobCache IBlobCache

The cache to get the item.

key string

The key to associate with the object.

fetchFunc Func<Task<T>>

A Func which will asynchronously return the latest value for the object should the cache not contain the key.

absoluteExpiration DateTimeOffset?

An optional expiration date.

Returns

IObservable<T>

A Future result representing the deserialized object from the cache.

Type Parameters

T

The type of item to get.

InsertAllObjects<T>(IBlobCache, IEnumerable<KeyValuePair<string, T>>, DateTimeOffset?)

Insert several objects into the cache, via the JSON serializer. Similarly to InsertAll, partial inserts should not happen.

[RequiresUnreferencedCode("Using InsertAllObjects requires types to be preserved for serialization.")]
[RequiresDynamicCode("Using InsertAllObjects requires types to be preserved for serialization.")]
public static IObservable<Unit> InsertAllObjects<T>(this IBlobCache blobCache, IEnumerable<KeyValuePair<string, T>> keyValuePairs, DateTimeOffset? absoluteExpiration = null)

Parameters

blobCache IBlobCache

The cache to insert the items.

keyValuePairs IEnumerable<KeyValuePair<string, T>>

The data to insert into the cache.

absoluteExpiration DateTimeOffset?

An optional expiration date.

Returns

IObservable<Unit>

A Future result representing the completion of the insert.

Type Parameters

T

The type of item.

InsertObject<T>(IBlobCache, string, T, DateTimeOffset?)

Inserts an object into the cache using the configured serializer.

[RequiresUnreferencedCode("Using InsertObject requires types to be preserved for serialization.")]
[RequiresDynamicCode("Using InsertObject requires types to be preserved for serialization.")]
public static IObservable<Unit> InsertObject<T>(this IBlobCache blobCache, string key, T value, DateTimeOffset? absoluteExpiration = null)

Parameters

blobCache IBlobCache

The blob cache to insert into.

key string

The key to associate with the object.

value T

The object to serialize and cache.

absoluteExpiration DateTimeOffset?

An optional expiration date for the cached data.

Returns

IObservable<Unit>

An observable that signals when the insertion is complete.

Type Parameters

T

The type of object to insert.

InsertObjects(IBlobCache, IDictionary<string, object>, DateTimeOffset?)

Insert several objects of mixed types into the cache.

[RequiresUnreferencedCode("Using InsertObjects requires types to be preserved for serialization.")]
[RequiresDynamicCode("Using InsertObjects requires types to be preserved for serialization.")]
public static IObservable<Unit> InsertObjects(this IBlobCache blobCache, IDictionary<string, object> keyValuePairs, DateTimeOffset? absoluteExpiration = null)

Parameters

blobCache IBlobCache

The cache to insert the items.

keyValuePairs IDictionary<string, object>

The data to insert into the cache.

absoluteExpiration DateTimeOffset?

An optional expiration date.

Returns

IObservable<Unit>

A Future result representing the completion of the insert.

InsertObjects<T>(IBlobCache, IEnumerable<KeyValuePair<string, T>>, DateTimeOffset?)

Inserts multiple objects into the cache with their associated keys.

[RequiresUnreferencedCode("Using InsertObjects requires types to be preserved for serialization.")]
[RequiresDynamicCode("Using InsertObjects requires types to be preserved for serialization.")]
public static IObservable<Unit> InsertObjects<T>(this IBlobCache blobCache, IEnumerable<KeyValuePair<string, T>> keyValuePairs, DateTimeOffset? absoluteExpiration = null)

Parameters

blobCache IBlobCache

The blob cache to insert into.

keyValuePairs IEnumerable<KeyValuePair<string, T>>

The key-value pairs to insert.

absoluteExpiration DateTimeOffset?

An optional expiration date for the cached data.

Returns

IObservable<Unit>

An observable that signals when the operation is complete.

Type Parameters

T

The type of items to insert.

InvalidateAllObjects<T>(IBlobCache)

Invalidates all objects of the specified type. To invalidate all objects regardless of type, use InvalidateAll.

public static IObservable<Unit> InvalidateAllObjects<T>(this IBlobCache blobCache)

Parameters

blobCache IBlobCache

The blob cache.

Returns

IObservable<Unit>

A Future result representing the completion of the invalidation.

Type Parameters

T

The type of object associated with the blob.

InvalidateObject<T>(IBlobCache, string)

Invalidates a single object from the cache. It is important that the Type Parameter for this method be correct, and you cannot use IBlobCache.Invalidate to perform the same task.

public static IObservable<Unit> InvalidateObject<T>(this IBlobCache blobCache, string key)

Parameters

blobCache IBlobCache

The blob cache.

key string

The key to invalidate.

Returns

IObservable<Unit>

A Future result representing the completion of the invalidation.

Type Parameters

T

The type of object associated with the blob.

InvalidateObjects<T>(IBlobCache, IEnumerable<string>)

Invalidates several objects from the cache. It is important that the Type Parameter for this method be correct, and you cannot use IBlobCache.Invalidate to perform the same task.

public static IObservable<Unit> InvalidateObjects<T>(this IBlobCache blobCache, IEnumerable<string> keys)

Parameters

blobCache IBlobCache

The blob cache.

keys IEnumerable<string>

The keys to invalidate.

Returns

IObservable<Unit>

A Future result representing the completion of the invalidation.

Type Parameters

T

The type of object associated with the blob.

SerializeWithContext<T>(T, IBlobCache)

Attempts to serialize an object with context and enhanced compatibility.

[RequiresUnreferencedCode("Serialization requires types to be preserved.")]
[RequiresDynamicCode("Serialization requires types to be preserved.")]
public static byte[] SerializeWithContext<T>(T value, IBlobCache cache)

Parameters

value T

The value to serialize.

cache IBlobCache

The cache.

Returns

byte[]

The serialized data as byte array.

Type Parameters

T

The type of object to serialize.

Exceptions

InvalidOperationException

Thrown when serialization fails.