Skip to content
This repository was archived by the owner on Jan 18, 2026. It is now read-only.

ExadelBoulder/provision

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Provision

TeamCity Build Status

An easy-to-use and fast caching framework for .NET with support for many storage systems:

Package Description Version
Provision The cache handler core NuGet Version
Provision.Config Config file support NuGet Version
Provision.Providers.Redis A cache handler using Redis for storage
Provision.Providers.MemoryCache A cache handlerr using System.Runtime.Cache for storage NuGet Version
Provision.Providers.PortableMemoryCache A cache handler using a cross-platform memory table for storage NuGet Version

Usage

Default configuration

The cache item expire time is by default 0 0/1 * 1/1 * ? * which equals to one minute.

Basic initialization

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 * ? *"));

Configure from app.config or web.config

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>

Support for tiered caching

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

Add object to cache

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 specified

Get object from cache

var 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);

Remove object from cache

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 key

Check if key exists in cache

var cacheHandler = new MemoryCacheHandler();

var key = cacheHandler.CreateKey("xyz", "1", "something"); // Returns "xyz_1_something"
var exists = await cacheHandler.Contains(key);

Purge cache

var cacheHandler = new MemoryCacheHandler();

var purged = await cacheHandler.Purge();

Important Notes

Object Lifetime when using Redis

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.

Credits

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.

About

An easy-to-use and fast caching system for .NET with support for MemoryCache, Redis

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 99.4%
  • Batchfile 0.6%