I have a Dictionary in my code which I am using in the following manner: Key Values 2011-07-15 1, 2, 3 2011-07-20 4, 5, 6 2010-02-11 7, 8, 9 My. Not a registered user yet? If you are a new visitor and do not already have a username and login, please register and create a new account.
-->Caching enables you to store data in memory for rapid access. When the data is accessed again, applications can get the data from the cache instead of retrieving it from the original source. This can improve performance and scalability. In addition, caching makes data available when the data source is temporarily unavailable.
The .NET Framework provides caching functionality that you can use to improve the performance and scalability of both Windows client and server applications, including ASP.NET.
Note
In the .NET Framework 3.5 and earlier versions, ASP.NET provided an in-memory cache implementation in the System.Web.Caching namespace. In previous versions of the .NET Framework, caching was available only in the System.Web namespace and therefore required a dependency on ASP.NET classes. In the .NET Framework 4, the System.Runtime.Caching namespace contains APIs that are designed for both Web and non-Web applications.
Caching Data
You can cache information by using classes in the System.Runtime.Caching namespace. The caching classes in this namespace provide the following features:
Abstract types that provide the foundation for creating custom cache implementations.
A concrete in-memory object cache implementation.
The abstract base caching class (ObjectCache) defines the following caching tasks:
Caching Sound Effect
Creating and managing cache entries.
Specifying expiration and eviction information.
Triggering events that are raised in response to changes in cache entries.
The MemoryCache class is an in-memory object cache implementation of the ObjectCache class. You can use the MemoryCache class for most caching tasks.
Note
The MemoryCache class is modeled on the ASP.NET cache object that is defined in the System.Web.Caching namespace. Therefore, the internal caching logic similar to the logic that was provided in earlier versions of ASP.NET.
For an example of how to use to caching in a WPF application, see Walkthrough: Caching Application Data in a WPF Application.
Caching in ASP.NET Applications
The caching classes in the System.Runtime.Caching namespace provide functionality for caching data in ASP.NET.
Note
If your application targets the .NET Framework 3.5 or earlier, you must use the caching classes that are defined in the System.Web.Caching namespace. For more information, see ASP.NET Caching Overview.
Note
When you develop new applications, we recommend that you use the MemoryCache class. The API that is provided in the System.Runtime.Caching namespace is like the API that is provided in the Cache namespace. Therefore, the API will be familiar if you used caching in earlier versions of ASP.NET. For an example of how to use caching in ASP.NET applications, see Walkthrough: Caching Application Data in ASP.NET.
Output Caching
To manually cache application data, you can use the MemoryCache class in ASP.NET. ASP.NET also supports output caching, which stores the generated output of pages, controls, and HTTP responses in memory. You can configure output caching declaratively in an ASP.NET Web page or by using settings in the Web.config file. For more information, see outputCache Element for caching (ASP.NET Settings Schema).
ASP.NET lets you extend output caching by creating custom output-cache providers. By using custom providers, you can store cached content using other storage devices such as disks, cloud storage, and distributed cache engines. To create a custom output cache provider, you create a class that derives from the OutputCacheProvider class and configure the application to use the custom output cache provider.
Caching in WCF REST Services
For WCF REST services, the .NET Framework enables you to take advantage of the declarative output caching that is available in ASP.NET. This enables you to cache responses from your WCF REST service operations. When a user sends an HTTP GET request to a service that is configured for caching, ASP.NET sends back the cached response, and the service method is not called. After the cache expires, the next time that a user sends an HTTP GET request, your service method is called and the response is again cached.
The .NET Framework also enables you to implement conditional HTTP GET caching. In REST scenarios, a conditional HTTP GET request is often used by services to implement intelligent HTTP caching as described in the HTTP Specification. For more information, see Caching Support for WCF Web HTTP Services.
Extending Caching in the .NET Framework
Caching in the .NET Framework is designed to be extensible. The ObjectCache class enables you to create a custom cache implementation. This class provides members that are available to all managed applications, including Windows Forms, Windows Presentation Foundation (WPF), and Windows Communications Foundation (WCF). You might do this in order to create a cache class that uses a different storage mechanism, or if you want granular control over cache operations.
To extend caching you can do the following:
Create a custom class that derives from the ObjectCache class and then provide a custom cache implementation in the derived class.
Create a class that derives from MemoryCache class and customize or extend the derived class. For an example of how to do this, see Caching Application Data by Using Multiple Cache Objects in an ASP.NET Application.
Create a class that derives from the OutputCacheProvider class and configure the application to use the custom output cache provider.
For more information, see the entry Extensible Output Caching with ASP.NET 4 (VS 2010 and .NET Framework 4.0 Series) on Scott Guthrie's blog.
See also
netwerk/base/public/nsICachingChannel.idl
ScriptablensICacheInfoChannel
Last changed in Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)This interface provides:
- Support for 'stream as file' semantics (for JAR and plugins).
- Support for 'pinning' cached data in the cache (for printing and save-as).
- Support for uniquely identifying cached data in cases when the URL is insufficient. For example a HTTP form submission.
A channel may optionally implement this interface to allow clients to affect its behavior with respect to how it uses the cache service.
Method overview
Attributes
Attribute | Type | Description |
cacheAsFile | boolean | Specifies whether or not the data should be cached to a file. This may fail if the disk cache is not present. The value of this attribute is usually only settable during the processing of a channel's OnStartRequest . The default value of this attribute depends on the particular implementation of nsICachingChannel . |
cacheFile | nsIFile | Get the 'file' where the cached data can be found. This is valid for as long as a reference to the cache token is held. This may return an error if cacheAsFile is false . Read only. |
cacheForOfflineUse | boolean | Specifies whether or not the data should be placed in the offline cache, in addition to normal memory/disk caching. This may fail if the offline cache is not present. The value of this attribute should be set before opening the channel. |
cacheKey | nsISupports | Uniquely identifies the data in the cache for this channel. Holding a reference to this key does not prevent the cached data from being removed. A cache key retrieved from a particular instance of null indicating that the URI of the channel is sufficient to locate the same cache entry. Setting a null cache key is likewise valid. |
cacheToken | nsISupports | Uniquely identifies the data in the cache. Holding a reference to this token prevents the cached data from being removed. A cache token retrieved from a particular instance of QueryInterface 'd to a nsICacheEntryInfo if more detail about the cache entry is needed. For example, expiration time. |
offlineCacheClientID | ACString | The session into which to cache offline data. If not specified, data will be placed in 'HTTP-offline'. |
offlineCacheToken | nsISupports | The same as cacheToken but accessing the offline app cache token if there is any. Exceptions thrown
|
Constants
Constant | Value | Description |
LOAD_NO_NETWORK_IO | 1 << 26 | This load flag inhibits fetching from the net. An error of This flag can be used to find out whether fetching this URL would cause validation of the cache entry via the network. Combining this flag withLOAD_BYPASS_LOCAL_CACHE will cause all loads to fail. This flag differs from LOAD_ONLY_FROM_CACHE in that this flag fails the load if validation is required while LOAD_ONLY_FROM_CACHE skips validation where possible. |
LOAD_CHECK_OFFLINE_CACHE | 1 << 27 | This load flag causes the offline cache to be checked when fetching a request. It will be set automatically if the browser is offline. This flag will not be transferred through a redirect. |
LOAD_BYPASS_LOCAL_CACHE | 1 << 28 | This load flag causes the local cache to be skipped when fetching a request. Unlike LOAD_BYPASS_CACHE , it does not force an end-to-end load (That is, it does not affect proxy caches). |
LOAD_BYPASS_LOCAL_CACHE_IF_BUSY | 1 << 29 | This load flag causes the local cache to be skipped if the request would otherwise block waiting to access the cache. |
LOAD_ONLY_FROM_CACHE | 1 << 30 | This load flag inhibits fetching from the net if the data in the cache has been evicted. An error of NS_ERROR_DOCUMENT_NOT_CACHED will be sent to the listener's onStopRequest in this case. This flag is set automatically when the application is offline. |
LOAD_ONLY_IF_MODIFIED | 1 << 31 | This load flag controls what happens when a document would be loaded from the cache to satisfy a call to OnDataAvailable events will be skipped. The listener will only see OnStartRequest followed by OnStopRequest . |
Methods
Caching
isFromCache()
Obsolete since Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)This method finds out whether or not this channel's data is being loaded from the cache.
Parameters
Caching_sha2_password
None.
Return value
Returns true
if this channel's data is being loaded from the cache, otherwise returns false
. This value is undefined before the channel fires its OnStartRequest
notification and after the channel fires its OnStopRequest
notification.