Simple cache implementation for .NET
Install-Package SimpleCacheSharp
Visual Studio 2015 required.
Restore nuget packages and copy SQLITE.Interop.dll from x86 and x64 in packages/System.Data.SQLite.Core.1.0.xxx.0/build/net46/ to costura32 and costura64 in SimpleCacheSharp/ folder. (x86 goes into costura32 and x64 into costura64)
Or to put it in code:
copy packages/System.Data.SQLite.Core.1.0.xxx.0/build/net46/x86/SQLite.Interop.dll SimpleCacheSharp/costura32
copy packages/System.Data.SQLite.Core.1.0.xxx.0/build/net46/x64/SQLite.Interop.dll SimpleCacheSharp/costura64
Open solution and hit build. Now reference SimpleCacheSharp.dll in your project.
Create an instance of the cache that is stored on the disk:
var cache = CacheFactory.Configure().UsingFile("cache.db").BuildCache();
using Encrypt() the cache file can be protected using a password:
var cache = CacheFactory.Configure().UsingFile("cache.db").Encrypt("TopSecret").BuildCache();
If you want to store the cache in memory instead use InMemory():
var cache = CacheFactory.Configure().InMemory().BuildCache();
All methods in ICache are async since reading and writing to the database are potentially expensive operations.
await cache.Set("dataKey", "my cached value");
Will store my cached value under dataKey with no expiry (the value will be keep cached forever)
If an item with the same was already cached, a call to Set() with the same key, will update its value.
If you want your data to expire after some time use one of the Set() overloads that either take a DateTime (entry will expire at the given moment) or a TimeSpan (will expire in xyz seconds/minutes/...):
await cache.Set("anotherKey", "Hello World", TimeSpan.FromSeconds(60));
Note: You cannot store null in the cache, because this value is used to indicate that a key does not exist in the cache.
var cachedValue = await cache.Get("dataKey");
cachedValue will now contain the string my cached value. If nothing was cached using the key dataKey the method will return null.
If you want to remove a value from that cache you simply call Remove()
await cache.Remove("dataKey");
If you want to change the expiry time of a cache item you have to call Expire() with the key of the item to update and the new expiry time.
await cache.Expire("dataKey", TimeSpan.FromSeconds(120));