Application Caching: To Add An Item To The Cache by Directly Setting The Item Via Key and Value
Application Caching: To Add An Item To The Cache by Directly Setting The Item Via Key and Value
Application Caching: To Add An Item To The Cache by Directly Setting The Item Via Key and Value
Net Application
An application can often increase performance by storing data in memory that is accessed
frequently and that requires significant processing time to create. For example, if your
application processes large amounts of data using complex logic and then returns the data as a
report accessed frequently by users, it is efficient to avoid re-creating the report every time that a
user requests it. Similarly, if your application includes a page that processes complex data but
that is updated only infrequently, it is inefficient for the server to re-create that page on every
request.
To help you increase application performance in these situations, ASP.NET provides caching
using two basic caching mechanisms.
1. Application caching, which allows you to cache data you generate, such as a DataSet
object
2. Page output caching, which saves the output of page processing and reuses the
output instead of re-processing the page when a user requests the page again.
1. Application caching
The application cache provides a programmatic way for you to store arbitrary data in memory
using key/value pairs. Using the application cache is like using application state. However,
unlike application state, the data in the application cache is volatile. This means it is not stored in
memory for the life of the application. The advantage of using the application cache is that
ASP.NET manages the cache and removes items when they expire or become invalidated, or
when memory runs low. You can also configure application caching to notify your application
when an item is removed.
To add an item to the cache by directly setting the item via key and value
Add items to the cache as you would add items to a dictionary by specifying the item's key and
value.
The following code example adds an item named CacheItem1 to the Cache object:
Cache["CacheItem1"] = "Cached Item 1";
I.
a) Include an @ OutputCache directive in the page and define Duration and VaryByParam
attributes.
b) Include a Location attribute in the @ OutputCache directive and define its value as one of
the following values in the OutputCacheLocation enumeration: Any, Client, Downstream,
Server, ServerAndClient, or None.
To enable output caching for a page, include the following page directive in the page:
Syntax
<%@ OutputCache Duration="60" VaryByParam="*" Location="Client" %>
II.
<outputCacheSettings>
<outputCacheProfiles>
<add name="Cache30Seconds" duration="30"
varyByParam="none" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
2. Include an @ OutputCachedirective in each ASP.NET page that uses the
profile, and set the CacheProfile attribute to the name of the cache profile
defined in your Web.config file.
The following code specifies that the page should use the cache profile named
Cache30Seconds:
<%@ OutputCache CacheProfile="Cache30Seconds" %>
A. Full Page Caching : To set a page's cacheability programmatically
In the page's code, call the SetCacheability method on the Cache property of the Response
object.
The following code sets the Cache-Control HTTP header to Public.
Response.Cache.SetCacheability(HttpCacheability.Public);
Control caching
In control caching, also known as fragment caching, you can cache parts of the page output
by creating user controls to contain the cached content and then marking the user controls as
cacheable. This allows specific content within a page to be cached, while the overall page is
recreated each time. For example, if you create a page that displays largely dynamic content,
such as stock information, but also has sections that are static, such as weekly summaries,
you can create the static sections in user controls and configure the user controls to be
cached.
Page fragment caching is implemented with user controls. You define different areas of a
page by creating separate user controls for each area of the page. Within each user control,
you can use the OutputCache directive to indicate how the output of the control is cached.
Page fragment caching is implemented with user controls. You define different areas of a page by
creating separate user controls for each area of the page. Within each user control, you can use
the OutputCache directive to indicate how the output of the control is cached.
If we enable caching for a user control, you can no longer programmatically access the control in
its containing page.
For example, suppose you declare a cached user control like this:
ctlContents.Message = "Hello!"
Post-cache substitution
Post-cache substitution is the opposite. The page is cached, but fragments within the page are
dynamic and should therefore not be cached. For example, if you create a page that is entirely
static for set periods of time such as a page of news stories, you can set the entire page to be
cached. If you added rotating ad banners to the cached page, they would not change between
page requests. However, with post-cache substitution, the page can be cached, but you can mark
specific parts as non-cacheable. In the example, you would mark your ad banners as noncacheable. They would then be dynamically created for each page request and added to the
cached page output.
To allow you to cache a page but substitute some content dynamically, you can use ASP.NET
post-cache substitution. With post-cache substitution, the entire page is output cached with
specific parts marked as exempt from caching. In the example of the ad banners, the AdRotator
control allows you to take advantage of post-cache substitution so that ads dynamically created
for each user and for each page refresh.
There are three ways to implement post-cache substitution:
a) Declaratively, using the Substitution control.
b) Programmatically, using the Substitution control API.
c) Implicitly, using the AdRotator control.
Automatic Data Removal
ASP.NET can remove data from the cache for one of these reasons:
Because memory on the server is low, a process known as scavenging.
Because the item in the cache has expired.
Because the item's dependency changes.
To help you manage cached items, ASP.NET can notify your application when items are
removed from the cache.
Scavenging
Scavenging is the process of deleting items from the cache when memory is scarce. Items are
removed when they have not been accessed in some time or when items are marked as low
priority when they are added to the cache. ASP.NET uses the CacheItemPriority object to
determine which items to scavenge first.
Expiration
In addition to scavenging, ASP.NET automatically removes items from the cache when they
expire. When adding an item to the cache, you can set it to expire as described in the following
table.
Dependencies
You can configure an item's lifetime in the cache to be dependent on other application elements
such as files or databases. When the element that a cache item depends on changes, ASP.NET
removes the item from the cache. For example, if your Web site displays a report that the
application creates from an XML file, you can place the report in the cache and configure it to
have a dependency on the XML file. When the XML file changes, ASP.NET removes the report
from the cache. When your code requests the report, the code first determines whether the report
is in the cache, and if not, your code can re-create it. Therefore, an up-to-date version of the
report is always available.
ASP.NET caching supports the dependencies described in the following table.