缓存

记录一下写服务器缓存的代码,

 

    public interface ICacheStorageProvider
    {
        object Get(string key);
        void Put(string key, object value);
        void Put(string key, object value, TimeSpan validFor);
        void Remove(string key);
        void Clear();
    }

  

public class DefaultCacheStorageProvider : ICacheStorageProvider
    {

        public void Put(string key, object value)
        {
            HttpRuntime.Cache.Insert(
                key,
                value,
                null,
                System.Web.Caching.Cache.NoAbsoluteExpiration,
                System.Web.Caching.Cache.NoSlidingExpiration,
                System.Web.Caching.CacheItemPriority.Normal,
                null);
        }

        public void Put(string key, object value, TimeSpan validFor)
        {
            HttpRuntime.Cache.Insert(
                key,
                value,
                null,
                System.Web.Caching.Cache.NoAbsoluteExpiration,
                validFor,
                System.Web.Caching.CacheItemPriority.Normal,
                null);
        }

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

        public void Clear()
        {
            var all = HttpRuntime.Cache
                .AsParallel()
                .Cast<DictionaryEntry>()
                .Select(x => x.Key.ToString())
                .ToList();

            foreach (var key in all)
            {
                Remove(key);
            }
        }

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

  

    public interface ICacheService
    {
        object Get(string key);
        void Put(string key, object value);
        void Put(string key, object value, TimeSpan validFor);

        void Remove(string key);
        void Clear();
    }

  

public class DefaultCacheService : ICacheService
    {
        private readonly ICacheStorageProvider _cacheStorageProvider;
        private readonly string _prefix;

        public DefaultCacheService(ICacheStorageProvider cacheStorageProvider, string prefix)
        {
            _cacheStorageProvider = cacheStorageProvider;
            _prefix = prefix;
        }

        public object Get(string key)
        {
            return _cacheStorageProvider.Get(BuildFullKey(key));
        }

        public void Put(string key, object value)
        {
            _cacheStorageProvider.Put(BuildFullKey(key), value);
        }

        public void Put(string key, object value, TimeSpan validFor)
        {
            _cacheStorageProvider.Put(BuildFullKey(key), value, validFor);
        }

        public void Remove(string key)
        {
            _cacheStorageProvider.Remove(BuildFullKey(key));
        }

        public void Clear()
        {
            _cacheStorageProvider.Clear();
        }

        private string BuildFullKey(string key)
        {
            return String.Concat(_prefix, "_", key);
        }
    }

  创建完以上四个类之后,就可以开始着手使用缓存了。

    public class BaseDataCache
    {
        private static readonly ICacheService cacheServiceSpecial;
        static BaseDataCache()
        {
            ICacheStorageProvider cacheStorageProvider = new DefaultCacheStorageProvider();
            cacheServiceSpecial = new DefaultCacheService(cacheStorageProvider, "Special");
        }

        /// <summary>
        /// 本缓存用于资源和视频页面
        /// </summary>
        /// <param name="schoolPhaseId"></param>
        /// <param name="subjectId"></param>
        /// <returns></returns>
        public static List<SpecialCacheDto> GetSpecial(int schoolPhaseId, int subjectId)
        {
            var basicDataContext = BasicDataContext.GetContext();
            var key = "Special_" + schoolPhaseId + "_" + subjectId;
            var specialWebDic = cacheServiceSpecial.Get(key) as List<SpecialCacheDto>;
            if (specialWebDic == null)
            {
                specialWebDic = basicDataContext.Specials.Where(s => s.SchoolPhaseID == schoolPhaseId && s.SubjectID == subjectId && !s.Name.Contains("旧")).Select(s => new SpecialCacheDto
                {
                    SpecialId = s.SpecialID ?? 0,
                    SpecialName = s.Name,
                    ParentId = s.ParentID ?? 0,
                    ChildCount = s.ChildCount,
                }).ToList();
                cacheServiceSpecial.Put(key, specialWebDic, new TimeSpan(1, 0, 0, 0));
            }
            return specialWebDic;
        }
    }

  

posted on 2018-01-22 17:01  木头人段  阅读(144)  评论(0编辑  收藏  举报

导航