An easy-to-use and fast caching framework for .NET with support for many storage systems:
- Redis
- System.Runtime.Caching.MemoryCache
- Portable Memory Cache for use with Windows Store, Windows Phone 8, Xamarin apps
| Package | Description | Version |
|---|---|---|
Provision |
The cache handler core | |
Provision.Config |
Config file support | |
Provision.Providers.Redis |
A cache handler using Redis for storage | |
Provision.Providers.MemoryCache |
A cache handlerr using System.Runtime.Cache for storage |
|
Provision.Providers.PortableMemoryCache |
A cache handler using a cross-platform memory table for storage |
The cache item expire time is by default 0 0/1 * 1/1 * ? * which equals to one minute.
var cacheHandler = new RedisCacheHandler(new RedisCacheHandlerConfiguration("localhost", 6379, 3));
var cacheHandler = new MemoryCacheHandler(new MemoryCacheHandlerConfiguration("0 0 0/1 1/1 * ? *"));
var cacheHandler = new PortableMemoryCacheHandler(new PortableMemoryCacheHandlerConfiguration("0 0 0/1 1/1 * ? *"));var cacheHandlers = ProvisionConfiguration.Current.GetHandlers();<configuration>
<configSections>
<section name="provision" type="Provision.Config.ProvisionConfiguration, Provision.Config" />
</configSections>
<provision handler="Provision.Providers.Redis.RedisCacheHandler, Provision.Providers.Redis" defaultConfiguration="redis">
<add name="redis" type="Provision.Providers.Redis.RedisCacheHandlerConfiguration, Provision.Providers.Redis" database="3" host="10.1.14.149" prefix="glue" expireTime="0 0 0/1 1/1 * ? *"/>
<!-- Expires all items automatically after 1 hour if nothing else is specified when adding the item to the cache -->
</provision>
</configuration>The CacheHandlerCollection is used like a cachehandler, meaning you just use AddOrUpdate, Contains, Get, etc like normal.
Provision will contact the handlers behind the scenes.
var cacheHandlers = new CacheHandlerCollection()
{
new MemoryCacheHandler(new MemoryCacheHandlerConfiguration(TimeSpan.FromSeconds(30))),
new RedisCacheHandler(new RedisCacheHandlerConfiguration("localhost", 6379, 3, null, "provision", 512, null, true))
};var cacheHandler = new MemoryCacheHandler();
var d = new Report() { Items = new List<ReportItem>() { new ReportItem() { Key = "1", Data = 100 } } };
var key = cacheHandler.CreateKey("xyz", "1", "something"); // Returns "xyz_1_something"
await cacheHandler.AddOrUpdate(key, d, DateTime.Now.AddDays(1)); // Adds the report to the cache with the key and sets the expiry date to 1 day forward
// NOTE: The expiry date is optional, the cache handler will use the global value for the cache handler if not specifiedvar cacheHandler = new MemoryCacheHandler();
var key = cacheHandler.CreateKey("xyz", "1", "something"); // Returns "xyz_1_something"
var cacheItem = await cacheHandler.Get<Report>(key); // Gets the cache item wrapper with the specified key
var report = cacheItem.Value;
// You can also use the following shorthand to get the value:
var report2 = await cacheHandler.GetValue<Report>(key);var cacheHandler = new MemoryCacheHandler();
var key = cacheHandler.CreateKey("xyz", "1", "something"); // Returns "xyz_1_something"
await cacheHandler.Remove(key); // Removes the cache item with the specified keyvar cacheHandler = new MemoryCacheHandler();
var key = cacheHandler.CreateKey("xyz", "1", "something"); // Returns "xyz_1_something"
var exists = await cacheHandler.Contains(key);var cacheHandler = new MemoryCacheHandler();
var purged = await cacheHandler.Purge();When using the RedisCacheHandler it is important to note that the RedisCacheHandlerConfiguration object that you pass into the constructor should be reused across your application.
Most IoC containers have options for setting an instance of an interface to a singleton, use this if possible.
Mono for ConcurrentDictionary, ReadOnlyCollection and SplitOrderedList used in the PortableMemoryCacheHandler
Quartz.NET for the Cron expression parser used for config parsing.
C5 for the TreeSet collection used by Quartz.NET.