asp.net forums 缓存

今天来说说其业务逻辑层。

业务逻辑层,此项目用的是Component模式(微软推荐)开发。这个项目(层)里面的比较复杂,我从缓存讲起

先看他的代码:

using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using AspNetForums.Configuration;

namespace AspNetForums
{
    /// <summary>
    /// 缓存管理
    /// </summary>
    public class ForumsCache
    {
        #region 构造器
        private ForumsCache()
        {
        }
        static ForumsCache()
        {
            HttpContext context = HttpContext.Current;
            if (context != null)
            {
                _cache = context.Cache;
            }
            else
            {
                _cache = HttpRuntime.Cache;
            }
        }
        #endregion

        #region 公有靜态字段
        /// <summary>
        /// 日缓存因子
        /// </summary>
        public static readonly int DayFactor = 17280;
        /// <summary>
        /// 小时缓存因子
        /// </summary>
        public static readonly int HourFactor = 720;
        /// <summary>
        /// 分缓存因子
        /// </summary>
        public static readonly int MinuteFactor = 12;
        /// <summary>
        /// 秒缓存因子
        /// </summary>
        public static readonly double SecondFactor = 0.2;
        #endregion

        #region 私有静态字段
        private static readonly Cache _cache;
        /// <summary>
        /// 缓存因子
        /// </summary>
        private static int Factor = 5;
        #endregion

        #region 静态方法
        /// <summary>
        /// 重新设置缓存因子
        /// </summary>
        /// <param name="cacheFactor"></param>
        public static void ReSetFactor(int cacheFactor)
        {
            Factor = cacheFactor;
        }

        /// <summary>
        /// 清空所有缓存项目
        /// </summary>
        public static void Clear()
        {
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            ArrayList al = new ArrayList();
            while (CacheEnum.MoveNext())
            {
                al.Add(CacheEnum.Key);
            }

            foreach (string key in al)
            {
                _cache.Remove(key);
            }

        }
        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="pattern">缓存键匹配模式</param>
        public static void RemoveByPattern(string pattern)
        {
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
            while (CacheEnum.MoveNext())
            {
                if (regex.IsMatch(CacheEnum.Key.ToString()))
                    _cache.Remove(CacheEnum.Key.ToString());
            }
        }

        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="key">缓存键名</param>
        public static void Remove(string key)
        {
            _cache.Remove(key);
        }

        /// <summary>
        /// 增加缓存项目
        /// </summary>
        /// <param name="key">缓存键名</param>
        /// <param name="obj">缓存对象</param>
        public static void Insert(string key, object obj)
        {
            Insert(key, obj, null, 1);
        }
        /// <summary>
        /// 增加缓存项目(缓存时间:小时缓存因子*12)
        /// </summary>
        /// <param name="key">缓存键名</param>
        /// <param name="obj">缓存对象</param>
        /// <param name="dep">缓存依赖荐</param>
        public static void Insert(string key, object obj, CacheDependency dep)
        {
            Insert(key, obj, dep, HourFactor * 12);
        }
        /// <summary>
        /// 增加缓存项目
        /// </summary>
        /// <param name="key">缓存键名</param>
        /// <param name="obj">缓存对象</param>
        /// <param name="seconds">缓存秒数</param>
        public static void Insert(string key, object obj, int seconds)
        {
            Insert(key, obj, null, seconds);
        }
        /// <summary>
        /// 增加缓存项目
        /// </summary>
        /// <param name="key">缓存键名</param>
        /// <param name="obj">缓存对象</param>
        /// <param name="seconds">缓存秒数</param>
        /// <param name="priority">缓存优先级</param>
        public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
        {
            Insert(key, obj, null, seconds, priority);
        }
        /// <summary>
        /// 增加缓存项目
        /// </summary>
        /// <param name="key">缓存键名</param>
        /// <param name="obj">缓存对象</param>
        /// <param name="dep">缓存依赖项</param>
        /// <param name="seconds">缓存秒数</param>
        public static void Insert(string key, object obj, CacheDependency dep, int seconds)
        {
            Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
        }
        /// <summary>
        /// 增加缓存
        /// </summary>
        /// <param name="key">缓存键名</param>
        /// <param name="obj">缓存对象</param>
        /// <param name="dep">缓存依赖项</param>
        /// <param name="seconds">缓存秒数</param>
        /// <param name="priority">缓存优先级</param>
        public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, dep, DateTime.Now.AddSeconds(Factor * seconds), Cache.NoSlidingExpiration, priority, null);
            }

        }
        /// <summary>
        /// 微小缓存
        /// </summary>
        /// <param name="key">缓存键名</param>
        /// <param name="obj">缓存对象</param>
        /// <param name="secondFactor">缓存秒因子</param>
        public static void MicroInsert(string key, object obj, int secondFactor)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, null, DateTime.Now.AddSeconds(Factor * secondFactor), Cache.NoSlidingExpiration);
            }
        }

        /// <summary>
        /// 增加缓存项目
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        public static void Max(string key, object obj)
        {
            Max(key, obj, null);
        }
        /// <summary>
        /// 最大缓存对象
        /// </summary>
        /// <param name="key">缓存键名</param>
        /// <param name="obj">缓存对象</param>
        /// <param name="dep">缓存依赖项</param>
        public static void Max(string key, object obj, CacheDependency dep)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, dep, DateTime.MaxValue, Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal, null);
            }
        }
        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <param name="key">缓存键名</param>
        /// <returns>返回缓存对象</returns>
        public static object Get(string key)
        {
            return _cache[key];
        }

        /// <summary>
        /// 根据秒缓存因子计算秒数
        /// </summary>
        public static int SecondFactorCalculate(int seconds)
        {
            return Convert.ToInt32(Math.Round((double)seconds * SecondFactor));
        }
        #endregion

    }
}

 

这么长的一段代码,我觉得大家都应该没有仔细看,就看我下面怎么分析了,呵呵。好的下面来说说这个类。

里面定义了一些时间因子,是用来设置缓存的时间的,这样的话更方便,不用理会。

这里面比较关键的就是那个静态的只读的_cache对象: private static readonly Cache _cache; 这里面需要注意的是:静态,只读

实际上系统的缓存,就是把需要缓存进来的东西添加到这个静态成员里面

恩,知道了这些,还需要关心一个特别重要的事情就是他的静态的构造函数,因为只有那个静态的构造函数对这个私有的变量来赋值(也只能是静态构造函数对静态只读变量赋值,关于静态构造函数,请参见:http://msdn2.microsoft.com/zh-cn/library/k9x6w0hc(VS.80).aspx)

下面我们来看看,这个构造函数给这个静态成员赋什么值了?

static ForumsCache()
        {
            HttpContext context = HttpContext.Current;
            if (context != null)
            {
                _cache = context.Cache;
            }
            else
            {
                _cache = HttpRuntime.Cache;
            }
        }

这是他的代码,这段代码我在看的时候,我有一个疑问,猛地一看这个代码,你会觉得

呀,怎么_cache对象总是在变化啊,用一次这个类,_cache不就是要变化一次?那么还有缓存的作用么?呵呵,如果您有和本人一样的想法,那就是对静态构造函数不理解了,“静态构造函数用于初始化任何静态数据,或用于执行仅需执行一次的特定操作。在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数。”--摘自msdn,也就是说,只要赋值一次,我们的_cache就再也不会变了,于是也就起到了缓存的作用。


这个缓存的类很实用,也有复用价值,需要缓存的东西只要调用Insert 方法就ok了,想在缓存里面获得数据调用Get方法,

 

posted on 2008-03-29 21:30  prayforsmile  阅读(201)  评论(0编辑  收藏  举报