CacheProvider factory

I have written a simple class to abstract all the caching paradigms in a managed world….

this is the interface:

namespace SdbBackbone.Caching
{
    public interface ICacheProvider
    {
        void AddItem(string key, object obj);
        void AddItem(string key, object obj, int minutesDuration);
        void AddApplicationItem(string key, object obj);
        object ReadItem(string key);
        T Get<T>(string key, Func<T> function, int minutesDuration);
        void Clear();
    }
}

This is the real implementation

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Caching;

namespace SdbBackbone.Caching
{
    public class CacheProvider : ICacheProvider
    {
        private const int DefaultCacheDurationInMinutes = 1;

        private static Cache Cache { get { return HttpRuntime.Cache; } }
        private int _applicationCacheDuration = -1;

        private int ApplicationCacheDuration
        {
            get
            {
                if (_applicationCacheDuration == -1)
                {

                    _applicationCacheDuration = 5;
                }

                return _applicationCacheDuration;
            }
        }

        public void AddItem(string key, object obj)
        {
            AddItem(key, obj, DefaultCacheDurationInMinutes);
        }

        public object ReadItem(string key)
        {
            return Cache.Get(key);
        }

        public void AddItem(string key, object obj, int minutesDuration)
        {
            Cache.Add(key, obj, null, DateTime.Now.AddMinutes(minutesDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
        }

        public void AddApplicationItem(string key, object obj)
        {
            AddItem(key, obj, ApplicationCacheDuration);
        }

        public T Get<T>(string key, Func<T> function, int minutesDuration) 
        {
            if (ReadItem(key) != null)
                return (T)ReadItem(key);

            var item = function();
            AddItem(key, item, minutesDuration);
            return item;
        }

        public void Clear()
        {
            var keys = new List<string>();

            // retrieve application Cache enumerator
            IDictionaryEnumerator enumerator = Cache.GetEnumerator();

            // copy all keys that currently exist in Cache
            while (enumerator.MoveNext())
            {
                keys.Add(enumerator.Key.ToString());
            }

            // delete every key from cache
            foreach (var t in keys)
            {
                Cache.Remove(t);
            }
        }
    }
}

and this is a possible fake for testing purpose

using System;
using System.Collections.Generic;

namespace SdbBackbone.Caching
{
    public class FakeCache : ICacheProvider
    {
        private readonly Dictionary<string, object> _cache = new Dictionary<string, object>(); 
        
        public void AddItem(string key, object obj)
        {
            if(_cache.ContainsKey(key))
                return;
            
            _cache.Add(key, obj);    
        }

        public void AddItem(string key, object obj, int minutesDuration)
        {
            AddItem(key, obj);
        }

        public object ReadItem(string key)
        {
            return (_cache.ContainsKey(key)) ? _cache[key] : null;
        }

        public void AddApplicationItem(string key, object obj)
        {
            AddItem(key, obj);
        }

        public T Get<T>(string key, Func<T> function, int minutesDuration)
        {
            return function();
        }

        public void Clear()
        {
            foreach (var o in _cache)
            {
                _cache.Remove(o.Key);
            }
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s