zoukankan      html  css  js  c++  java
  • C# CacheHepler Class

    internal class CacheHelper
        {
            /// <summary>
            /// Insert value into the cache using
            /// appropriate name/value pairs
            /// </summary>
            /// <typeparam name="T">Type of cached item</typeparam>
            /// <param name="o">Item to be cached</param>
            /// <param name="key">Name of item</param>
            public static void Add<T>(T o, string key)
            {
                // NOTE: Apply expiration parameters as you see fit.
                // In this example, I want an absolute 
                // timeout so changes will always be reflected 
                // at that time. Hence, the NoSlidingExpiration.  
                HttpContext.Current.Cache.Insert(
                    key,
                    o,
                    null,
                    DateTime.Now.AddMinutes(1440),
                    System.Web.Caching.Cache.NoSlidingExpiration);
            }
    
            /// <summary>
            /// Remove item from cache
            /// </summary>
            /// <param name="key">Name of cached item</param>
            public static void Clear(string key)
            {
                HttpContext.Current.Cache.Remove(key);
            }
    
            /// <summary>
            /// Check for item in cache
            /// </summary>
            /// <param name="key">Name of cached item</param>
            /// <returns></returns>
            public static bool Exists(string key)
            {
                return HttpContext.Current.Cache[key] != null;
            }
    
            /// <summary>
            /// Retrieve cached item
            /// </summary>
            /// <typeparam name="T">Type of cached item</typeparam>
            /// <param name="key">Name of cached item</param>
            /// <param name="value">Cached value. Default(T) if 
            /// item doesn't exist.</param>
            /// <returns>Cached item as type</returns>
            public static bool Get<T>(string key, out T value)
            {
                try
                {
                    if (!Exists(key))
                    {
                        value = default(T);
                        return false;
                    } 
                    value = (T) HttpContext.Current.Cache[key];
                }
                catch
                {
                    value = default(T);
                    return false;
                }
                return true;
            }
    

    使用方法:

    public override List<Employee> GetEmployeeList()
    {
        string key = ConfigurationHelper.CacheKeyEmployeeList;
    
        List<Employee> employees = CacheHelper.Get<List<Employee>>(key);
    
        if (employees == null)
        {
            employees = instance.GetEmployeeList();
            CacheHelper.Add(employees, key);
        }
    
        return employees;
    }
  • 相关阅读:
    提升键盘可访问性和AT可访问性
    2个小技巧
    设计模式(6): 数据抽象与业务封装
    目录
    医疗经济学
    医疗场景下的行为经济学(三)
    抗体、免疫
    医疗场景下的行为经济学(二)
    医疗场景下的行为经济学(一)
    单身社会如何生活-日本纪录片ガイアの夜明け系列
  • 原文地址:https://www.cnblogs.com/objectboy/p/3716121.html
Copyright © 2011-2022 走看看