Skip to main content

C# example class of a Generic Cache Manager.

using System;
using System.Collections.Generic;

namespace SqlSugar
{
    public interface ICacheManager<V>
    {
        V this[string key] { get; }
        void Add(string key, V value);
        void Add(string key, V value, int cacheDurationInSeconds);
        bool ContainsKey(string key);
        V Get(string key);
        IEnumerable<string> GetAllKey();
        void Remove(string key);
        V Func(string cacheKey, Func<ICacheManager<V>, string, V> successAction, Func<ICacheManager<V>, string, V> errorAction);
        void Action(string cacheKey, Action<ICacheManager<V>, string> successAction, Func<ICacheManager<V>, string, V> errorAction);
    }

    public class CacheManager<V> : ICacheManager<V>
    {
        readonly System.Collections.Concurrent.ConcurrentDictionary<string, V> InstanceCache = new System.Collections.Concurrent.ConcurrentDictionary<string, V>();

        private static CacheManager<V> _instance = null;
        private static readonly object _instanceLock = new object();

        private CacheManager() { }

        public V this[string key]
        {
            get
            {
                return this.Get(key);
            }
        }

        public bool ContainsKey(string key)
        {
            return this.InstanceCache.ContainsKey(key);
        }

        public V Get(string key)
        {
            if (this.ContainsKey(key))
            {
                return this.InstanceCache[key];
            }
            else
            {
                return default(V);
            }
        }

        public static CacheManager<V> GetInstance()
        {
            if (_instance == null)
                lock (_instanceLock)
                    if (_instance == null)
                    {
                        _instance = new CacheManager<V>();
                    }
            return _instance;
        }

        public void Add(string key, V value)
        {
            this.InstanceCache.GetOrAdd(key, value);
        }

        public void Add(string key, V value, int cacheDurationInSeconds)
        {
            Add(key, value);
        }

        public void Remove(string key)
        {
            V val;
            this.InstanceCache.TryRemove(key, out val);
        }

        public IEnumerable<string> GetAllKey()
        {
            return this.InstanceCache.Keys;
        }

        public void Action(string cacheKey, Action<ICacheManager<V>, string> successAction, Func<ICacheManager<V>, string, V> errorAction)
        {
            if (this.ContainsKey(cacheKey))
            {
                successAction(this, cacheKey);
            }
            else
            {
                this.Add(cacheKey, errorAction(this, cacheKey));
            }
        }

        public V Func(string cacheKey, Func<ICacheManager<V>, string, V> successAction, Func<ICacheManager<V>, string, V> errorAction)
        {
            var cm = CacheManager<V>.GetInstance();
            if (cm.ContainsKey(cacheKey))
            {
                return successAction(cm, cacheKey);
            }
            else
            {
                var reval = errorAction(cm, cacheKey);
                cm.Add(cacheKey, reval);
                return reval;
            }
        }
    }

    //Usage:
    public class Usage
    {
		public ICacheManager<T> GetCacheInstance<T>()
		{
			return CacheManager<T>.GetInstance();
		}

		private List<T> GetListOrCache<T>(string cacheKey, string sql)
		{
			return this.GetCacheInstance<List<T>>().Func(cacheKey,
			 (cm, key) =>
			 {
				 return cm[cacheKey];

			 }, (cm, key) =>
			 {
				 this.Context.Ado.IsEnableLogEvent = false;
				 var reval = this.Context.Ado.SqlQuery<T>(sql);
				 this.Context.Ado.IsEnableLogEvent = isEnableLogEvent;
				 return reval;
			 });
		}
    }
}