AspNetCoreCache is a ASP.NET Core cache wrapper for quick and effective cache usage. It deals with all the hard work for having sourced caches and a cache manager to make life easier.
The SourceCache can be used when you want a single variable type to be cached and want all the process to be fully managed for you apart from its data source.
You can use the SourceExample like below to add your own data source into the equation.
public class SourceExample : SourceCache<Dictionary<string,string>>
{
private const string CACHENAMEKEY = "exampleCacheKeyName";
public SourceExample(IDistributedCache cache) : base(CACHENAMEKEY, cache, new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromDays(1)))
{
}
//This function allows you to use a source for the cache, such as a database call
public override Task<Dictionary<string, string>> GetItemFromSource()
{
return Task.Run(() => { return new Dictionary<string, string>(); });
}
}
You can then also add your inherited class into your Startup.cs -> ConfigureServices function to add it to the Dependency Injection.
services.AddSingleton<ISourceCache<Dictionary<string, string>>, SourceExample>();
You can then use the DI like so.
public class ExampleController : ControllerBase
{
private readonly ISourceCache<Dictionary<string, string>> _SourceCache;
public ExampleController(ISourceCache<Dictionary<string,string>> sourceCache)
{
_SourceCache = sourceCache;
}
}
You can create an instance of your class like so.
SourceExample sourceCache = new SourceExample(IDistributedMemoryExample);
Once you have an instance of the class, you can then use the following functions.
//Pulls the item out of the cache
var item = await sourceCache.GetCacheItem();
//Refreshes the cache to load from the datasource again
await sourceCache.RefreshCache();
The SingleCacheManager can be used when you have a single type you want to cache but you want full control of the get,add/update and clear capabilities rather than it being taken care of automatically for you.
You can use the SingleCacheManager like below
public class SingleCacheExample : SingleCacheManager<Dictionary<string, string>>
{
private const string CACHENAMEKEY = "exampleCacheKeyName";
public SingleCacheExample(IDistributedCache cache) : base(CACHENAMEKEY, cache, new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromDays(1)))
{
}
}
You can then also add your inherited class into your Startup.cs -> ConfigureServices function to add it to the Dependency Injection.
services.AddSingleton<ISingleCacheManager<Dictionary<string, string>>, SingleCacheExample>();
You can then use the DI like so.
public class ExampleController : ControllerBase
{
private readonly ISingleCacheManager<Dictionary<string, string>> _SingleCacheManager;
public ExampleController(ISingleCacheManager<Dictionary<string,string>> singleCacheManager)
{
_SingleCacheManager = singleCacheManager;
}
}
You can create an instance of your class like so.
SingleCacheExample singleCache = new SingleCacheExample(IDistributedMemoryExample);
Once you have an instance of the class, you can then use the following functions.
//Pulls the item out of the cache
var item = await singleCache.GetCacheItem();
//Adds/Updates the cache with the supplied variable
await singleCache.SetUpdateCacheItem(newOrUpdatedVariable);
//Clears the cache down
await singleCache.ClearCache();
The CacheManager can be used when you have multiple types you want to cache but you want full control of the get, add/update and clear capabilities rather than it being taken care of automatically for you.
You can use the CacheManager like below
CacheManager cacheManager = new CacheManager(IDistributedMemoryExample, MemoryOptionsExample);
You can then also add the CacheManager into your Startup.cs -> ConfigureServices function to add it to Dependency Injection.
services.AddSingleton<ICacheManager, CacheManager>();
You can then use the DI like so.
public class ExampleController : ControllerBase
{
private readonly ICacheManager _CacheManager;
public ExampleController(ICacheManager cacheManager)
{
_CacheManager = cacheManager;
}
}
You can create an instance of your class like so.
CacheManager cacheManager = new CacheManager(IDistributedMemoryExample, MemoryOptionsExample);
Once you have an instance of the class, you can then use the following functions.
//Pulls the item out of the cache as the specified variable type with the provided key
var item = await cacheManager.GetCacheItem<TypeExpectedOut>("CacheKeyNameHere");
//Adds/Updates the cache with the supplied variable for the specified key
await cacheManager.SetUpdateCacheItem("CacheKeyNameHere", newOrUpdatedVariable);
//Clears the cache down for that particular key
await cacheManager.ClearCache("CacheKeyNameHere");
Copyright © 2019 David Whitehead
This project is licensed under the MIT License.