ASP.NET Core 中的缓存内存(MemoryCache)
ASP.NET Core 中的缓存内存
ASP.NET Core 中的分布式缓存(SQL Server 和 Redis 分布式缓存)
响应缓存在 ASP.NET Core 中的中间件
github 地址
IMemoryCache Interface
Memcached 教程 | 菜鸟教程
1、IMemoryCache 接口【方法】
CreateEntry:在缓存中创建或覆盖项。
Remove:删除与给定键关联的对象。
TryGetValue:获取与此键关联的项(如果存在)。
2、IMemoryCache 接口【扩展方法】
Get(IMemoryCache, Object)
Get<TItem>(IMemoryCache, Object)
GetOrCreate<TItem>(IMemoryCache, Object, Func<ICacheEntry,TItem>)
GetOrCreateAsync<TItem>(IMemoryCache, Object, Func<ICacheEntry,Task<TItem>>)
Set<TItem>(IMemoryCache, Object, TItem)
Set<TItem>(IMemoryCache, Object, TItem, MemoryCacheEntryOptions)
Set<TItem>(IMemoryCache, Object, TItem, IChangeToken)
Set<TItem>(IMemoryCache, Object, TItem, DateTimeOffset)
Set<TItem>(IMemoryCache, Object, TItem, TimeSpan)
TryGetValue<TItem>(IMemoryCache, Object, TItem)
3、MemoryCacheOptions 类
CompactionPercentage:获取或设置在超出最大大小时要压缩的缓存量。
ExpirationScanFrequency:获取或设置连续扫描过期项之间的最小时间长度。
SizeLimit:获取或设置缓存的最大大小。
4、属性
Key:缓存key
Value:缓存值
AbsoluteExpiration:绝对过期时间,为null则条件无效
AbsoluteExpirationRelativeToNow:相对当前时间的绝对过期时间(使用TimeSpan),为null条件无效
SlidingExpiration:滑动过期时间
ExpirationTokens:提供用来自定义缓存过期
PostEvictionCallbacks:缓存失效回调
Priority:缓存项优先级(在缓存满载的时候绝对清除的顺序)
Size:代表缓存数据的大小,在内存缓存中一般为null
SetSize:12
SizeLimit:缓存的大小
*
5、Memory
IMemoryCache Interface
方法
CreateEntry(Object):在缓存中创建或覆盖项。
Remove(Object):删除与给定键关联的对象。
TryGetValue(Object, Object):获取与此键关联的项(如果存在)。
扩展方法
Get(IMemoryCache, Object)
Get<TItem>(IMemoryCache, Object)
GetOrCreate<TItem>(IMemoryCache, Object, Func<ICacheEntry,TItem>)
GetOrCreateAsync<TItem>(IMemoryCache, Object, Func<ICacheEntry,Task<TItem>>)
Set<TItem>(IMemoryCache, Object, TItem)
Set<TItem>(IMemoryCache, Object, TItem, MemoryCacheEntryOptions)
Set<TItem>(IMemoryCache, Object, TItem, IChangeToken)
Set<TItem>(IMemoryCache, Object, TItem, DateTimeOffset):创建或覆盖缓存中的指定条目,并设置具有绝对过期日期的值。
Set<TItem>(IMemoryCache, Object, TItem, TimeSpan)
TryGetValue<TItem>(IMemoryCache, Object, TItem)
*
MemoryCacheOptions
MemoryCacheOptions.SizeLimit 属性:获取或设置缓存的最大大小。
*
MemoryCacheEntryOptions
MemoryCacheEntryOptions.Size 属性:获取或设置缓存项值的大小。
*
MemoryCacheEntryExtensions
MemoryCacheEntryExtensions.SetSize 方法:设置缓存项值的大小。
*
5、注意
注意:静态构造函数只会实例化一次;【参考MemoryCacheHelper.cs】
注意:在【泛型类】中,不同的类型,会单独维护自己的类,比如MemoryCacheHelper<string>已经走了静态构造函数,MemoryCacheHelper<int>会再次走构造函数;【参考MemoryCacheHelper.cs】
6、Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
}
7、Controller.cs
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
namespace Asset.WebAPI.Controllers
{
[ApiVersion("1.0")]
[ApiVersion("0.1")]
[Route("v{version:apiVersion}/values")]
public class ValuesController : ControllerBase
{
private readonly IMemoryCache _memoryCache;
public ValuesController(IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
}
[HttpGet("set")]
public IActionResult Set()
{
_memoryCache.Set<string>("timestamp", DateTime.Now.ToString());
_memoryCache.Set<string>("timestamp", DateTime.Now.ToString(), new MemoryCacheEntryOptions
{
AbsoluteExpiration = DateTime.Now.AddMinutes(1),
SlidingExpiration = TimeSpan.FromMinutes(1)
});
string timestamp = _memoryCache.GetOrCreate<string>("timestamp", entry =>
{
return DateTime.Now.ToString();
});
return Ok();
}
[HttpGet("get")]
public IActionResult Get()
{
string timestamp = _memoryCache.Get<string>("timestamp");
string timestampNew = string.Empty;
if (!_memoryCache.TryGetValue<string>("timestamp", out timestampNew))
_memoryCache.Set<string>("timestamp", DateTime.Now.ToString());
return Ok();
}
[HttpGet("get_async")]
public async Task<IActionResult> GetAsync()
{
var cacheEntry = await _memoryCache.GetOrCreateAsync("timestamp", entry =>
{
entry.SlidingExpiration = TimeSpan.FromSeconds(3);
return Task.FromResult(DateTime.Now);
});
return Ok(new { code = 200, data = cacheEntry });
}
[HttpGet("remove")]
public IActionResult Remove()
{
_memoryCache.Remove("timestamp");
return Ok();
}
}
}
8、MemoryCacheHelper.cs 代码如下
public class MemoryCacheHelper<T>
{
static MemoryCacheHelper()
{ }
}
*
*
*
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)