zoukankan      html  css  js  c++  java
  • c# Cache 使用实例

            /// <summary>
            /// 创建缓存项的文件
            /// </summary>
            /// <param name="key">缓存Key</param>
            /// <param name="obj">object对象</param>
            public static void Insert(string key, object obj)
            {
                //创建缓存 
                HttpContext.Current.Cache.Insert(key, obj);
            }
            /// <summary>
            /// 移除缓存项的文件
            /// </summary>
            /// <param name="key">缓存Key</param>
            public static void Remove(string key)
            {
                //创建缓存
                HttpContext.Current.Cache.Remove(key);
            }
            /// <summary>
            /// 创建缓存项的文件依赖
            /// </summary>
            /// <param name="key">缓存Key</param>
            /// <param name="obj">object对象</param>
            /// <param name="fileName">文件绝对路径</param>
            public static void Insert(string key, object obj, string fileName)
            {
                //创建缓存依赖项
                CacheDependency dep = new CacheDependency(fileName);
                //创建缓存
                HttpContext.Current.Cache.Insert(key, obj, dep);
            }
    
            /// <summary>
            /// 创建缓存项过期
            /// </summary>
            /// <param name="key">缓存Key</param>
            /// <param name="obj">object对象</param>
            /// <param name="expires">过期时间(分钟)</param>
            public static void Insert(string key, object obj, int expires)
            {
                HttpContext.Current.Cache.Insert(key, obj, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, expires, 0));
            }
    
            /// <summary>
            /// 获取缓存对象
            /// </summary>
            /// <param name="key">缓存Key</param>
            /// <returns>object对象</returns>
            public static object Get(string key)
            {
                if (string.IsNullOrEmpty(key))
                {
                    return null;
                }
                try
                {
                    return HttpContext.Current.Cache.Get(key);
                }
                catch
                {
                    return null;
                }
            }
    
            /// <summary>
            /// 获取缓存对象
            /// </summary>
            /// <typeparam name="T">T对象</typeparam>
            /// <param name="key">缓存Key</param>
            /// <returns></returns>
            public static T Get<T>(string key)
            {
                object obj = Get(key);
                return obj == null ? default(T) : (T)obj;
            }
    
          
    
            /// <summary>
            /// 获取数据缓存
            /// </summary>
            /// <param name="CacheKey"></param>
            public static object GetCache(string CacheKey)
            {
                System.Web.Caching.Cache objCache = HttpRuntime.Cache;
                return objCache[CacheKey];
            }
    
            /// <summary>
            /// 设置数据缓存
            /// </summary>
            public static void SetCache(string CacheKey, object objObject)
            {
                System.Web.Caching.Cache objCache = HttpRuntime.Cache;
                objCache.Insert(CacheKey, objObject);
            }
    
            /// <summary>
            /// 移除指定数据缓存
            /// </summary>
            public static void RemoveAllCache(string CacheKey)
            {
                System.Web.Caching.Cache _cache = HttpRuntime.Cache;
                _cache.Remove(CacheKey);
            }
    
            /// <summary>
            /// 移除全部缓存
            /// </summary>
            public static void RemoveAllCache()
            {
                System.Web.Caching.Cache _cache = HttpRuntime.Cache;
                IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
                while (CacheEnum.MoveNext())
                {
                    _cache.Remove(CacheEnum.Key.ToString());
                }
            }
  • 相关阅读:
    java中为什么notify()可能会导致死锁,而notifyAll()则不会
    java中wait()和sleep()的区别;notify()和notifyall()区别
    你不知道的Golang盲点汇总【持续更新】
    rsync性能终极优化【Optimize rsync performance】
    基于cephfs搭建高可用分布式存储并mount到本地
    检测代码潜在bug和质量之SonarQube
    玩透二叉树(Binary-Tree)及前序(先序)、中序、后序【递归和非递归】遍历
    好用到哭!8个技巧让Vim菜鸟变专家
    Golang fmt Printf 格式化参数手册/详解/说明
    淘宝滑动验证码研究
  • 原文地址:https://www.cnblogs.com/feizianquan/p/9709854.html
Copyright © 2011-2022 走看看