Class SerializerExtensions
- Namespace
- Akavache
- Assembly
- Akavache.dll
Provides extension methods for serializer operations on blob caches.
- Inheritance
-
Serializer
Extensions
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
databyte[]The data to deserialize.
cacheIBlobCache The cache.
Returns
- T
The deserialized object.
Type Parameters
TThe type to deserialize to.
Exceptions
- Invalid
Operation Exception $"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
blobCacheIBlobCache The blob cache.
keystringThe key to return asynchronously.
typeTypeThe 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.
Parameters
blobCacheIBlobCache 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.
Parameters
blobCacheIBlobCache The blob cache.
typeTypeThe 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.
Parameters
blobCacheIBlobCache The blob cache.
Returns
- IObservable<string>
An observable sequence of keys for the specified type, guaranteed to not be null.
Type Parameters
TThe 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
blobCacheIBlobCache The blob cache.
Returns
- IObservable<T>
A Future result representing all objects in the cache with the specified Type.
Type Parameters
TThe 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
blobCacheIBlobCache The cache to get the item.
keystringThe key to store the returned result under.
fetchFuncFunc<IObservable<T>>A method to fetch a observable.
fetchPredicateFunc<DateTime , bool>Offset 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.
absoluteExpirationDateTime ?Offset An optional expiration date.
shouldInvalidateOnErrorboolIf this is true, the cache will be cleared when an exception occurs in fetchFunc.
cacheValidationPredicateFunc<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
TThe 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
blobCacheIBlobCache The cache to get the item.
keystringThe key to store the returned result under.
fetchFuncFunc<Task<T>>A method that will fetch the task.
fetchPredicateFunc<DateTime , bool>Offset 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.
absoluteExpirationDateTime ?Offset An optional expiration date.
shouldInvalidateOnErrorboolIf this is true, the cache will be cleared when an exception occurs in fetchFunc.
cacheValidationPredicateFunc<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
TThe 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
blobCacheIBlobCache The blob cache.
keystringThe key to return the date for.
Returns
- IObservable<Date
Time ?>Offset The date the key was created on.
Type Parameters
TThe 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
blobCacheIBlobCache The blob cache.
keystringThe key to look up in the cache.
Returns
- IObservable<T>
A Future result representing the object in the cache.
Type Parameters
TThe 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
blobCacheIBlobCache The blob cache to retrieve from.
keysIEnumerable<string>The keys for the objects to retrieve.
Returns
- IObservable<Key
Value <string, T>>Pair An observable that emits key-value pairs for the found objects.
Type Parameters
TThe 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
blobCacheIBlobCache The cache to get the item.
keystringThe key to associate with the object.
fetchFuncFunc<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
TThe 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
blobCacheIBlobCache The cache to get the item.
keystringThe key to associate with the object.
fetchFuncFunc<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.absoluteExpirationDateTime ?Offset An optional expiration date.
Returns
- IObservable<T>
A Future result representing the deserialized object from the cache.
Type Parameters
TThe 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
blobCacheIBlobCache The cache to get the item.
keystringThe key to associate with the object.
fetchFuncFunc<Task<T>>A Func which will asynchronously return the latest value for the object should the cache not contain the key.
absoluteExpirationDateTime ?Offset An optional expiration date.
Returns
- IObservable<T>
A Future result representing the deserialized object from the cache.
Type Parameters
TThe 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
blobCacheIBlobCache The cache to insert the items.
keyValuePairsIEnumerable<KeyValue <string, T>>Pair The data to insert into the cache.
absoluteExpirationDateTime ?Offset An optional expiration date.
Returns
- IObservable<Unit>
A Future result representing the completion of the insert.
Type Parameters
TThe 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
blobCacheIBlobCache The blob cache to insert into.
keystringThe key to associate with the object.
valueTThe object to serialize and cache.
absoluteExpirationDateTime ?Offset An optional expiration date for the cached data.
Returns
- IObservable<Unit>
An observable that signals when the insertion is complete.
Type Parameters
TThe 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
blobCacheIBlobCache The cache to insert the items.
keyValuePairsIDictionary<string, object>The data to insert into the cache.
absoluteExpirationDateTime ?Offset 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
blobCacheIBlobCache The blob cache to insert into.
keyValuePairsIEnumerable<KeyValue <string, T>>Pair The key-value pairs to insert.
absoluteExpirationDateTime ?Offset An optional expiration date for the cached data.
Returns
- IObservable<Unit>
An observable that signals when the operation is complete.
Type Parameters
TThe type of items to insert.
InvalidateAllObjects<T>(IBlobCache)
Invalidates all objects of the specified type. To invalidate all objects regardless of type, use InvalidateAll.
Parameters
blobCacheIBlobCache The blob cache.
Returns
- IObservable<Unit>
A Future result representing the completion of the invalidation.
Type Parameters
TThe 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.
Parameters
blobCacheIBlobCache The blob cache.
keystringThe key to invalidate.
Returns
- IObservable<Unit>
A Future result representing the completion of the invalidation.
Type Parameters
TThe 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
blobCacheIBlobCache The blob cache.
keysIEnumerable<string>The keys to invalidate.
Returns
- IObservable<Unit>
A Future result representing the completion of the invalidation.
Type Parameters
TThe 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
valueTThe value to serialize.
cacheIBlobCache The cache.
Returns
- byte[]
The serialized data as byte array.
Type Parameters
TThe type of object to serialize.
Exceptions
- Invalid
Operation Exception Thrown when serialization fails.