zoukankan      html  css  js  c++  java
  • 缓存技术,封装好的缓存类

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5.   
    6. namespace Admin.Helper  
    7. {  
    8.     /// <summary>  
    9.     /// .net 本身自带的cache相关方法  
    10.     /// </summary>  
    11.     public static class CacheHelper  
    12.     {  
    13.         /// <summary>  
    14.         /// fileds  
    15.         /// </summary>  
    16.         private static System.Web.Caching.Cache ObjCache = System.Web.HttpRuntime.Cache;  
    17.  
    18.         #region Exist方法  
    19.         /// <summary>  
    20.         /// 判断指定Key的缓存是否存在  
    21.         /// </summary>  
    22.         /// <param name="Key"></param>  
    23.         /// <returns></returns>  
    24.         public static bool Exist(string Key)  
    25.         {  
    26.             if (ObjCache[Key] == null)  
    27.             {  
    28.                 return false;  
    29.             }  
    30.             return true;  
    31.         }  
    32.         #endregion  
    33.  
    34.         #region  Get方法  
    35.         /// <summary>  
    36.         /// 获得指定Key的缓存对象  
    37.         /// </summary>  
    38.         /// <param name="Key"></param>  
    39.         /// <returns></returns>  
    40.         public static Object Get(string Key)  
    41.         {  
    42.             object objkey = null;  
    43.             if (ObjCache[Key] != null)  
    44.             {  
    45.                 objkey = ObjCache.Get(Key);  
    46.             }  
    47.             return objkey;  
    48.         }  
    49.         #endregion  
    50.  
    51.         #region Set方法  
    52.   
    53.         /// <summary>  
    54.         /// 设置缓存  
    55.         /// </summary>  
    56.         /// <param name="Key">Cache Key</param>  
    57.         /// <param name="expiry">缓存时间</param>  
    58.         /// <param name="obj">缓存对象</param>  
    59.         public static void Set(string Key, DateTime expiry, object obj)  
    60.         {  
    61.             if (ObjCache[Key] != null)  
    62.             {  
    63.                 ObjCache.Remove(Key);  
    64.             }  
    65.   
    66.             ObjCache.Insert(Key, obj, null, expiry, TimeSpan.Zero);  
    67.         }  
    68.   
    69.         /// <summary>  
    70.         /// 设置缓存  
    71.         /// </summary>  
    72.         /// <param name="Key">Cache Key</param>  
    73.         /// <param name="min">缓存时间【分钟】</param>  
    74.         /// <param name="obj">缓存对象</param>  
    75.         public static void Set(string Key, int min, object obj)  
    76.         {  
    77.             double douNum = double.Parse(min.ToString());  
    78.             Set(Key, DateTime.Now.AddMinutes(douNum), obj);  
    79.         }  
    80.         #endregion  
    81.  
    82.         #region Del方法  
    83.   
    84.         /// <summary>  
    85.         /// 删除指定Key的缓存  
    86.         /// </summary>  
    87.         /// <param name="Key"></param>  
    88.         public static void Del(string Key)  
    89.         {  
    90.             if (ObjCache[Key] != null)  
    91.             {  
    92.                 ObjCache.Remove(Key);  
    93.             }  
    94.         }  
    95.         #endregion  
    96.  
    97.         #region 其他  
    98.   
    99.         /// <summary>  
    100.         /// 获取缓存中的项数  
    101.         /// </summary>  
    102.         public static int Count  
    103.         {  
    104.             get  
    105.             {  
    106.                 return ObjCache.Count;  
    107.             }  
    108.         }  
    109.   
    110.         /// <summary>  
    111.         /// 获取可用于缓存的千字节数  
    112.         /// </summary>  
    113.         public static long PrivateBytes  
    114.         {  
    115.             get  
    116.             {  
    117.                 return ObjCache.EffectivePrivateBytesLimit;  
    118.             }  
    119.         }  
    120.         #endregion  
    121.     }  
    122. }   
    1. 用方法如下:  
    2.   
    3. /// <summary>  
    4.         /// 获取产品详情  
    5.         /// </summary>  
    6.         /// <param name="proId"></param>  
    7.         /// <returns></returns>  
    8.         public static ProductDto GetProduct(string proId)  
    9.         {  
    10.             var key = "_ProductId" + proId;  
    11.             //通过key找到缓存的对象  
    12.             var cache = Helper.CacheHelper.Get(key);  
    13.             if (cache != null)  
    14.             {  
    15.                 return (ProductDto)cache;  
    16.             }  
    17.             var pro = ServiceLocator.Create<IProductService>().Get(proId);  
    18.             if (pro == null) return new ProductDto();  
    19.             Helper.CacheHelper.Set(key, DateTime.Now.AddHours(8), pro);  
    20.             return pro;  
    21.         } 
  • 相关阅读:
    2020软件工程第四次作业
    软件工程第三次作业
    软件工程第二次作业
    软件工程第一次作业
    #数据挖掘与数据化运营实战#2.3数据挖掘技术以及在数据化运营中的应用
    #简单统计学#单样本t检验
    #简单统计学#加权平均数
    软件工程(2019)结对编程第二次作业
    软件工程(2019)结对编程第一次作业
    软件工程(2019)第三次个人作业
  • 原文地址:https://www.cnblogs.com/liuguanghai/p/4702104.html
Copyright © 2011-2022 走看看