zoukankan      html  css  js  c++  java
  • 公用类库(4) 缓存操作类CacheUtil

      该缓存类与是基于企业库的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;
            }
        }
    }
  • 相关阅读:
    人的一生为什么要努力 &1
    数据库_数据库系统概论
    电子商务安全
    虚拟专用网技术
    人的一生为什么要努力
    数据备份与恢复技术
    入侵检测技术
    简历模板连接
    防火墙技术
    字节与位
  • 原文地址:https://www.cnblogs.com/mcgrady/p/2970170.html
Copyright © 2011-2022 走看看