【问题标题】:Flushing MemoryCache ASP.NET Core 2刷新 MemoryCache ASP.NET Core 2
【发布时间】:2017-08-18 15:47:30
【问题描述】:

我在更新项目后尝试刷新缓存,我尝试了几个不同的选项,但没有一个按预期工作

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();
     }

我已尝试在这里 Dispose() MemoryCache 但在下一次 Api 调用时,它仍然被释放。由于钥匙有点动态,我不能只拿到钥匙。我该怎么做?

【问题讨论】:

  • 为什么叫它GetById而不是GetByUrl?
  • 不管怎样,你能重构Update方法中的GetById{url}部分吗?
  • 确定todo 项目具有Url 属性,您可以使用该属性重新将该项目重新插入缓存?或者甚至删除includeExcerpt true 和 false 变体?

标签: c# .net-core memorycache


【解决方案1】:

您可以改为存储字典。这样,您可以将动态键用于条目,将单个静态键用于字典容器,可以将其存储在缓存中,而不是单独存储每个条目。

类似的东西:

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]);
}

【讨论】:

  • 问题是密钥不是静态的。键是基于参数的变量
  • @IsaacLevin 我建议的方式允许对条目使用动态键,对字典容器使用一个静态键,可以将其存储在缓存中,而不是一一存储。你明白了吗?
  • 既然你这样说,我就做到了:) 我会看看
  • 我在答案中重新表述了这个想法。如果这样做了,请随时通知我们。
  • 冲洗会是什么样子?或者我只是在更新中重新初始化?
猜你喜欢
  • 1970-01-01
  • 2017-09-26
  • 2019-05-06
  • 1970-01-01
  • 2019-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-03-30
  • 1970-01-01
相关资源
最近更新 更多