缓存大型响应在Web API 2中

huangapple go评论81阅读模式
英文:

Cache huge response in web api 2

问题

  1. 如果我在服务器端缓存这个数据,我猜所有用户将会得到缓存的答案,直到缓存被释放?

  2. 我是否可以在服务器上缓存这个响应,直到我以某种方式告诉API停止缓存它?保持24小时缓存是可以的。

  3. 是否有任何库可以实现这个目标?

祝好。

英文:

We have this 90.000 lines Json object that takes 5 seconds to load. We need to load all this data since there is a lot of relations that needs to be included. Fortunately this data almost never change so it will basically always be the same (it do need to be able to snap out of the cache if the data updates)

Im not that good on server side cache so my question is.

  1. If i cache this serverside i guess all users will be served the cached answer until the cache releases?

  2. Can i cache this response on the server for until i somehow tell the api stop caching this? Its fine to hold 24 hours cache

  3. Any libriaries that i can achieve this with?

Regards

答案1

得分: 3

你可以在 .Net Framework 中使用 MemoryCache

  1. MemoryCache 存在于进程中,因此如果需要的话,所有用户都可以访问相同的缓存数据(在同一台服务器上)。
  2. 当保存到缓存时,您可以设置过期时间。
  3. 它是Framework内置的。
英文:

You could use MemoryCache in .Net Framework

  1. MemoryCache lives in the Process, so all users can access the same cached data if you want (on the same server)
  2. You set the expiration time when saving to the cache.
  3. Built in the Framework

答案2

得分: 0

感谢您的输入,我的第一次尝试看起来是这样的。似乎工作正常。请提供反馈

MemoryCache memoryCache = MemoryCache.Default;
var test = memoryCache.Get("testKey")

if (test != null) {
    return Ok(test) 
} else {
    var data = GetData()
         .ToList();

    memoryCache.Add("testKey", data, DateTimeOffset.UtcNow.AddDays(1));
    return Ok(data)
}

如果您有任何问题,请随时提出。

英文:

Thanks for the input my first attempt looks like this. It seems to work. Please provide feedback

        MemoryCache memoryCache = MemoryCache.Default;
        var test = memoryCache.Get("testKey")

        if (test != null) {
            return Ok(test) 
        } else {
            var data = GetData()
                 .ToList();

            memoryCache.Add("testKey", data, DateTimeOffset.UtcNow.AddDays(1));
            return Ok(data)
        }

huangapple
  • 本文由 发表于 2020年1月3日 15:22:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定