代码改变世界

关于C#缓存示例

2010-08-31 22:49  音乐让我说  阅读(706)  评论(0编辑  收藏  举报

代码如下:

 

using System;
using System.Web;
using System.Web.Caching;

namespace HR.Config
{
    /// <summary>
    /// 控制类,用于缓存操作
    /// </summary>
    public sealed class CacheAccess
    {
        /// <summary>
        /// 将对象加入到缓存中
        /// </summary>
        /// <param name="cacheKey">缓存键</param>
        /// <param name="cacheObject">缓存对象</param>
        /// <param name="dependency">缓存依赖项</param>
        public static void SaveToCache(string cacheKey, object cacheObject, CacheDependency dependency)
        {
            Cache cache = HttpRuntime.Cache;
            cache.Insert(cacheKey, cacheObject, dependency);
        }

        /// <summary>
        /// 从缓存中取得对象,不存在则返回null
        /// </summary>
        /// <param name="cacheKey">缓存键</param>
        /// <returns>获取的缓存对象</returns>
        public static object GetFromCache(string cacheKey)
        {
            Cache cache = HttpRuntime.Cache;

            return cache[cacheKey];
        }
    }
}

 

代码2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml;
using System.Web;
using System.Web.Caching;

namespace HR.Config
{

    /// <summary>
    /// 读取配置和保存配置的基类
    /// </summary>
    public class BaseConfigManager
    {
        private readonly string cachePrefix = string.Empty;     //缓存Key的前缀,目的是为了不引起Key的重复。
        private readonly string configFilePath = string.Empty; //配置文件的路径

        public BaseConfigManager(string cachePrefixParam, string configFilePathParam)
        {
            cachePrefix = cachePrefixParam;
            configFilePath = configFilePathParam;
        }

        /// <summary>
        /// 根据配置的Key得到Value
        /// </summary>
        /// <param name="configKey">键</param>
        /// <returns>值</returns>
        protected virtual string GetValueByKey(string configKey)
        {
            string key = cachePrefix + "." + configKey; //要保存到Cache中的Key
            object cacheValue = CacheAccess.GetFromCache(key);
            if (cacheValue == null)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(@configFilePath);
                XmlNode node = xmlDoc.GetElementsByTagName(configKey)[0];
                string innerValue = node.InnerText;

                CacheDependency fileDependency = new CacheDependency(@configFilePath);
                CacheAccess.SaveToCache(key, innerValue, fileDependency);
                cacheValue = innerValue;
            }
            return (string)cacheValue;
        }

        /// <summary>
        /// 保存值到配置文件
        /// </summary>
        /// <param name="configKey">键</param>
        /// <param name="value">要保存的值</param>
        protected virtual void SaveValueByKeyAndValue(string configKey, object value)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@configFilePath);
            XmlNode node = xmlDoc.GetElementsByTagName(configKey)[0];
            node.InnerText = value.ToString();
            xmlDoc.Save(@configFilePath);
        }
    }
}

 

 

代码3:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml;
using System.Web;
using System.Web.Caching;

namespace HR.Config
{
    public class TopicConfigManager : BaseConfigManager
    {
        public TopicConfigManager()
            : base("Topic", AppDomain.CurrentDomain.BaseDirectory + "Config/TopicSysConfig.xml")
        {

        }
        public TopicConfigManager(string FilePath)
            : base("Topic", FilePath)
        {

        }
        #region 读取配置

        /// <summary>
        /// 
        /// </summary>
        /// <returns>结果</returns>
        public int GetAddTopicPoints()
        {
            string value = GetValueByKey("AddTopicPoints");
            return int.Parse(value);
        }

        #endregion

        #region 保存配置

        public void SaveAddTopicPoints(int value)
        {
            SaveValueByKeyAndValue("AddTopicPoints", value);
        }

        #endregion
    }
}

 

等待更新...