如何深入理解并高效运用.NetCore中的MemoryCache功能?

2026-03-30 11:021阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计751个文字,预计阅读时间需要4分钟。

如何深入理解并高效运用.NetCore中的MemoryCache功能?

目录- 引用类库- MemoryCacheOptions 配置- 缓存配置- 单个缓存项配置- 完整代码- 引用类库

1. Install-Package Microsoft.Extensions.Caching.Memory

2.MemoryCacheOptions 配置

3.ExpirationScanFrequency 获取或设置

目录
  • 引用类库
  • MemoryCacheOptions 缓存配置
  • MemoryCacheEntryOptions 单个缓存项配置
    • 完整代码

引用类库

1.Install-Package Microsoft.Extensions.Caching.Memory

MemoryCacheOptions 缓存配置

1.ExpirationScanFrequency获取或设置对过期项的连续扫描之间的最短时间间隔

如何深入理解并高效运用.NetCore中的MemoryCache功能?

2.SizeLimit 缓存是没有大小的的,此值设置缓存的份数

3.CompactionPercentage获取或设置在超过最大大小时压缩缓存的数量,优先压缩优先级较低的缓存,0.2代表20%

services.AddMemoryCache(options => { // 缓存最大为100份 //##注意netcore中的缓存是没有单位的,缓存项和缓存的相对关系 options.SizeLimit = 2; //缓存满了时候压缩20%的优先级较低的数据 options.CompactionPercentage = 0.2; //两秒钟查找一次过期项 options.ExpirationScanFrequency = TimeSpan.FromSeconds(2); });

MemoryCacheEntryOptions 单个缓存项配置

1.AbsoluteExpiration 绝对过期时间

2.AbsoluteExpirationRelativeToNow 相对于现在的绝对过期时间

3.SlidingExpiration 滑动过期时间,在时间段范围内 缓存被再次访问,过期时间将会被重置

4.Priority 优先级

5.Size 缓存份数

public bool Add(string key, object value, int ExpirtionTime = 20) { if (!string.IsNullOrEmpty(key)) { MemoryCacheEntryOptions cacheEntityOps = new MemoryCacheEntryOptions() { //滑动过期时间 20秒没有访问则清除 SlidingExpiration = TimeSpan.FromSeconds(ExpirtionTime), //设置份数 Size = 1, //优先级 Priority = CacheItemPriority.Low, }; //过期回掉 cacheEntityOps.RegisterPostEvictionCallback((keyInfo, valueInfo, reason, state) => { Console.WriteLine($"回调函数输出"); }); _cache.Set(key, value, cacheEntityOps); } return true; }

完整代码

1.接口

public interface ICacheService { /// <summary> /// 新增 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="ExpirtionTime"></param> /// <returns></returns> bool Add(string key, object value, int ExpirtionTime = 20); /// <summary> /// 获取 /// </summary> /// <param name="key"></param> /// <returns></returns> string GetValue(string key); /// <summary> /// 验证缓存项是否存在 /// </summary> /// <param name="key">缓存Key</param> /// <returns></returns> bool Exists(string key); /// <summary> /// 移除 /// </summary> /// <param name="key"></param> /// <returns></returns> bool Remove(string key); }

2. 实现ICacheService

/// <summary> /// 缓存接口实现 /// </summary> public class MemoryCacheService : ICacheService { protected IMemoryCache _cache; public MemoryCacheService(IMemoryCache cache) { _cache = cache; } public bool Add(string key, object value, int ExpirtionTime = 20) { if (!string.IsNullOrEmpty(key)) { MemoryCacheEntryOptions cacheEntityOps = new MemoryCacheEntryOptions() { //滑动过期时间 20秒没有访问则清除 SlidingExpiration = TimeSpan.FromSeconds(ExpirtionTime), //设置份数 Size = 1, //优先级 Priority = CacheItemPriority.Low, }; //过期回掉 cacheEntityOps.RegisterPostEvictionCallback((keyInfo, valueInfo, reason, state) => { Console.WriteLine($"回调函数输出"); }); _cache.Set(key, value, cacheEntityOps); } return true; } public bool Remove(string key) { if (string.IsNullOrEmpty(key)) { return false; } if (Exists(key)) { _cache.Remove(key); return true; } return false; } public string GetValue(string key) { if (string.IsNullOrEmpty(key)) { return null; } if (Exists(key)) { return _cache.Get(key).ToString(); } return null; } public bool Exists(string key) { if (string.IsNullOrEmpty(key)) { return false; } object cache; return _cache.TryGetValue(key, out cache); } }

大神贴1:www.jb51.net/article/195870.htm

大神贴2:www.jb51.net/article/252078.htm

到此这篇关于.NetCore MemoryCache使用的文章就介绍到这了,更多相关.NetCore MemoryCache使用内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计751个文字,预计阅读时间需要4分钟。

如何深入理解并高效运用.NetCore中的MemoryCache功能?

目录- 引用类库- MemoryCacheOptions 配置- 缓存配置- 单个缓存项配置- 完整代码- 引用类库

1. Install-Package Microsoft.Extensions.Caching.Memory

2.MemoryCacheOptions 配置

3.ExpirationScanFrequency 获取或设置

目录
  • 引用类库
  • MemoryCacheOptions 缓存配置
  • MemoryCacheEntryOptions 单个缓存项配置
    • 完整代码

引用类库

1.Install-Package Microsoft.Extensions.Caching.Memory

MemoryCacheOptions 缓存配置

1.ExpirationScanFrequency获取或设置对过期项的连续扫描之间的最短时间间隔

如何深入理解并高效运用.NetCore中的MemoryCache功能?

2.SizeLimit 缓存是没有大小的的,此值设置缓存的份数

3.CompactionPercentage获取或设置在超过最大大小时压缩缓存的数量,优先压缩优先级较低的缓存,0.2代表20%

services.AddMemoryCache(options => { // 缓存最大为100份 //##注意netcore中的缓存是没有单位的,缓存项和缓存的相对关系 options.SizeLimit = 2; //缓存满了时候压缩20%的优先级较低的数据 options.CompactionPercentage = 0.2; //两秒钟查找一次过期项 options.ExpirationScanFrequency = TimeSpan.FromSeconds(2); });

MemoryCacheEntryOptions 单个缓存项配置

1.AbsoluteExpiration 绝对过期时间

2.AbsoluteExpirationRelativeToNow 相对于现在的绝对过期时间

3.SlidingExpiration 滑动过期时间,在时间段范围内 缓存被再次访问,过期时间将会被重置

4.Priority 优先级

5.Size 缓存份数

public bool Add(string key, object value, int ExpirtionTime = 20) { if (!string.IsNullOrEmpty(key)) { MemoryCacheEntryOptions cacheEntityOps = new MemoryCacheEntryOptions() { //滑动过期时间 20秒没有访问则清除 SlidingExpiration = TimeSpan.FromSeconds(ExpirtionTime), //设置份数 Size = 1, //优先级 Priority = CacheItemPriority.Low, }; //过期回掉 cacheEntityOps.RegisterPostEvictionCallback((keyInfo, valueInfo, reason, state) => { Console.WriteLine($"回调函数输出"); }); _cache.Set(key, value, cacheEntityOps); } return true; }

完整代码

1.接口

public interface ICacheService { /// <summary> /// 新增 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="ExpirtionTime"></param> /// <returns></returns> bool Add(string key, object value, int ExpirtionTime = 20); /// <summary> /// 获取 /// </summary> /// <param name="key"></param> /// <returns></returns> string GetValue(string key); /// <summary> /// 验证缓存项是否存在 /// </summary> /// <param name="key">缓存Key</param> /// <returns></returns> bool Exists(string key); /// <summary> /// 移除 /// </summary> /// <param name="key"></param> /// <returns></returns> bool Remove(string key); }

2. 实现ICacheService

/// <summary> /// 缓存接口实现 /// </summary> public class MemoryCacheService : ICacheService { protected IMemoryCache _cache; public MemoryCacheService(IMemoryCache cache) { _cache = cache; } public bool Add(string key, object value, int ExpirtionTime = 20) { if (!string.IsNullOrEmpty(key)) { MemoryCacheEntryOptions cacheEntityOps = new MemoryCacheEntryOptions() { //滑动过期时间 20秒没有访问则清除 SlidingExpiration = TimeSpan.FromSeconds(ExpirtionTime), //设置份数 Size = 1, //优先级 Priority = CacheItemPriority.Low, }; //过期回掉 cacheEntityOps.RegisterPostEvictionCallback((keyInfo, valueInfo, reason, state) => { Console.WriteLine($"回调函数输出"); }); _cache.Set(key, value, cacheEntityOps); } return true; } public bool Remove(string key) { if (string.IsNullOrEmpty(key)) { return false; } if (Exists(key)) { _cache.Remove(key); return true; } return false; } public string GetValue(string key) { if (string.IsNullOrEmpty(key)) { return null; } if (Exists(key)) { return _cache.Get(key).ToString(); } return null; } public bool Exists(string key) { if (string.IsNullOrEmpty(key)) { return false; } object cache; return _cache.TryGetValue(key, out cache); } }

大神贴1:www.jb51.net/article/195870.htm

大神贴2:www.jb51.net/article/252078.htm

到此这篇关于.NetCore MemoryCache使用的文章就介绍到这了,更多相关.NetCore MemoryCache使用内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!