SlideShare a Scribd company logo
ASP.NET
CACHING
Assignment-2
Presented By:
Ashish Kumar
Mca-4th sem.
Caching
 Caching is a technique of storing frequently used
data/information in memory, so that, when the same
data/information is needed next time, it could be directly
retrieved from the memory instead of being generated by the
application.
 Caching places frequently used data in quickly accessed media
such as the random access memory of the computer.
Caching in ASP.Net
Different Types of Caching:
Output Caching : Output cache stores a copy of the finally
rendered HTML pages or part of pages sent to the client. When
the next client requests for this page, instead of regenerating
the page, a cached copy of the page is sent, thus saving time.
Data Caching : Data caching means caching data from a
data source. As long as the cache is not expired, a request for
the data will be fulfilled from the cache. When the cache is
expired, fresh data is obtained by the data source and the cache
is refilled.
 Object Caching : Object caching is caching the objects on
a page, such as data-bound controls. The cached data is stored
in server memory.
 Class Caching : Web pages or web services are compiled
into a page class in the assembly, when run for the first time.
Then the assembly is cached in the server. Next time when a
request is made for the page or service, the cached assembly
is referred to When the source code is changed, the CLR
recompiles the assembly.
 Configuration Caching : Application wide
configuration information is stored in a configuration file.
Configuration caching stores the configuration information in
the server memory.
 Output Caching:
Rendering a page may involve some complex processes such as,
database access, rendering complex controls etc. Output caching
allows bypassing the round trips to server by caching data in
memory. Even the whole page could be cached.
The Output Cache directive is responsible of output caching. It
enables output caching and provides certain control over its
behavior.
Syntax:
<%@ OutputCache Duration="15" VaryByParam="None" %>
protected void Page_Load(object sender, EventArgs e)
{
Thread.Sleep(10000); Response.Write("This page was
generated and cache at:" + DateTime.Now.ToString());
}
 Data Caching
The main aspect of data caching is caching the data source
controls. like a database file. These controls derive from the
abstract class DataSourceControl and have the following
inherited properties for implementing caching:
 CacheDuration - It sets the number of seconds for which
the data source will cache data.
 CacheExpirationPolicy - It defines the cache behavior
when the data in cache has expired.
 CacheKeyDependency - It identifies a key for the controls
that auto-expires the content of its cache when removed.
 Enable Caching - It specifies whether or not to cache the
data.
Example Of Data Caching
For this example, add a label to the page, which would show the
response time for the page.
<asp:Label ID="lbltime" runat="server"></asp:Label>
Add an event handler for the page load event:
protected void Page_Load(object sender, EventArgs e) {
lbltime.Text = String.Format("Page posted at: {0}",
DateTime.Now.ToLongTimeString()); }
Object Caching
Object caching provides more flexibility than other cache
techniques. You can use object caching to place any object in the
cache. The object can be of any type a data type, a web control,
a class, a dataset object, etc. The item is added to the cache simply
by assigning a new key name, shown as follows Like:
Cache["key"] = item;

More Related Content

Asp.net

  • 2. Caching  Caching is a technique of storing frequently used data/information in memory, so that, when the same data/information is needed next time, it could be directly retrieved from the memory instead of being generated by the application.  Caching places frequently used data in quickly accessed media such as the random access memory of the computer.
  • 3. Caching in ASP.Net Different Types of Caching: Output Caching : Output cache stores a copy of the finally rendered HTML pages or part of pages sent to the client. When the next client requests for this page, instead of regenerating the page, a cached copy of the page is sent, thus saving time. Data Caching : Data caching means caching data from a data source. As long as the cache is not expired, a request for the data will be fulfilled from the cache. When the cache is expired, fresh data is obtained by the data source and the cache is refilled.
  • 4.  Object Caching : Object caching is caching the objects on a page, such as data-bound controls. The cached data is stored in server memory.  Class Caching : Web pages or web services are compiled into a page class in the assembly, when run for the first time. Then the assembly is cached in the server. Next time when a request is made for the page or service, the cached assembly is referred to When the source code is changed, the CLR recompiles the assembly.  Configuration Caching : Application wide configuration information is stored in a configuration file. Configuration caching stores the configuration information in the server memory.
  • 5.  Output Caching: Rendering a page may involve some complex processes such as, database access, rendering complex controls etc. Output caching allows bypassing the round trips to server by caching data in memory. Even the whole page could be cached. The Output Cache directive is responsible of output caching. It enables output caching and provides certain control over its behavior. Syntax: <%@ OutputCache Duration="15" VaryByParam="None" %> protected void Page_Load(object sender, EventArgs e) { Thread.Sleep(10000); Response.Write("This page was generated and cache at:" + DateTime.Now.ToString()); }
  • 6.  Data Caching The main aspect of data caching is caching the data source controls. like a database file. These controls derive from the abstract class DataSourceControl and have the following inherited properties for implementing caching:  CacheDuration - It sets the number of seconds for which the data source will cache data.  CacheExpirationPolicy - It defines the cache behavior when the data in cache has expired.  CacheKeyDependency - It identifies a key for the controls that auto-expires the content of its cache when removed.  Enable Caching - It specifies whether or not to cache the data.
  • 7. Example Of Data Caching For this example, add a label to the page, which would show the response time for the page. <asp:Label ID="lbltime" runat="server"></asp:Label> Add an event handler for the page load event: protected void Page_Load(object sender, EventArgs e) { lbltime.Text = String.Format("Page posted at: {0}", DateTime.Now.ToLongTimeString()); }
  • 8. Object Caching Object caching provides more flexibility than other cache techniques. You can use object caching to place any object in the cache. The object can be of any type a data type, a web control, a class, a dataset object, etc. The item is added to the cache simply by assigning a new key name, shown as follows Like: Cache["key"] = item;