简体   繁体   English

ASP.NET中的System.Web.Caching.Cache

[英]System.Web.Caching.Cache in ASP.NET

I just discovered System.Web.Caching.Cache used in a project that I am working on and I am having a hard time finding more information on it. 我刚刚发现在我正在处理的项目中使用了System.Web.Caching.Cache ,但我很难找到有关它的更多信息。

My question is how this cache is persisted? 我的问题是该缓存如何持久化? Is it client-side (similar to ViewState ), server-side ( Session )? 是客户端(类似于ViewState ),服务器端( Session )吗? Entirely different? 完全不同?

Example: 例:

protected string FileContent 
{ 
    get 
    { 
        return Cache[FILE_UPLOAD_KEY + Id] as string ?? GetFileUpload(); 
    } 
}

It's a server-side, application-wide cache. 它是服务器端的应用程序级缓存。

One instance of this class is created per application domain, and it remains valid as long as the application domain remains active. 此类的一个实例是在每个应用程序域中创建的,只要该应用程序域保持活动状态,它就一直有效。 Information about an instance of this class is available through the Cache property of the HttpContext object or the Cache property of the Page object. 可通过HttpContext对象的Cache属性或Page对象的Cache属性获得有关此类的实例的信息。 ( Cache Class, MSDN ) 缓存类,MSDN

It grants the ability to set time limits and so forth on cached objects. 它可以设置缓存对象的时间限制等。 And it doesn't promise the object will be there when you need it again. 并且它不保证当您再次需要该对象时该对象将在那里。 It keeps items in cache only so long as there is sufficient memory to do so. 只要有足够的内存,它将项目保留在缓存中。

So, it's not intended for passing objects between page views (use ViewState or Session for that) or controls (use Items for that). 因此,它不打算在页面视图(为此使用ViewStateSession )或控件(为此使用Items )之间传递对象。 It's intended to cache global objects (accessible in any request from all clients) that are expensive to build. 它旨在缓存构建成本很高的全局对象(可在所有客户端的任何请求中访问)。

It's persisted at the server, and it's global across sessions, like Application . 它一直保存在服务器上,并且在整个会话中都是全局的,例如Application So when you set a value in the Cache , it's available to all users until it expires. 因此,当您在Cache设置一个值时,所有用户都可以使用它,直到它过期。

EDIT 编辑

The example you've got probably isn't quite right (unless GetFileUpload() actually writes to the cache). 您得到的示例可能不太正确(除非GetFileUpload()实际上写入高速缓存)。 Generally your calls to cache look something like: 通常,您对缓存的调用类似于:

string GetSomeStringFromCache()
{
    string someString = Cache[SomeKey] as string;
    if (someString == null)
    {
        someString = GetStringUsingSomeExpensiveFunction();
        Cache.Add(SomeKey, someString, /*a bunch of other parameters*/);
    }
    return someString;
}

This will put it in the Cache if it's not already there, but if it is, it will just use it. 如果它尚不在缓存中,则将其放入缓存,但如果存在,它将仅使用它。

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

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