NetCore MemoryCache使用
程序员文章站
2023-12-29 00:00:28
引用类库 1.Install-Package Microsoft.Extensions.Caching.Memory MemoryCacheOptions 缓存配置 1.ExpirationScanFrequency 获取或设置对过期项的连续扫描之间的最短时间间隔 2.SizeLimit 缓存是没有 ......
引用类库
1.install-package microsoft.extensions.caching.memory
memorycacheoptions 缓存配置
1.expirationscanfrequency 获取或设置对过期项的连续扫描之间的最短时间间隔
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($"回调函数输出【键:{keyinfo},值:{valueinfo},被清除的原因:{reason}】"); }); _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($"回调函数输出【键:{keyinfo},值:{valueinfo},被清除的原因:{reason}】"); }); _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:
大神贴2: