zoukankan      html  css  js  c++  java
  • 封装MemoryCache

    一、定义一个缓存接口IChace

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace 缓存
     7 {
     8     public interface ICache
     9     {
    10         T Get<T>(string key);
    11         void Add(string key,object data,int cacheTime=30);
    12         bool Contains(string keys);
    13         void Remove(string key);
    14         void RemoveAll();
    15         object this[string key] { get;set; }
    16         int Count { get; }
    17     }
    18 }

    二、定义一个MyCache类用来封装MemoryCache的基本业务,实现缓存接口IChace

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Runtime.Caching;
     5 using System.Text;
     6 
     7 namespace 缓存
     8 {
     9     class MyCache : ICache
    10     {
    11         /// <summary>
    12         /// 索引器,提供一个全局读写
    13         /// </summary>
    14         /// <param name="key"></param>
    15         /// <returns></returns>
    16         public object this[string key] { get => Cache.Get(key); set => Add(key,value); }
    17 
    18         protected MemoryCache Cache
    19         {
    20             get
    21             {
    22                 return MemoryCache.Default;
    23             }
    24         }
    25            
    26 
    27         public int Count =>(int)Cache.GetCount() ;
    28 
    29         /// <summary>
    30         /// 添加缓存
    31         /// </summary>
    32         /// <param name="key"></param>
    33         /// <param name="data"></param>
    34         /// <param name="cacheTime">分钟</param>
    35         public void Add(string key, object data, int cacheTime = 30)
    36         {
    37             if (Cache.Contains(key))
    38             {
    39                 this.Remove(key);
    40             }
    41             var policy = new CacheItemPolicy();
    42             policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);
    43             Cache.Add(new CacheItem(key, data), policy);
    44         }
    45 
    46         /// <summary>
    47         /// 判断cache是否存在
    48         /// </summary>
    49         /// <param name="key"></param>
    50         /// <returns></returns>
    51         public bool Contains(string key)
    52         {
    53             return Cache.Contains(key);
    54         }
    55 
    56         /// <summary>
    57         /// 读取缓存
    58         /// </summary>
    59         /// <typeparam name="T"></typeparam>
    60         /// <param name="key"></param>
    61         /// <returns></returns>
    62         public T Get<T>(string key)
    63         {
    64             if (Cache.Contains(key))
    65             {
    66                 return (T)Cache[key];
    67             }
    68             else
    69             {
    70                 return default(T);
    71             }
    72         }
    73 
    74         /// <summary>
    75         /// 删除缓存
    76         /// </summary>
    77         /// <param name="key"></param>
    78         public void Remove(string key)
    79         {
    80             Cache.Remove(key);
    81         }
    82 
    83         /// <summary>
    84         /// 删除所有缓存
    85         /// </summary>
    86         public void RemoveAll()
    87         {
    88             foreach (var item in Cache)
    89             {
    90                this.Remove(item.Key);
    91             }
    92         }
    93     }
    94 }


    三、定义一个CacheManager类来管理不同的缓存对象,上端主程序直接调用该类进行业务缓存

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace 缓存
     7 {
     8     public class CacheManager
     9     {
    10         private CacheManager()
    11         {
    12 
    13         }
    14         private static ICache _cache = null;
    15         static CacheManager()
    16         {
    17             _cache = Activator.CreateInstance(typeof(MyCache)) as ICache;
    18         }
    19 
    20         /// <summary>
    21         /// 获取缓存数量
    22         /// </summary>
    23         public static int Count => _cache.Count;
    24 
    25         /// <summary>
    26         /// 判断缓存项是否存在
    27         /// </summary>
    28         /// <param name="key">缓存项</param>
    29         /// <returns></returns>
    30         public static bool Conatins(string key)
    31         {
    32             return _cache.Contains(key);
    33         }
    34 
    35         /// <summary>
    36         /// 判断是否存在缓存,存在返回结果,不存在返回T类型的默认值
    37         /// </summary>
    38         /// <typeparam name="T"></typeparam>
    39         /// <param name="key"></param>
    40         /// <returns></returns>
    41         public static T Get<T>(string key)
    42         {
    43             if(Conatins(key))
    44             {
    45                 return _cache.Get<T>(key);
    46             }
    47             else
    48             {
    49                 return default(T);
    50             }
    51         }
    52 
    53         /// <summary>
    54         /// 判断是否存在缓存,如果没有则先增加再获取
    55         /// </summary>
    56         /// <typeparam name="T"></typeparam>
    57         /// <param name="key">缓存项</param>
    58         /// <param name="func">没有缓存时所增加的缓存项</param>
    59         /// <param name="cacheTime">默认过期时间</param>
    60         /// <returns></returns>
    61         public static T Get<T>(string key,Func<T> func,int cacheTime=30)
    62         {
    63             if (!_cache.Contains(key))
    64             {
    65                 _cache.Add(key, func, cacheTime);
    66             }
    67             return _cache.Get<T>(key);
    68         }
    69 
    70         /// <summary>
    71         /// 删除缓存数据项
    72         /// </summary>
    73         /// <param name="key">缓存项</param>
    74         public static void Remove(string key)
    75         {
    76             _cache.Remove(key);
    77         }
    78 
    79         /// <summary>
    80         /// 删除所有缓存数据项
    81         /// </summary>
    82         public static void RemoveAll()
    83         {
    84             _cache.RemoveAll();
    85         }
    86     }
    87 }
  • 相关阅读:
    TD在IE7不能浏览的问题解决办法
    SqlServer中的IsNull
    C#日期加减
    this._form为空或不是对象
    清除自动保存的远程机器登录密码
    解决下载文件名乱码问题的简单方法
    createTextRange选中测试表格
    GridView改变行的颜色(二)
    行列转换(1)
    .NET中调用系统程序
  • 原文地址:https://www.cnblogs.com/AlexOneBlogs/p/7421135.html
Copyright © 2011-2022 走看看