代码改变世界

CacheStrategy缓存

2013-04-18 11:06 by leo.wl2020, ... 阅读, ... 评论, 收藏, 编辑

提高应用服务器性能,针对经常不变内容进行缓存.

1.CacheStrategy:

using System;
    using System.Web.Caching;

    /// <summary>
    /// CacheStrategy
    /// </summary>
    public class CacheStrategy : ICacheStrategy
    {
        /// <summary>
        /// webCache
        /// </summary>
        private static volatile System.Web.Caching.Cache webCache = System.Web.HttpRuntime.Cache;

        #region ICacheStrategy Members
        /// <summary>
        /// 添加指定ID的对象
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="value">缓存对象</param>
        public void Put(string key, object value)
        {
            if (key == null || key.Length == 0 || key == null)
            {
                return;
            }

            webCache.Insert(key, value, null, DateTime.MaxValue, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
        }

        /// <summary>
        /// 添加指定ID的对象
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="value">缓存对象</param>
        /// <param name="expire">到期时间,单位:秒</param>
        public void Put(string key, object value, int expire)
        {
            if (key == null || key.Length == 0 || key == null)
            {
                return;
            }

            webCache.Insert(key, value, null, DateTime.Now.AddSeconds(expire), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
        }

        /// <summary>
        /// 添加指定ID的对象(关联指定文件组)
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="value">缓存对象</param>
        /// <param name="file">关联的文件名</param>
        public void PutWithFileChange(string key, object value, string file)
        {
            string[] files = new string[1] { file };
            this.PutWithFileChange(key, value, files);
        }

        /// <summary>
        /// 添加指定ID的对象(关联指定文件组)
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="value">缓存对象</param>
        /// <param name="files">关联的文件名组</param>
        public void PutWithFileChange(string key, object value, string[] files)
        {
            if (key == null || key.Length == 0 || key == null)
            {
                return;
            }

            CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(this.OnRemove);
            CacheDependency dep = new CacheDependency(files, DateTime.Now);
            webCache.Insert(key, value, dep, DateTime.MaxValue, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, callBack);
        }

        /// <summary>
        /// 返回指定ID的对象
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns></returns>
        public object Get(string key)
        {
            if (key == null || key.Length == 0)
            {
                return null;
            }

            return webCache.Get(key);
        }

        /// <summary>
        /// 移除指定ID的对象
        /// </summary>
        /// <param name="key">缓存键</param>
        public void Remove(string key)
        {
            if (key == null || key.Length == 0)
            {
                return;
            }

            webCache.Remove(key);
        }

        /// <summary>
        /// 建立回调委托的一个实例
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="reason"></param>
        public 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;
            }
        }
        #endregion
    }

2.ICacheStrategy

/// <summary>
    /// 公共缓存策略接口
    /// </summary>
    public interface ICacheStrategy
    {
        /// <summary>
        /// 添加指定ID的对象
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="value">缓存对象</param>
        void Put(string key, object value);

        /// <summary>
        /// 添加指定ID的对象
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="value">缓存对象</param>
        /// <param name="expire">到期时间,单位:秒</param>
        void Put(string key, object value, int expire);

        /// <summary>
        /// 添加指定ID的对象(关联指定文件组)
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="value">缓存对象</param>
        /// <param name="file">关联的文件名</param>
        void PutWithFileChange(string key, object value, string file);

        /// <summary>
        /// 添加指定ID的对象(关联指定文件组)
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="value">缓存对象</param>
        /// <param name="files">关联的文件名组</param>
        void PutWithFileChange(string key, object value, string[] files);

        /// <summary>
        /// 返回指定ID的对象
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns>object</returns>
        object Get(string key);

        /// <summary>
        /// 移除指定ID的对象
        /// </summary>
        /// <param name="key">缓存键</param>
        void Remove(string key);
    }

依赖接口,可以增加程序的可测试性,方便使用TDD驱动开发.