C# 刷新内存缓存ASP.NET核心2
我试图在更新一个项目后刷新缓存,我尝试了几个不同的选项,但没有一个能按预期工作C# 刷新内存缓存ASP.NET核心2,c#,.net-core,memorycache,C#,.net Core,Memorycache,我试图在更新一个项目后刷新缓存,我尝试了几个不同的选项,但没有一个能按预期工作 public class PostApiController : Controller { private readonly IPostService _postService; private readonly IPostTagService _postTagService; private IMemoryCache _cache; private MemoryCacheEntryO
public class PostApiController : Controller
{
private readonly IPostService _postService;
private readonly IPostTagService _postTagService;
private IMemoryCache _cache;
private MemoryCacheEntryOptions cacheEntryOptions;
public PostApiController(IPostService postService, IPostTagService postTagService, IMemoryCache cache)
{
_postService = postService;
_postTagService = postTagService;
_cache = cache;
cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromDays(1));
}
[HttpGet("{url}", Name = "GetPost")]
public IActionResult GetById(string url, bool includeExcerpt)
{
Post cacheEntry;
if (!_cache.TryGetValue($"GetById{url}{includeExcerpt}", out cacheEntry))
{
cacheEntry = _postService.GetByUrl(url, includeExcerpt);
_cache.Set($"GetById{url}{includeExcerpt}", cacheEntry, cacheEntryOptions);
}
if (cacheEntry == null)
{
return NotFound();
}
return new ObjectResult(cacheEntry);
}
[HttpPut("{id}")]
public IActionResult Update(int id, [FromBody] Post item)
{
if (item == null)
{
return BadRequest();
}
var todo = _postService.GetById(id);
if (todo == null)
{
return NotFound();
}
_postService.Update(item);
_postTagService.Sync(item.Tags.Select(a => new PostTag { PostId = item.Id, TagId = a.Id }).ToList());
//Want to flush entire cache here
return new NoContentResult();
}
我已经尝试在这里释放()MemoryCache,但在下一次Api调用中,它仍然被释放。因为钥匙是动态的,我不能只拿钥匙。我该怎么做呢?你可以把字典存起来。通过这种方式,您可以为条目使用动态键,为字典容器使用单个静态键,它们可以存储在缓存中,而不是单独存储每个条目 诸如此类:
private const string CachedEntriesKey = "SOME-STATIC-KEY";
[HttpGet("{url}", Name = "GetPost")]
public IActionResult GetById(string url, bool includeExcerpt)
{
Dictionary<string, Post> cacheEntries;
if (!_cache.TryGetValue(CachedEntriesKey, out cacheEntries))
{
cacheEntries = new Dictionary<string, Post>();
_cache.Set(CachedEntriesKey, cacheEntries);
}
var entryKey = $"GetById{url}{includeExcerpt}";
if (!cacheEntries.ContainsKey(entryKey))
{
return NotFound(); // by the way, why you do that instead of adding to the cache?
}
return new ObjectResult(cacheEntries[entryKey]);
}
private const string CachedEntriesKey=“SOME-STATIC-KEY”;
[HttpGet(“{url}”,Name=“GetPost”)]
公共IActionResult GetById(字符串url,bool includeExcerpt)
{
词典条目;
if(!\u cache.TryGetValue(CachedEntriesKey,out cacheEntries))
{
cacheEntries=新字典();
_Set(CachedEntriesKey,cacheEntries);
}
var entryKey=$“GetById{url}{includeExcerpt}”;
if(!cacheEntries.ContainsKey(entryKey))
{
return NotFound();//顺便问一下,为什么这样做而不是添加到缓存中?
}
返回新的ObjectResult(cacheEntries[entryKey]);
}
为什么称它为GetById而不是GetByUrl?无论如何,您可以在更新方法中重建GetById{url}部分吗?当然todo
项有一个url
属性,您可以使用它将该项重新插入缓存中?或者甚至删除两个includeExcerpt
true和false变体?问题在于密钥不是静态的。键是基于中的变量params@IsaacLevin我提出的方法允许为条目使用动态键,为字典容器使用一个静态键,它可以存储在缓存中,而不是逐个条目。你明白了吗?我明白了,因为你是这样说的:)我来看看我在回答中重新表述了这个想法。如果真是这样的话,请随时通知我们。那潮水会是什么样子呢?或者我只是在更新中重新初始化?