在内存中缓存应用程序数据:MVC的Web API [英] Caching application data in memory: MVC Web API

查看:1165
本文介绍了在内存中缓存应用程序数据:MVC的Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个MVC的WebAPI将被用于返回将被绑定到下拉框,或用作网站上的预输入文本框结果值,我想缓存值,在内存中,这样我就不需要执行数据库请求在每次API被击中的时间。

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.

我要使用MemoryCache类,我知道当第一个请求到达,但我不希望在API的第一个请求会比别人慢,我可以填充缓存。我的问题是:有没有办法让我自动填充缓存第一次启动时的WebAPI?我看到有一个App_Start文件夹,也许我只是扔东西会在这里?

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?

初​​始群体之后,我可能会运行一个小时/每天的请求来更新缓存必需的。

After the initial population, I will probably run an hourly/daily request to update the cache as required.

的MemoryCache:
<一href=\"http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx\">http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx

MemoryCache: http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx

UDPATE

UDPATE

下面埃拉的答案的伎俩,基本上我只需要看看Global.asax中的能力。
感谢您的快速帮助在这里,这已经脱离了一个单独的问题,我对不同的缓存类型的优点/缺点。

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.

<一个href=\"http://stackoverflow.com/questions/18937855/pros-cons-of-different-asp-net-caching-options\">Pros/Cons不同的ASP.NET缓存选项

推荐答案

您可以使用在Global.asax参加办法start方法来初始化资源。
将用于应用程序的资源基本上宽

You can use the global.asax appplication start method to initialize resources. Resources which will be used application wide basically.

下面这个链接可以帮助你找到更多的信息:
<一href=\"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

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

提示:
如果在过程缓存(通常情况下,如果您缓存Web上下文/线程中的东西)的使用,保持你的web应用程序是由IIS控制头脑。
如果没有用户的请求必须提供标准的IIS配置将关闭20分钟后你的Web应用程序。
这意味着,任何资源,你的内存有,将被释放。

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应用,全球ASAX接下来的时间,应用程序启动将再次excecuted,因为IIS将重新初始化您的Web应用程序。
如果您想prevent这种行为,你要么配置应用程序池空闲超时20分钟后不超时。或者你使用不同的缓存策略(持续高速缓存,分布式缓存...)。

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...).

要为这个配置IIS,在这里你可以找到更多的信息:
<一href=\"http://brad.kingsleyblog.com/IIS7-Application-Pool-Idle-Time-out-Settings/\">http://brad.kingsleyblog.com/IIS7-Application-Pool-Idle-Time-out-Settings/

To configure IIS for this, here you can find more information: http://brad.kingsleyblog.com/IIS7-Application-Pool-Idle-Time-out-Settings/

这篇关于在内存中缓存应用程序数据:MVC的Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆