简体   繁体   English

ASP.NET MVC-在ram中存储数据

[英]ASP.NET MVC - storing data in ram

I have some data in .txt file that i would like to load into RAM memory during Application_Start(), so that i can use this data globally over my asp.net mvc application. 我在.txt文件中有一些数据,我希望在Application_Start()期间将其加载到RAM内存中,以便可以在asp.net mvc应用程序上全局使用此数据。 How can i do that? 我怎样才能做到这一点?

将文本存储在Session中,以便在用户关闭浏览器Session["MyText"] = txtfile.ToString();之后将其丢弃Session["MyText"] = txtfile.ToString();

If the data is by client, then use Session 如果数据是由客户端提供的,则使用Session

Session["value"] = valueForCurrentUser;

If the data is global and the same for every user, then use Cache. 如果数据是全局数据,并且每个用户的数据都相同,则使用缓存。

Cache.Add("value", valueForEveryUser, null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);

from: http://msdn.microsoft.com/en-us/library/system.web.caching.cache.add.aspx 来自: http : //msdn.microsoft.com/zh-cn/library/system.web.caching.cache.add.aspx

Thats is better than session I think, do you can use on all Application. 那比我认为的会话好,您可以在所有Application上使用。

    public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        HttpContext.Current.Application["PerfilLevel"] = "0";


        AreaRegistration.RegisterAllAreas();

On DAL 在DAL上

int teste = Convert.ToInt32(HttpContext.Current.Application["PerfilLevel"]);

On Controller 在控制器上

int teste = Convert.ToInt32(HttpContext.Application["PerfilLevel"]);

Note: Same value... 注意:相同的值...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM