Using Cache methods & HttpCacheability in ASP.Net

The main benefits of caching are performance-related operations like accessing database information can be one of the most expensive operations of an ASP page's life cycle. If the database information is fairly static, this database-information can be cached.
Here we look at many of the Cache methods you can use in ASP.NET code-behind and other code files. These methods and properties are used to control the HTTP cache settings on your ASP.NET response. They must be called on the HttpResponse object, using the Response.Cache...() style syntax.

::Method name & It's usage::

AddValidationCallback: You will need this when using callbacks, which I have no experience with.

AppendCacheExtension: You can use this to add a custom header to the Cache-Control header, which could be used for future changes in HTTP 1.1 or proprietary options.

SetAllowResponseInBrowserHistory: This overrides certain settings made by SetCacheability, such as NoCache and ServerAndNoCache.

SetCacheability: This is important and sets the Cache-Control header, which is the preferred mechanism for caching dynamic pages. See the list of HttpCacheability enums below.

SetETag: This allows you to specify a string that is considered the 'tag' of a resource. This is not recommended by Yahoo and not normally needed.

SetETagFromFileDependencies: Tells ASP.NET to assign random etags to your resources that are keyed to the file contents. Simplifies ETag usage. Not normally needed.

SetExpires: Very important and useful for static resources such as logo images or web site layout images. Recommended by Yahoo for static resources.

SetLastModified: This can be used to date your file and return a 304 when a user requests the same one again. This doesn't save an HTTP request. Yahoo recommends modified dates over ETags.

SetLastModifiedFromFileDependencies: Same as above but tells ASP.NET to read in the file metadata automatically.

SetMaxAge: Very important. This gives you a relative time window you can specify a resource can be cached for. This is an alternative to the Expires header, and it overrides the Expires header.

SetNoServerCaching: This seems to remove the HttpCacheability.Server setting. It seems like a really poor design in ASP.NET.

SetNoStore: Applies the "Cache-Control: no-store" header. This is useful for advertisements and dynamic responses.

SetNoTransforms: Some proxy caches can change the format of your files when they store them. This setting should tell them not to.

SetOmitVaryStar: Changes header when using vary parameters. Not often useful.

SetProxyMaxAge: Not likely to be useful. It suggests that proxy caches can expire or keep resources for a specific time. I doubt they would honor this exactly.

SetRevalidation: Indicates when validation should occur. See Cache-Control header section.

SetSlidingExpiration: Changes the logic of when the server expires its cache. Has many quirks and you must test it carefully.

SetValidUntilExpires: Don't listen to browsers when they say a resource is expired or stale. Otherwise, they can invalidate caches.

SetVaryByCustom: Allows you to set the custom vary header, which is useful when you have the Vary header. See section on Vary.

VaryByContentEncodings,VaryByHeaders,VaryByParams: These are public getters only, meaning you cannot set these properties. They are useful for debugging and diagnostics of your Vary header.

HttpCacheability enumeration values in ASP.Net

You need to call SetCacheability on the Response.Cache to set the main Cache-Control header. This header controls the location and general behavior of the cache. You need to combine this setting with other Cache class method calls to achieve many behaviors. However, these enums define the general setting.

::Enum value & It's usage::

HttpCacheability.NoCache: Tells the browser, the server, and proxies to never cache this resource. This can be useful for advertisements and resources that are always changing.

HttpCacheability.Private: Only cache on the browser. This will provide bandwidth savings for your users, but your server won't store a cached copy of the output. This is adequate for many sites.

HttpCacheability.Public: The ultimate cache setting: tells the server to save the page, proxy caches to save the page, and the browser to save the page.

HttpCacheability.Server: Only cache the page on the server (output caching without browser caching). However, when your visitors click on your static pages, they will be reloaded.

HttpCacheability.ServerAndNoCache: The same as NoCache except it allows the server to store the page. Has slightly different meaning for remote clients and proxies. Not often useful.

HttpCacheability.ServerAndPrivate: Tells proxy caches to never cache this page, but to allow the browser and the server to cache it.
Tags: , , ,
Hot on Web:


About author