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; } }
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.
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
.
扩展阅读
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); }