引用类库
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:https://www.jb51.net/article/195870.htm
大神贴2:https://www.jb51.net/article/252078.htm
到此这篇关于.NetCore MemoryCache使用的文章就介绍到这了,更多相关.NetCore MemoryCache使用内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!