Sixeyed.Caching available now on NuGet and GitHub!
- by Elton Stoneman
Originally posted on: http://geekswithblogs.net/EltonStoneman/archive/2013/10/22/sixeyed.caching-available-now-on-nuget-and-github.aspxThe good guys at Pluralsight have okayed me to publish my caching framework (as seen in Caching in the .NET Stack: Inside-Out) as an open-source library, and it’s out now. You can get it here: Sixeyed.Caching source code on GitHub, and here: Sixeyed.Caching package v1.0.0 on NuGet. If you haven’t seen the course, there’s a preview here on YouTube: In-Process and Out-of-Process Caches, which gives a good flavour. The library is a wrapper around various cache providers, including the .NET MemoryCache, AppFabric cache, and memcached*. All the wrappers inherit from a base class which gives you a set of common functionality against all the cache implementations: • inherits OutputCacheProvider, so you can use your chosen cache provider as an ASP.NET output cache; • serialization and encryption, so you can configure whether you want your cache items serialized (XML, JSON or binary) and encrypted; • instrumentation, you can optionally use performance counters to monitor cache attempts and hits, at a low level. The framework wraps up different caches into an ICache interface, and it lets you use a provider directly like this: Cache.Memory.Get<RefData>(refDataKey);
- or with configuration to use the default cache provider:
Cache.Default.Get<RefData>(refDataKey);
The library uses Unity’s interception framework to implement AOP caching, which you can use by flagging methods with the [Cache] attribute:
[Cache]
public RefData GetItem(string refDataKey)
- and you can be more specific on the required cache behaviour:
[Cache(CacheType=CacheType.Memory, Days=1]
public RefData GetItem(string refDataKey)
- or really specific:
[Cache(CacheType=CacheType.Disk, SerializationFormat=SerializationFormat.Json, Hours=2, Minutes=59)]
public RefData GetItem(string refDataKey)
Provided you get instances of classes with cacheable methods from the container, the attributed method results will be cached, and repeated calls will be fetched from the cache.
You can also set a bunch of cache defaults in application config, like whether to use encryption and instrumentation, and whether the cache system is enabled at all:
<sixeyed.caching enabled="true">
<performanceCounters instrumentCacheTotalCounts="true"
instrumentCacheTargetCounts="true"
categoryNamePrefix ="Sixeyed.Caching.Tests"/>
<encryption enabled="true"
key="1234567890abcdef1234567890abcdef"
iv="1234567890abcdef"/> <!-- key must be 32 characters, IV must be 16 characters-->
</sixeyed.caching>
For AOP and methods flagged with the cache attribute, you can override the compile-time cache settings at runtime with more config (keyed by the class and method name):
<sixeyed.caching enabled="true">
<targets>
<target keyPrefix="MethodLevelCachingStub.GetRandomIntCacheConfiguredInternal" enabled="false"/>
<target keyPrefix="MethodLevelCachingStub.GetRandomIntCacheExpiresConfiguredInternal" seconds="1"/>
</targets>
It’s released under the MIT license, so you can use it freely in your own apps and modify as required.
I’ll be adding more content to the GitHub wiki, which will be the main source of documentation, but for now there’s an FAQ to get you started.
* - in the course the framework library also wraps NCache Express, but there's no public redistributable library that I can find, so it's not in Sixeyed.Caching.