该缓存类与是基于企业库的caching组件的。公开有两个方法,分别是写缓存和读缓存。代码如下:

namespace Tmac.Utilities
{
    /// <summary>
    /// 缓存辅助类
    /// </summary>
    public class CacheUtil
    {
        private static ICacheManager cacheManager = CacheFactory.GetCacheManager();
        private static string filePath = AppConfigUtil.FilePath;//文件路径

        /// <summary>
        /// write cache
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="isFileMode"></param>
        public static void WriteCache(string key, object value,bool isFileMode)
        {
            try
            {
                if (isFileMode)//为true表示本地文件,创建文件依赖
                {
                    FileDependency fd = new FileDependency(filePath);
                    cacheManager.Add(key,value,CacheItemPriority.High,null,fd);
                }
                else
                {
                    cacheManager.Add(key,value,CacheItemPriority.High,null,new AbsoluteTime(DateTime.Now.AddSeconds(10)));//指定10秒后过期
                }
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// read cache
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheManager"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T ReadCache<T>(ICacheManager cacheManager,string key)
        {
            T obj = default(T);
            if (cacheManager.Contains(key))
            {
                obj = (T)cacheManager.GetData(key);
            }
            return obj;
        }
    }
}

 

posted on 2013-03-19 22:41  永远的麦子  阅读(2348)  评论(0编辑  收藏  举报