Quantcast
Channel: Codeology » Caching
Viewing all articles
Browse latest Browse all 5

Bettering the better .NET cache

$
0
0

It’s been nearly three years since I first wrote SharpCache so I recently revisited the code to see what could be added, improved or removed. The result was a complete reworking of the code base to an almost new implementation.

The first major refactor is the removal of the ability to register multiple providers. In most use cases of the cache only one provider is ever used, so registering and maintaining a list of other providers was a waste of time. To that effect Cache now only has a simple property Provider which can be set to any class that implements ICacheProvider.

The second major refactor is a set of asynchronous methods analogous to the existing synchronous ones. Caching happens in a ThreadPool thread and calls a callback once complete. The signatures of the methods can be seen in the extract below.

    public delegate void CacheCallback(object state);
    public delegate void CacheExistsCallback(bool exists, object state);
    public delegate void CacheGetCallback(object value, object state);

    public class Cache
    {

        ...

        public static void ClearAsync(CacheCallback callback, object state);
        public static void ExistsAsync(string key, CacheExistsCallback callback, object state);
        public static void GetAsync(string key, CacheGetCallback callback, object state);
        public static void SetAsync(string key, object value, CacheCallback callback, object state);
        public static void SetAsync(string key, object value, int minutes, CacheCallback callback, object state);
        public static void SetAsync(string key, object value, TimeSpan ts, CacheCallback callback, object state);
        public static void SetAsync(string key, object value, DateTime dt, CacheCallback callback, object state);
        public static void UnsetAsync(string key, CacheCallback callback, object state);

        ...

    }

I’ve also refactored the several of the supplied example cache providers.

The LocalCacheProvider now supports memory limits, and by default is limited to a cache size of 100MB. If the cache grows beyond this the oldest items will be deleted until the memory falls below this limit. I’ve also modified the MemcacheCacheProvider so that it no longer requires an external assembly dependency. I’ve basically included the BeIT Memcached client library source into the library.

There’s still improvments to be made nothing is perfect, but this will hopefully serve well for another three years.

To download the new SharpCache simplly checkout the Github repository, or download one of the files below.

sharpcache-release-2.0.4656.zip
27 KB
sharpcache-debug-2.0.4656.zip
68 KB
sharpcache-source-2.0.4656.zip
49 KB

Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images