1#
调用:

CacheInstance.Couch.Get<string>(verifyCodeKey);


需要工具类

    public static class CacheInstance

    {

        public static ICachePolicy Local { get { return CacheBuilder.Instance["Local"]; } }



        public static ICachePolicy Couch { get { return CacheBuilder.Instance["Couch"]; } }



        public static ICachePolicy Redis { get { return CacheBuilder.Instance["Redis"]; } }



        public static ICachePolicy CouchLocal { get { return CacheBuilder.Instance["CouchLocal"]; } }



        public static ICachePolicy LocalCouch { get { return CacheBuilder.Instance["LocalCouch"]; } }        



        public class CacheCollection

        {

                private static IDictionary<string, ICachePolicy> _allCache = new Dictionary<string, ICachePolicy>();

                private static void InitAllCache()

                {

                        _allCache["Local"] = Local;

                        _allCache["Couch"] = Couch;

                        _allCache["Redis"] = Redis;

                        _allCache["CouchLocal"] = CouchLocal;

                        _allCache["LocalCouch"] = LocalCouch;

                }

                

                public ICachePolicy this[string name]

                {

                        get

                        {

                                if (!_allCache.Any())

                                {

                                        lock(_allCache)

                                        {

                                                if (!_allCache.Any())

                                                {

                                                        InitAllCache();

                                                }

                                        }

                                }

                                

                                return _allCache[name];

                        }

                }

        }        



        public static CacheCollection Collection = new CacheCollection();      

    }


引用相关:



  public interface ICachePolicy

    {

        void Add(string key, object value, TimeSpan expire);



        void Remove(string key);



        TData Get<TData>(string key);



        TData Get<TData>(string key, TimeSpan newExpiration);



        bool TryGet<TData>(string key, out TData value);



        bool TryGet<TData>(string key, TimeSpan newExpiration, out TData value);

    }



    public interface ICasCachePolicy : ICachePolicy

    {

        CasedResult<bool> CasAdd(string key, object value, TimeSpan expire, ulong cas);



        CasedResult<TData> GetWithCas<TData>(string key);

    }



    public class CasedResult<TData>

    {

        public TData Result { get; set; }

        public ulong Cas { get; set; }

    }



    public interface IMixedCachePolicy : ICachePolicy

    {

        void SetPolicies(params ICachePolicy[] policies);



        ICachePolicy[] Policies { get; }

    }


以及

    public class CacheBuilder
    {
        private StructureMap.Container _container = new StructureMap.Container();

        private class Nested
        {
            static Nested() { }
            internal static readonly CacheBuilder Builder = new CacheBuilder();
        }

        public static CacheBuilder Instance
        {
            get { return Nested.Builder; }
        }

        public CacheBuilder()
        {
            Init();
        }

        private void Init()
        {
            _container.Configure(config =>
                                     {
                                         config.ForSingletonOf<ICachePolicy>().Use(context =>
                                                                     new RuntimeCachePolicy()).Named("Local");
                                         config.ForSingletonOf<ICachePolicy>().Use(context =>
                                                                     new CouchBasePolicy()).Named("Couch");
                                         config.ForSingletonOf<ICachePolicy>().Use(context =>
                                                                     new RedisCachePolicy()).Named("Redis");
                                     });

            _container.Configure(config =>
                                     {
                                         config.ForSingletonOf<ICachePolicy>().Use(context =>
                                                                     new MixedCachePolicy(this["Couch"], this["Local"])).Named("CouchLocal");
                                         config.ForSingletonOf<ICachePolicy>().Use(context =>
                                                                     new MixedCachePolicy(this["Local"], this["Couch"])).Named("LocalCouch");
                                     });
        }

        public ICachePolicy this[string key]
        {
            get { return _container.GetInstance<ICachePolicy>(key); }
        }

    }
还有

internal class RuntimeCachePolicy : ICachePolicy
    {
        public void Add(string key, object value, TimeSpan expire)
        {
            HttpRuntime.Cache.Add(key, value, null, DateTime.Now.Add(expire), System.Web.Caching.Cache.NoSlidingExpiration,
                CacheItemPriority.High, OnRemove);
        }

        public void Remove(string key)
        {
            HttpRuntime.Cache.Remove(key);
        }

        private object Get(string key)
        {
            return HttpRuntime.Cache.Get(key);
        }

        private object Get(string key, TimeSpan newExpiration)
        {
            var obj = Get(key);
            if (obj != null)
            {
                Add(key, obj, newExpiration);
            }

            return obj;
        }

        public TData Get<TData>(string key)
        {
            return (TData) Get(key);
        }

        public TData Get<TData>(string key, TimeSpan newExpiration)
        {
            return (TData)Get(key, newExpiration);
        }

        public bool TryGet<TData>(string key, out TData value)
        {
            var obj = Get(key);
            value = (TData) obj;

            return obj != null;
        }


        public bool TryGet<TData>(string key, TimeSpan newExpiration, out TData value)
        {
            var got = TryGet(key, out value);
            if (got)
            {
                Add(key, value, newExpiration);
            }

            return got;
        }

        private void OnRemove(string key, object val, CacheItemRemovedReason reason)
        {
            switch (reason)
            {
                case CacheItemRemovedReason.DependencyChanged:
                {
                    break;
                }
                case CacheItemRemovedReason.Expired:
                {
                    break;
                }
                case CacheItemRemovedReason.Removed:
                {
                    break;
                }
                case CacheItemRemovedReason.Underused:
                {
                    break;
                }
                default:
                    break;
            }
        }
    }