Table of Contents

Class SqlRawPersistentBlobCache

Namespace
Akavache.Sqlite3
Assembly
Akavache.Sqlite3.dll

This class represents an IBlobCache backed by a SQLite3 database, and it is the default (and best!) implementation.

public class SqlRawPersistentBlobCache : IEnableLogger, IObjectBulkBlobCache, IObjectBlobCache, IBulkBlobCache, IBlobCache, IDisposable
Inheritance
SqlRawPersistentBlobCache
Implements
Derived
Extension Methods
WhenAnyMixin.WhenAny<TSender, TRet, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(TSender?, Expression<Func<TSender, T1>>, Expression<Func<TSender, T2>>, Expression<Func<TSender, T3>>, Expression<Func<TSender, T4>>, Expression<Func<TSender, T5>>, Expression<Func<TSender, T6>>, Expression<Func<TSender, T7>>, Expression<Func<TSender, T8>>, Expression<Func<TSender, T9>>, Expression<Func<TSender, T10>>, Expression<Func<TSender, T11>>, Expression<Func<TSender, T12>>, Func<IObservedChange<TSender, T1>, IObservedChange<TSender, T2>, IObservedChange<TSender, T3>, IObservedChange<TSender, T4>, IObservedChange<TSender, T5>, IObservedChange<TSender, T6>, IObservedChange<TSender, T7>, IObservedChange<TSender, T8>, IObservedChange<TSender, T9>, IObservedChange<TSender, T10>, IObservedChange<TSender, T11>, IObservedChange<TSender, T12>, TRet>)
WhenAnyMixin.WhenAny<TSender, TRet, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(TSender?, Expression<Func<TSender, T1>>, Expression<Func<TSender, T2>>, Expression<Func<TSender, T3>>, Expression<Func<TSender, T4>>, Expression<Func<TSender, T5>>, Expression<Func<TSender, T6>>, Expression<Func<TSender, T7>>, Expression<Func<TSender, T8>>, Expression<Func<TSender, T9>>, Expression<Func<TSender, T10>>, Expression<Func<TSender, T11>>, Expression<Func<TSender, T12>>, Func<IObservedChange<TSender, T1>, IObservedChange<TSender, T2>, IObservedChange<TSender, T3>, IObservedChange<TSender, T4>, IObservedChange<TSender, T5>, IObservedChange<TSender, T6>, IObservedChange<TSender, T7>, IObservedChange<TSender, T8>, IObservedChange<TSender, T9>, IObservedChange<TSender, T10>, IObservedChange<TSender, T11>, IObservedChange<TSender, T12>, TRet>, bool)

Constructors

SqlRawPersistentBlobCache(string, IScheduler?)

Initializes a new instance of the SqlRawPersistentBlobCache class.

public SqlRawPersistentBlobCache(string databaseFile, IScheduler? scheduler = null)

Parameters

databaseFile string

The location of the database file.

scheduler IScheduler

The scheduler to perform operations on.

Properties

Connection

Gets the connection to the sqlite file.

public SQLiteConnection Connection { get; }

Property Value

SQLiteConnection

ForcedDateTimeKind

Gets or sets the DateTimeKind handling for BSON readers to be forced.

public DateTimeKind? ForcedDateTimeKind { get; set; }

Property Value

DateTimeKind?

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; }

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

AfterReadFromDiskFilter(byte[], IScheduler)

This method is called immediately after reading any data to disk. Override this in encrypting data stores in order to decrypt the data.

protected virtual IObservable<byte[]> AfterReadFromDiskFilter(byte[] data, IScheduler scheduler)

Parameters

data byte[]

The byte data that has just been read from disk.

scheduler IScheduler

The scheduler to use if an operation has to be deferred. If the operation can be done immediately, use Observable.Return and ignore this parameter.

Returns

IObservable<byte[]>

A Future result representing the decrypted data.

BeforeWriteToDiskFilter(byte[], IScheduler)

This method is called immediately before writing any data to disk. Override this in encrypting data stores in order to encrypt the data.

protected virtual IObservable<byte[]> BeforeWriteToDiskFilter(byte[] data, IScheduler scheduler)

Parameters

data byte[]

The byte data about to be written to disk.

scheduler IScheduler

The scheduler to use if an operation has to be deferred. If the operation can be done immediately, use Observable.Return and ignore this parameter.

Returns

IObservable<byte[]>

A Future result representing the encrypted data.

Dispose()

public void Dispose()

Dispose(bool)

Disposes of any managed objects that are IDisposable.

protected virtual void Dispose(bool isDisposing)

Parameters

isDisposing bool

If the method 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(IEnumerable<string>)

Retrieve several values from the key-value cache. If any of the keys are not in the cache, this method should return an IObservable which OnError's with KeyNotFoundException.

public IObservable<IDictionary<string, byte[]>> Get(IEnumerable<string> keys)

Parameters

keys IEnumerable<string>

The keys to return asynchronously.

Returns

IObservable<IDictionary<string, byte[]>>

A Future result representing the byte data for each key.

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 string

The 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(IEnumerable<string>)

Returns the time that the keys were added to the cache, or returns null if the key isn't in the cache.

public IObservable<IDictionary<string, DateTimeOffset?>> GetCreatedAt(IEnumerable<string> keys)

Parameters

keys IEnumerable<string>

The keys to return the date for.

Returns

IObservable<IDictionary<string, DateTimeOffset?>>

The date the key was created on.

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 string

The 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 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>(string)

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

public IObservable<T?> GetObject<T>(string key)

Parameters

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>(IEnumerable<string>)

Get several objects from the cache and deserialize it via the JSON serializer.

public IObservable<IDictionary<string, T>> GetObjects<T>(IEnumerable<string> keys)

Parameters

keys IEnumerable<string>

The key to look up in the cache.

Returns

IObservable<IDictionary<string, T>>

A Future result representing the object in the cache.

Type Parameters

T

The type of object associated with the blob.

GetSchemaVersion()

Gets the current version of schema being used.

protected int GetSchemaVersion()

Returns

int

The version of the schema.

Initialize()

Initializes the cache.

protected IObservable<Unit> Initialize()

Returns

IObservable<Unit>

An observable that signals when the initialization is complete.

Insert(IDictionary<string, byte[]>, DateTimeOffset?)

Inserts several keys into the database at one time. If any individual insert fails, this operation should cancel the entire insert (i.e. it should not partially succeed).

public IObservable<Unit> Insert(IDictionary<string, byte[]> keyValuePairs, DateTimeOffset? absoluteExpiration = null)

Parameters

keyValuePairs IDictionary<string, byte[]>

The keys and values to insert.

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.

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 string

The 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 string

The key to associate with the object.

value T

The 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.

InsertObjects<T>(IDictionary<string, T>, DateTimeOffset?)

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

public IObservable<Unit> InsertObjects<T>(IDictionary<string, T> keyValuePairs, DateTimeOffset? absoluteExpiration = null)

Parameters

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 object associated with the blob.

Invalidate(IEnumerable<string>)

Remove several keys from the cache. If the key doesn't exist, this method should do nothing and return (not throw KeyNotFoundException).

public IObservable<Unit> Invalidate(IEnumerable<string> keys)

Parameters

keys IEnumerable<string>

The key to remove from the cache.

Returns

IObservable<Unit>

A observable that signals when the operational is completed.

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 string

The 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 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>(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 IObservable<Unit> InvalidateObjects<T>(IEnumerable<string> keys)

Parameters

keys IEnumerable<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.

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.