Asp.net Core 缓存 MemoryCache(服务器缓存)

原文链接:https://www.cnblogs.com/yuangang/p/5800113.html

https://blog.csdn.net/qq_42606051/article/details/81699503

官网资料:https://docs.microsoft.com/zh-cn/aspnet/core/performance/caching/memory?view=aspnetcore-2.1

 

缓存接口 ICacheService

 

   缓存也好,数据库也好,我们就是进行CRUD操作,接口没什么好解释的,注释我写的很明白,这里就列给大家:

 

  #  验证缓存项是否存在

 

复制代码

 1         /// <summary>
 2         /// 验证缓存项是否存在
 3         /// </summary>
 4         /// <param name="key">缓存Key</param>
 5         /// <returns></returns>
 6         bool Exists(string key);
 7 
 8         /// <summary>
 9         /// 验证缓存项是否存在(异步方式)
10         /// </summary>
11         /// <param name="key">缓存Key</param>
12         /// <returns></returns>
13         Task<bool> ExistsAsync(string key);  

复制代码

 

  # 添加缓存

 

复制代码

 1  /// <summary>
 2         /// 添加缓存
 3         /// </summary>
 4         /// <param name="key">缓存Key</param>
 5         /// <param name="value">缓存Value</param>
 6         /// <returns></returns>
 7         bool Add(string key, object value);
 8 
 9         /// <summary>
10         /// 添加缓存(异步方式)
11         /// </summary>
12         /// <param name="key">缓存Key</param>
13         /// <param name="value">缓存Value</param>
14         /// <returns></returns>
15         Task<bool> AddAsync(string key, object value);
16 
17         /// <summary>
18         /// 添加缓存
19         /// </summary>
20         /// <param name="key">缓存Key</param>
21         /// <param name="value">缓存Value</param>
22         /// <param name="expiresSliding">滑动过期时长(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
23         /// <param name="expiressAbsoulte">绝对过期时长</param>
24         /// <returns></returns>
25         bool Add(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte);
26 
27         /// <summary>
28         /// 添加缓存(异步方式)
29         /// </summary>
30         /// <param name="key">缓存Key</param>
31         /// <param name="value">缓存Value</param>
32         /// <param name="expiresSliding">滑动过期时长(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
33         /// <param name="expiressAbsoulte">绝对过期时长</param>
34         /// <returns></returns>
35         Task<bool> AddAsync(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte);
36 
37         /// <summary>
38         /// 添加缓存
39         /// </summary>
40         /// <param name="key">缓存Key</param>
41         /// <param name="value">缓存Value</param>
42         /// <param name="expiresIn">缓存时长</param>
43         /// <param name="isSliding">是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
44         /// <returns></returns>
45         bool Add(string key, object value, TimeSpan expiresIn, bool isSliding = false);
46 
47         /// <summary>
48         /// 添加缓存(异步方式)
49         /// </summary>
50         /// <param name="key">缓存Key</param>
51         /// <param name="value">缓存Value</param>
52         /// <param name="expiresIn">缓存时长</param>
53         /// <param name="isSliding">是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
54         /// <returns></returns>
55         Task<bool> AddAsync(string key, object value, TimeSpan expiresIn, bool isSliding = false);

复制代码

 

   # 删除缓存

 

复制代码

 1  /// <summary>
 2         /// 删除缓存
 3         /// </summary>
 4         /// <param name="key">缓存Key</param>
 5         /// <returns></returns>
 6         bool Remove(string key);
 7 
 8         /// <summary>
 9         /// 删除缓存(异步方式)
10         /// </summary>
11         /// <param name="key">缓存Key</param>
12         /// <returns></returns>
13         Task<bool> RemoveAsync(string key);
14 
15         /// <summary>
16         /// 批量删除缓存
17         /// </summary>
18         /// <param name="key">缓存Key集合</param>
19         /// <returns></returns>
20         void RemoveAll(IEnumerable<string> keys);
21 
22         /// <summary>
23         /// 批量删除缓存(异步方式)
24         /// </summary>
25         /// <param name="key">缓存Key集合</param>
26         /// <returns></returns>
27         Task RemoveAllAsync(IEnumerable<string> keys);

复制代码

 

     # 获取缓存

 

复制代码

 1 /// <summary>
 2         /// 获取缓存
 3         /// </summary>
 4         /// <param name="key">缓存Key</param>
 5         /// <returns></returns>
 6         T Get<T>(string key) where T : class;
 7 
 8         /// <summary>
 9         /// 获取缓存(异步方式)
10         /// </summary>
11         /// <param name="key">缓存Key</param>
12         /// <returns></returns>
13         Task<T> GetAsync<T>(string key) where T : class;
14 
15         /// <summary>
16         /// 获取缓存
17         /// </summary>
18         /// <param name="key">缓存Key</param>
19         /// <returns></returns>
20         object Get(string key);
21 
22         /// <summary>
23         /// 获取缓存(异步方式)
24         /// </summary>
25         /// <param name="key">缓存Key</param>
26         /// <returns></returns>
27         Task<object> GetAsync(string key);
28 
29         /// <summary>
30         /// 获取缓存集合
31         /// </summary>
32         /// <param name="keys">缓存Key集合</param>
33         /// <returns></returns>
34         IDictionary<string, object> GetAll(IEnumerable<string> keys);
35 
36         /// <summary>
37         /// 获取缓存集合(异步方式)
38         /// </summary>
39         /// <param name="keys">缓存Key集合</param>
40         /// <returns></returns>
41         Task<IDictionary<string, object>> GetAllAsync(IEnumerable<string> keys);

复制代码

 

  # 修改缓存 

 

复制代码

 1 /// <summary>
 2         /// 修改缓存
 3         /// </summary>
 4         /// <param name="key">缓存Key</param>
 5         /// <param name="value">新的缓存Value</param>
 6         /// <returns></returns>
 7         bool Replace(string key, object value);
 8 
 9         /// <summary>
10         /// 修改缓存(异步方式)
11         /// </summary>
12         /// <param name="key">缓存Key</param>
13         /// <param name="value">新的缓存Value</param>
14         /// <returns></returns>
15         Task<bool> ReplaceAsync(string key, object value);
16 
17         /// <summary>
18         /// 修改缓存
19         /// </summary>
20         /// <param name="key">缓存Key</param>
21         /// <param name="value">新的缓存Value</param>
22         /// <param name="expiresSliding">滑动过期时长(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
23         /// <param name="expiressAbsoulte">绝对过期时长</param>
24         /// <returns></returns>
25         bool Replace(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte);
26 
27         /// <summary>
28         /// 修改缓存(异步方式)
29         /// </summary>
30         /// <param name="key">缓存Key</param>
31         /// <param name="value">新的缓存Value</param>
32         /// <param name="expiresSliding">滑动过期时长(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
33         /// <param name="expiressAbsoulte">绝对过期时长</param>
34         /// <returns></returns>
35         Task<bool> ReplaceAsync(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte);
36 
37         /// <summary>
38         /// 修改缓存
39         /// </summary>
40         /// <param name="key">缓存Key</param>
41         /// <param name="value">新的缓存Value</param>
42         /// <param name="expiresIn">缓存时长</param>
43         /// <param name="isSliding">是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
44         /// <returns></returns>
45         bool Replace(string key, object value, TimeSpan expiresIn, bool isSliding = false);
46 
47         /// <summary>
48         /// 修改缓存(异步方式)
49         /// </summary>
50         /// <param name="key">缓存Key</param>
51         /// <param name="value">新的缓存Value</param>
52         /// <param name="expiresIn">缓存时长</param>
53         /// <param name="isSliding">是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
54         /// <returns></returns>
55         Task<bool> ReplaceAsync(string key, object value, TimeSpan expiresIn, bool isSliding = false);

复制代码

 

 

缓存实现类 MemoryCache

 

  MemoryCache 这个比较简单,有微软比较完善的文档和一些比较好的文章,这个大家都用好久了。

  我们新建一个缓存实现类 MemoryCacheService 实现接口 ICacheService

  public class MemoryCacheService :ICacheService { }

  通过构造器注入 IMemoryCache:

  protected IMemoryCache _cache;

  public MemoryCacheService(IMemoryCache cache)
  {
    _cache = cache;
  }

  

  实现接口的方法,我们的方法都带有 异步 和 同步 两种,我们节约篇章和时间,异步的就不大家写了,大家自己添加一下就好。

 

  #  验证缓存项是否存在

  在MemoryCache中,我们是通过 TryGetValue 来检测 Key是否存在的

  

复制代码

 1 /// <summary>
 2         /// 验证缓存项是否存在
 3         /// </summary>
 4         /// <param name="key">缓存Key</param>
 5         /// <returns></returns>
 6         public bool Exists(string key)
 7         {
 8             if (key == null)
 9             {
10                 throw new ArgumentNullException(nameof(key));
11             }
12             object cached;
13             return _cache.TryGetValue(key,out cached);
14         }

复制代码

 

  # 添加缓存

  三个添加方法 :

  

复制代码

 1 /// <summary>
 2         /// 添加缓存
 3         /// </summary>
 4         /// <param name="key">缓存Key</param>
 5         /// <param name="value">缓存Value</param>
 6         /// <returns></returns>
 7         public bool Add(string key, object value)
 8         {
 9             if (key == null)
10             {
11                 throw new ArgumentNullException(nameof(key));
12             }
13             if (value == null)
14             {
15                 throw new ArgumentNullException(nameof(value));
16             }
17             _cache.Set(key, value);
18             return Exists(key);
19         }
20 /// <summary>
21         /// 添加缓存
22         /// </summary>
23         /// <param name="key">缓存Key</param>
24         /// <param name="value">缓存Value</param>
25         /// <param name="expiresSliding">滑动过期时长(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
26         /// <param name="expiressAbsoulte">绝对过期时长</param>
27         /// <returns></returns>
28         public bool Add(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte)
29         {
30             if (key == null)
31             {
32                 throw new ArgumentNullException(nameof(key));
33             }
34             if (value == null)
35             {
36                 throw new ArgumentNullException(nameof(value));
37             }
38             _cache.Set(key, value,
39                     new MemoryCacheEntryOptions()
40                     .SetSlidingExpiration(expiresSliding)
41                     .SetAbsoluteExpiration(expiressAbsoulte)
42                     );
43 
44             return Exists(key);
45         }
46  /// <summary>
47         /// 添加缓存
48         /// </summary>
49         /// <param name="key">缓存Key</param>
50         /// <param name="value">缓存Value</param>
51         /// <param name="expiresIn">缓存时长</param>
52         /// <param name="isSliding">是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
53         /// <returns></returns>
54         public bool Add(string key, object value, TimeSpan expiresIn,bool isSliding = false)
55         {
56             if (key == null)
57             {
58                 throw new ArgumentNullException(nameof(key));
59             }
60             if (value == null)
61             {
62                 throw new ArgumentNullException(nameof(value));
63             }
64             if (isSliding)
65             _cache.Set(key, value,
66                 new MemoryCacheEntryOptions()
67                 .SetSlidingExpiration(expiresIn)
68                 );
69             else
70                 _cache.Set(key, value,
71                 new MemoryCacheEntryOptions()
72                 .SetAbsoluteExpiration(expiresIn)
73                 );
74 
75             return Exists(key);
76         }

复制代码

 

 

 

  # 删除缓存

  单个删除 和 批量删除

 

复制代码

 1 /// <summary>
 2         /// 删除缓存
 3         /// </summary>
 4         /// <param name="key">缓存Key</param>
 5         /// <returns></returns>
 6         public bool Remove(string key)
 7         {
 8             if (key == null)
 9             {
10                 throw new ArgumentNullException(nameof(key));
11             }
12             _cache.Remove(key);
13 
14             return !Exists(key);
15         }
16 /// <summary>
17         /// 批量删除缓存
18         /// </summary>
19         /// <param name="key">缓存Key集合</param>
20         /// <returns></returns>
21         public void RemoveAll(IEnumerable<string> keys)
22         {
23             if (keys == null)
24             {
25                 throw new ArgumentNullException(nameof(keys));
26             }
27 
28             keys.ToList().ForEach(item => _cache.Remove(item));
29         }

复制代码

 

  # 获取缓存

 

复制代码

 1 /// <summary>
 2         /// 获取缓存
 3         /// </summary>
 4         /// <param name="key">缓存Key</param>
 5         /// <returns></returns>
 6         public T Get<T>(string key) where T : class
 7         {
 8             if (key == null)
 9             {
10                 throw new ArgumentNullException(nameof(key));
11             }
12             return _cache.Get(key) as T;
13         }
14 /// <summary>
15         /// 获取缓存
16         /// </summary>
17         /// <param name="key">缓存Key</param>
18         /// <returns></returns>
19         public object Get(string key)
20         {
21             if (key == null)
22             {
23                 throw new ArgumentNullException(nameof(key));
24             }
25             return _cache.Get(key);
26         }
27 /// <summary>
28         /// 获取缓存集合
29         /// </summary>
30         /// <param name="keys">缓存Key集合</param>
31         /// <returns></returns>
32         public IDictionary<string, object> GetAll(IEnumerable<string> keys)
33         {
34             if (keys == null)
35             {
36                 throw new ArgumentNullException(nameof(keys));
37             }
38 
39             var dict = new Dictionary<string, object>();
40 
41             keys.ToList().ForEach(item => dict.Add(item, _cache.Get(item)));
42 
43             return dict;
44         }

复制代码

 

  # 修改缓存

复制代码

 1  /// <summary>
 2         /// 修改缓存
 3         /// </summary>
 4         /// <param name="key">缓存Key</param>
 5         /// <param name="value">新的缓存Value</param>
 6         /// <returns></returns>
 7         public bool Replace(string key, object value)
 8         {
 9             if (key == null)
10             {
11                 throw new ArgumentNullException(nameof(key));
12             }
13             if (value == null)
14             {
15                 throw new ArgumentNullException(nameof(value));
16             }
17             if (Exists(key))
18                 if (!Remove(key)) return false;
19 
20             return Add(key, value);
21 
22         }
23 /// <summary>
24         /// 修改缓存
25         /// </summary>
26         /// <param name="key">缓存Key</param>
27         /// <param name="value">新的缓存Value</param>
28         /// <param name="expiresSliding">滑动过期时长(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
29         /// <param name="expiressAbsoulte">绝对过期时长</param>
30         /// <returns></returns>
31         public bool Replace(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte)
32         {
33             if (key == null)
34             {
35                 throw new ArgumentNullException(nameof(key));
36             }
37             if (value == null)
38             {
39                 throw new ArgumentNullException(nameof(value));
40             }
41             if (Exists(key))
42                 if (!Remove(key)) return false;
43 
44             return Add(key, value, expiresSliding, expiressAbsoulte);
45         }
46 /// <summary>
47         /// 修改缓存
48         /// </summary>
49         /// <param name="key">缓存Key</param>
50         /// <param name="value">新的缓存Value</param>
51         /// <param name="expiresIn">缓存时长</param>
52         /// <param name="isSliding">是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
53         /// <returns></returns>
54         public bool Replace(string key, object value, TimeSpan expiresIn, bool isSliding = false)
55         {
56             if (key == null)
57             {
58                 throw new ArgumentNullException(nameof(key));
59             }
60             if (value == null)
61             {
62                 throw new ArgumentNullException(nameof(value));
63             }
64             if (Exists(key))
65                 if (!Remove(key)) return false;
66 
67             return Add(key, value, expiresIn, isSliding);
68         }

复制代码

 

  最后 释放:

  public void Dispose()
  {
    if (_cache != null)
      _cache.Dispose();
    GC.SuppressFinalize(this);
  }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值