Table of Contents

Class JsonSerializationMixin

Namespace
Akavache
Assembly
Akavache.Core.dll

Set of extension methods associated with JSON serialization.

public static class JsonSerializationMixin
Inheritance
JsonSerializationMixin

Methods

GetAllObjects<T>(IBlobCache)

Return all objects of a specific Type in the cache.

public static IObservable<IEnumerable<T>> GetAllObjects<T>(this IBlobCache blobCache)

Parameters

blobCache IBlobCache

The cache to get the items.

Returns

IObservable<IEnumerable<T>>

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

Type Parameters

T

The type of item to get.

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.

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.

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 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 cache to get the item.

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 item to get.

GetObject<T>(IBlobCache, string)

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

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

Parameters

blobCache IBlobCache

The cache to get the item.

key string

The key to look up in the cache modified key name. If this is true, GetAllObjects will not find this object.

Returns

IObservable<T>

A Future result representing the object in the cache.

Type Parameters

T

The type of item.

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

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.

public static IObservable<T?> GetOrCreateObject<T>(this IBlobCache blobCache, string key, Func<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<T>

A Func which will 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.

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.

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.

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, IDictionary<string, T>, DateTimeOffset?)

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

public static IObservable<Unit> InsertAllObjects<T>(this IBlobCache blobCache, IDictionary<string, T> keyValuePairs, DateTimeOffset? absoluteExpiration = null)

Parameters

blobCache IBlobCache

The cache to insert the items.

keyValuePairs IDictionary<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?)

Insert an object into the cache, via the JSON serializer.

public static IObservable<Unit> InsertObject<T>(this IBlobCache blobCache, string key, T value, DateTimeOffset? absoluteExpiration = null)

Parameters

blobCache IBlobCache

The cache to insert the item.

key string

The key to associate with the object.

value T

The object to serialize.

absoluteExpiration DateTimeOffset?

An optional expiration date.

Returns

IObservable<Unit>

An observable which signals when the insertion has completed.

Type Parameters

T

The type of item.

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 cache to invalidate.

Returns

IObservable<Unit>

An observable that signals when the operation has finished.

Type Parameters

T

The type of item to invalidate.

Remarks

Returns a Unit for each invalidation completion. Use Wait instead of First to wait for this.

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 cache to invalidate.

key string

The key to invalidate.

Returns

IObservable<Unit>

An observable that signals when the operation has completed.

Type Parameters

T

The type of item to invalidate.