C# 在内存中缓存应用程序数据:MVC Web API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18937545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Caching application data in memory: MVC Web API
提问by vesuvious
I am writing an MVC webAPI that will be used to return values that will be bound to dropdown boxes or used as type-ahead textbox results on a website, and I want to cache values in memory so that I do not need to perform database requests every time the API is hit.
我正在编写一个 MVC webAPI,它将用于返回将绑定到下拉框或用作网站上的预先输入文本框结果的值,我想将值缓存在内存中,以便我不需要执行数据库请求每次 API 被命中时。
I am going to use the MemoryCache class and I know I can populate the cache when the first request comes in but I don't want the first request to the API to be slower than others. My question is: Is there a way for me to automatically populate the cache when the WebAPI first starts? I see there is an "App_Start" folder, maybe I just throw something in here?
我将使用 MemoryCache 类,我知道我可以在第一个请求进入时填充缓存,但我不希望对 API 的第一个请求比其他请求慢。我的问题是:有没有办法在 WebAPI 首次启动时自动填充缓存?我看到有一个“App_Start”文件夹,也许我只是在这里扔了一些东西?
After the initial population, I will probably run an hourly/daily request to update the cache as required.
在初始填充之后,我可能会每小时/每天运行一次请求以根据需要更新缓存。
MemoryCache: http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx
内存缓存:http: //msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx
UDPATE
UDPATE
Ela's answer below did the trick, basically I just needed to look at the abilities of Global.asax. Thanks for the quick help here, this has spun up a separate question for me about the pros/cons of different caching types.
下面 Ela 的回答解决了问题,基本上我只需要看看 Global.asax 的能力。感谢这里的快速帮助,这为我提出了一个关于不同缓存类型的优缺点的单独问题。
采纳答案by MichaC
You can use the global.asax appplication start method to initialize resources. Resources which will be used application wide basically.
您可以使用 global.asax 应用程序启动方法来初始化资源。基本上将在应用程序范围内使用的资源。
The following link should help you to find more information: http://www.asp.net/web-forms/tutorials/data-access/caching-data/caching-data-at-application-startup-cs
以下链接可以帮助您找到更多信息:http: //www.asp.net/web-forms/tutorials/data-access/caching-data/caching-data-at-application-startup-cs
Hint: If you use in process caching (which is usually the case if you cache something within the web context / thread), keep in mind that your web application is controlled by IIS. The standard IIS configuration will shut down your web application after 20 minutes if no user requests have to be served. This means, that any resources you have in memory, will be freed.
提示:如果您使用进程内缓存(通常是在 Web 上下文/线程中缓存某些内容时的情况),请记住您的 Web 应用程序由 IIS 控制。如果不需要处理用户请求,标准 IIS 配置将在 20 分钟后关闭您的 Web 应用程序。这意味着,您在内存中的任何资源都将被释放。
After this happens, the next time a user accesses your web application, the global asax, application start will be excecuted again, because IIS reinitializes your web application. If you want to prevent this behaviour, you either configure the application pool idle timeout to not time out after 20minutes. Or you use a different cache strategy (persistent cache, distributed cache...).
发生这种情况后,下次用户访问您的 Web 应用程序时,全局 asax,应用程序启动将再次执行,因为 IIS 会重新初始化您的 Web 应用程序。如果您想防止这种行为,您可以将应用程序池空闲超时配置为在 20 分钟后不超时。或者您使用不同的缓存策略(持久缓存、分布式缓存...)。
To configure IIS for this, here you can find more information: http://brad.kingsleyblog.com/IIS7-Application-Pool-Idle-Time-out-Settings/
要为此配置 IIS,您可以在此处找到更多信息:http: //brad.kingsleyblog.com/IIS7-Application-Pool-Idle-Time-out-Settings/