Page.Cache

https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page.cache?view=netframework-4.8

Gets the Cache object associated with the application in which the page resides.

 

An application's Cache object allows you to store and retrieve arbitrary data on subsequent requests. The cache is not specifically associated with a page or user session. It is used primarily to enhance application performance. For more information, see Caching Application Data. For more information on the difference between application caching and page output caching, see ASP.NET Caching Overview.

 

Page.Cache vs. HttpContext.Current.Cache

 

There is no difference, the former uses the current page instance and it's Cache property, the latter uses the static approach via HttpContext.Current.Cache which would work also in a static method without page instance.

Both are referring to the same application cache.

So you can get the Cache via Page, for example in Page_Load:

protected void Page_load(Object sender, EventArgs e)
{
    System.Web.Caching.Cache cache = this.Cache;
}

or in a static method (which is used in a HttpContext) via HttpContext.Current:

static void Foo()
{
    var context = HttpContext.Current;
    if (context != null)
    {
        System.Web.Caching.Cache cache = context.Cache;
    }
}

 

Difference between HttpRuntime.Cache and HttpContext.Current.Cache?

I find following detail from http://theengineroom.provoke.co.nz/archive/2007/04/27/caching-using-httpruntime-cache.aspx

For caching I looked into using HttpContext.Current.Cache but after reading other blogs I found that caching using HttpContext uses HttpRuntime.Cache to do the actual caching. The advantage of using HttpRuntime directly is that it is always available, for example, in Console applications and in Unit tests.

Using HttpRuntime.Cache is simple. Objects can be stored in the cache and are indexed by a string. Along with a key and the object to cache the other important parameter is the expiry time. This parameter sets the time before the object is dropped from the cache.

Here is good link for you.

Another good resource.

 

Is the HttpContext.Current.Cache available to all sessions

HttpContext.Current is available to all pages, but not necessarily to all threads. If you try to use it inside a background thread, ThreadPool delegate, async call (using an ASP.NET Async page), etc., you'll end up with a NullReferenceException.

If you need to get access to the cache from library classes, i.e. classes that don't have knowledge of the current request, you should use HttpRuntime.Cache instead. This is more reliable because it doesn't depend on an HttpContext.

 

扩展阅读

Cache Class

Caching Application Data

 

 

How to: Add Items to the Cache

You can access items in the application cache using the Cache object. You can add an item to the application cache using the Cache object's Insert method. The method adds an item to the cache and has several overloads that enable you to add the item with different options for setting dependencies, expiration, and removal notification. If you use the Insert method to add an item to the cache and an item with the same name already exists, the existing item in the cache is replaced.

You can also add items to the cache using the Add method. This method enables you to set all the same options as the Insert method; however, Add method returns the object you added to the cache. Additionally, if you use the Add method and an item with the same name already exists in the cache, the method will not replace the item and will not raise an exception.

 

 

How to: Retrieve Values of Cached Items

To retrieve data from the cache, you specify the key that the cached item was stored under. However, because information stored in the cache is volatile不稳定的—that is, it might be removed by ASP.NET—the recommended development pattern is to determine first whether the item is in the cache. If it is not, you add it back to the cache and then retrieve the item.

To retrieve the value of a cached item

  • Check to see if the item is not null (Nothing in Visual Basic), in the Cache object. If it exists, assign it to your variable. Otherwise, recreate the item, add it to the cache, and then access it.

    The following code example shows how to retrieve the item named CacheItem from the cache. The code assigns the contents of the item to the variable named cachedString. If the item is not in the cache, the code adds the item to the cache and then assigns the item to cachedString.

    C#
string cachedString;
cachedString = (string)Cache["CacheItem"];
if (cachedString == null)
{
  cachedString = "Hello, World.";
  Cache.Insert("CacheItem", cachedString);
}

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(206)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2018-04-24 There was a conflict between
点击右上角即可分享
微信分享提示