Class InMemoryBlobCache
- Namespace
- Akavache
- Assembly
- Akavache.Core.dll
This class is an IBlobCache backed by a simple in-memory Dictionary. Use it for testing / mocking purposes.
public class InMemoryBlobCache : ISecureBlobCache, IObjectBlobCache, IBlobCache, IDisposable, IEnableLogger
- Inheritance
-
InMemoryBlobCache
- Implements
- Extension Methods
Constructors
InMemoryBlobCache()
Initializes a new instance of the InMemoryBlobCache class.
public InMemoryBlobCache()
InMemoryBlobCache(IEnumerable<KeyValuePair<string, byte[]>>)
Initializes a new instance of the InMemoryBlobCache class.
public InMemoryBlobCache(IEnumerable<KeyValuePair<string, byte[]>> initialContents)
Parameters
initialContents
IEnumerable<KeyValuePair<string, byte[]>>The initial contents of the cache.
InMemoryBlobCache(IScheduler)
Initializes a new instance of the InMemoryBlobCache class.
public InMemoryBlobCache(IScheduler scheduler)
Parameters
scheduler
ISchedulerThe scheduler to use for Observable based operations.
InMemoryBlobCache(IScheduler?, IEnumerable<KeyValuePair<string, byte[]>>?)
Initializes a new instance of the InMemoryBlobCache class.
public InMemoryBlobCache(IScheduler? scheduler, IEnumerable<KeyValuePair<string, byte[]>>? initialContents)
Parameters
scheduler
ISchedulerThe scheduler to use for Observable based operations.
initialContents
IEnumerable<KeyValuePair<string, byte[]>>The initial contents of the cache.
Properties
ForcedDateTimeKind
Gets or sets the DateTimeKind handling for BSON readers to be forced.
public DateTimeKind? ForcedDateTimeKind { get; set; }
Property Value
Remarks
By default, Newtonsoft.Json.Bson.BsonReader uses a DateTimeKind of Local and Newtonsoft.Json.Bson.BsonWriter
uses Utc. Thus, DateTimes are serialized as UTC but deserialized as local time. To force BSON readers to
use some other DateTimeKind
, you can set this value.
Scheduler
Gets the IScheduler used to defer operations. By default, this is BlobCache.TaskpoolScheduler.
public IScheduler Scheduler { get; protected set; }
Property Value
- IScheduler
Shutdown
Gets an Observable that fires after the Dispose completes successfully, since there is no such thing as an AsyncDispose().
public IObservable<Unit> Shutdown { get; }
Property Value
- IObservable<Unit>
Methods
Dispose()
public void Dispose()
Dispose(bool)
Disposes of the managed memory inside the class.
protected virtual void Dispose(bool isDisposing)
Parameters
isDisposing
boolIf this is being called by the Dispose method.
Flush()
This method guarantees that all in-flight inserts have completed and any indexes have been written to disk.
public IObservable<Unit> Flush()
Returns
- IObservable<Unit>
A signal indicating when the flush is complete.
Get(string)
Retrieve a value from the key-value cache. If the key is not in the cache, this method should return an IObservable which OnError's with KeyNotFoundException.
public IObservable<byte[]> Get(string key)
Parameters
key
stringThe key to return asynchronously.
Returns
- IObservable<byte[]>
A Future result representing the byte data.
GetAllKeys()
Return all keys in the cache. Note that this method is normally for diagnostic / testing purposes, and that it is not guaranteed to be accurate with respect to in-flight requests.
public IObservable<IEnumerable<string>> GetAllKeys()
Returns
- IObservable<IEnumerable<string>>
A list of valid keys for the cache.
GetAllObjects<T>()
Return all objects of a specific Type in the cache.
public IObservable<IEnumerable<T>> GetAllObjects<T>()
Returns
- IObservable<IEnumerable<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.
GetCreatedAt(string)
Returns the time that the key was added to the cache, or returns null if the key isn't in the cache.
public IObservable<DateTimeOffset?> GetCreatedAt(string key)
Parameters
key
stringThe key to return the date for.
Returns
- IObservable<DateTimeOffset?>
The date the key was created on.
GetObjectCreatedAt<T>(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 IObservable<DateTimeOffset?> GetObjectCreatedAt<T>(string key)
Parameters
key
stringThe 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>(string)
Get an object from the cache and deserialize it via the JSON serializer.
public IObservable<T> GetObject<T>(string key)
Parameters
key
stringThe 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.
Insert(string, byte[], DateTimeOffset?)
Insert a blob into the cache with the specified key and expiration date.
public IObservable<Unit> Insert(string key, byte[] data, DateTimeOffset? absoluteExpiration = null)
Parameters
key
stringThe key to use for the data.
data
byte[]The data to save in the cache.
absoluteExpiration
DateTimeOffset?An optional expiration date. After the specified date, the key-value pair should be removed.
Returns
- IObservable<Unit>
A signal to indicate when the key has been inserted.
InsertObject<T>(string, T, DateTimeOffset?)
Insert an object into the cache, via the JSON serializer.
public IObservable<Unit> InsertObject<T>(string key, T value, DateTimeOffset? absoluteExpiration = null)
Parameters
key
stringThe key to associate with the object.
value
TThe object to serialize.
absoluteExpiration
DateTimeOffset?An optional expiration date.
Returns
- IObservable<Unit>
A Future result representing the completion of the insert.
Type Parameters
T
The type of object associated with the blob.
Invalidate(string)
Remove a key from the cache. If the key doesn't exist, this method should do nothing and return (not throw KeyNotFoundException).
public IObservable<Unit> Invalidate(string key)
Parameters
key
stringThe key to remove from the cache.
Returns
- IObservable<Unit>
A signal indicating when the invalidate is complete.
InvalidateAll()
Invalidate all entries in the cache (i.e. clear it). Note that this method is blocking and incurs a significant performance penalty if used while the cache is being used on other threads.
public IObservable<Unit> InvalidateAll()
Returns
- IObservable<Unit>
A signal indicating when the invalidate is complete.
InvalidateAllObjects<T>()
Invalidates all objects of the specified type. To invalidate all objects regardless of type, use InvalidateAll.
public IObservable<Unit> InvalidateAllObjects<T>()
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>(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 IObservable<Unit> InvalidateObject<T>(string key)
Parameters
key
stringThe 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.
OverrideGlobals(IDictionary<string, byte[]>, IScheduler?)
Overrides the global registrations with specified values.
public static InMemoryBlobCache OverrideGlobals(IDictionary<string, byte[]> initialContents, IScheduler? scheduler = null)
Parameters
initialContents
IDictionary<string, byte[]>The default inner contents to use.
scheduler
ISchedulerThe default scheduler to use.
Returns
- InMemoryBlobCache
A generated cache.
OverrideGlobals(IDictionary<string, object>, IScheduler?)
Overrides the global registrations with specified values.
public static InMemoryBlobCache OverrideGlobals(IDictionary<string, object> initialContents, IScheduler? scheduler = null)
Parameters
initialContents
IDictionary<string, object>The default inner contents to use.
scheduler
ISchedulerThe default scheduler to use.
Returns
- InMemoryBlobCache
A generated cache.
OverrideGlobals(IScheduler?, params KeyValuePair<string, byte[]>[])
Overrides the global registrations with specified values.
public static InMemoryBlobCache OverrideGlobals(IScheduler? scheduler = null, params KeyValuePair<string, byte[]>[] initialContents)
Parameters
scheduler
ISchedulerThe default scheduler to use.
initialContents
KeyValuePair<string, byte[]>[]The default inner contents to use.
Returns
- InMemoryBlobCache
A generated cache.
Vacuum()
This method eagerly removes all expired keys from the blob cache, as well as does any cleanup operations that makes sense (Hint: on SQLite3 it does a Vacuum).
public IObservable<Unit> Vacuum()
Returns
- IObservable<Unit>
A signal indicating when the operation is complete.