0% found this document useful (0 votes)
67 views34 pages

Submitted To: Mr. R.K. Singla Submitted By:navneet Kaur Mca 5 Sem Morning Roll No. 14

The document discusses caching in ASP.NET applications. It describes how caching can improve performance by storing expensive data and pages in memory for faster retrieval. It discusses different types of caching including output caching, data caching, and fragment caching. It provides examples of how to implement caching using ASP.NET cache directives and programmatically using the Cache class.

Uploaded by

ghugi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views34 pages

Submitted To: Mr. R.K. Singla Submitted By:navneet Kaur Mca 5 Sem Morning Roll No. 14

The document discusses caching in ASP.NET applications. It describes how caching can improve performance by storing expensive data and pages in memory for faster retrieval. It discusses different types of caching including output caching, data caching, and fragment caching. It provides examples of how to implement caching using ASP.NET cache directives and programmatically using the Cache class.

Uploaded by

ghugi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Submitted to:

Mr. R.K. Singla Submitted by:Navneet kaur


MCA 5th sem morning
Roll no. 14
Caching
 One of the coolest features in Asp.net.
 Caching enables you to store the expensive data into
Cache object and later retrieve it without doing
expensive operations.
 A very common example where you want to use
caching is datagrid paging. Datagrid paging enables
you to view the records in multiple pages. Each time
you visit a different page all the records are fetched
from the database. This becomes very expensive
operation. Caching can save a lot of expensive
operations since you can store all the records in the
cache object and use the cache object as the data
source.
Introduction

 Web applications are accessed by multiple users. Slow


access is the most common problem for any web site
when it is accessed by a large number of client
simultaneously.
 For resolve this problem we can have used high level of
hardware configuration, Load Balancer , High bandwidth
but load is not the only reason that make a web site is
slow , so we need to provide such kind of mechanism
which also provide fast data access and provide
performance improvement of web site. Caching
provide the solution.
Caching

 Caching is a technique where we can store frequently used data


and Web pages are stored temporarily on local hard disks for later
retrieval. This technique improves the access time when multiple
users access a Web site simultaneously or a single user accesses a
Web site multiple times.
 Caching enables you to store the expensive data into Cache object
and later retrieve it without doing expensive operations.
 A very common example where you want to use caching is datagrid
paging. Datagrid paging enables you to view the records in multiple
pages. Each time you visit a different page all the records are
fetched from the database. This becomes very expensive operation.
Caching can save a lot of expensive operations since you can store all
the records in the cache object and use the cache object as the data
source.
Different Caching Location

 Caching in a web application can be done either on client


side (Client Browser), In between Client and Server
(Proxy & Reverse Proxy Caching ) and on Server
Side( Data Caching/ Page Output Caching) . So we can
classified caching location in 4 way:-

 Client Caching
 Proxy Caching
 Reverse Proxy Caching
 Web Server Caching
1. Client Caching :
 In Client Caching Client Browser perform caching by
storing cached data on local disk as temporary file or
browser internal memory. This provide quick access of
some information by client which reduce the network
load and server load also. This information can't be
shared by other clients so it is client specific.
Cont…

Advantages
 Data that are cached on local client can be easily accessed
 Reduce Network Traffic

Disadvantages
 Cached data are totally browser dependent, so it is not shareable
2. Proxy Caching :
 Main disadvantage of Client caching was data that are store on
client browser are client specific. Proxy Caching technique used a
dedicated server that store caching information in between client
and web server in a shared location, that all client can used same
shared data. The proxy server (e.g. Microsoft Proxy Server ) fulfills all
the requests for the Web page without sending out the request to
the actual Web server over the Internet, resulting in faster access.
Cache array
 Proxy caches are often located near network gateways to reduce the
bandwidth . Some times Multiple Proxy Cache server is used for
larger number of client. This is called Cache Array.
Contd....

Advantages
 Data that are cached on proxy server can a
accessed easily
 Reduce Network Traffic
Disadvantages
 It a Deployment and Infrastructure overhead
to maintain a Proxy Cache Server
3. Reverse Proxy Caching :
 Some Proxy Cache server can placed in front of web server to reduce
the number of requests that they receive. This allows the proxy
server to respond to the frequently received requests and pass the
other requests to the Web server. This is called a reverse proxy.
Contd…

Advantages
 Data that are cached on reverse proxy server can a accessed easily
 Web server reduce the number of request

Disadvantages
 As the Server configured in front of Web sever some what it
increases the network traffic.
4. Web Server Caching :

 In Web server caching cached data stored inside the web


server, Data caching and page caching used web sever
caching mechanism.
Contd…

Advantages
 Improve the performance of sites by decreasing the round trip of
data retrieving from database or some other server

Disadvantages
 Increase the Network Load
Caching Opportunity in
ASP.NET
 ASP.NET provides support for
 page
 partial page (Fragment)
 data caching

Caching a page that is dynamically generated, called PAGE OUTPUT


CACHING. In page caching when a pages is cached that dynamically
generated only the first time it is accessed. Any subsequent access to the same
page will be returned from the cache.
ASP.NET also allow to Cached a portion of a page called PARTIAL
PAGE CACHING OR FRAGMENT CACHING .
Other server data are cached (e.g. SQL Server data, XML Data ) that can be easily
accessed without re-retrieving that data using DATA CACHING.
Output Caching or Page
Caching:
 used for pages and is also known as Page-level caching.
 Enable output caching to add a directive in your html view of the aspx page. The
output directive can be written like this:
@ OutputCache Duration="60" VaryByParam="none"
The page will be cached for 60 seconds, the VaryByParam attribute
is set to "none" which means that there is no sort of caching
implemented.
 2 attributes of output caching are:-
 Duration - The time in seconds of how long the output should be
cached. After the specified duration has elapsed, the cached output
will be removed and page content generated for the next request.
That output will again be cached for 10 seconds and the process
repeats.
 VaryByParam - This attribute is compulsory and specifies the
querystring parameters to vary the cache.
 None - means the page content to be served is the same regardless of the
parameters passed through the querystring
Contd…

 That's why we always specify what the caching depends on.

@ OutputCache Duration="60"
VaryByParam="Category"

In the above OutputCache directive the VaryByParam


attribute is changed to "Category" which means that now
the result of caching depends upon Category. Here Category
represents the name of the column in the database.
Programmatic Page Caching
 You can also use caching programmatically, meaning
that you can change the value of cache depending upon
the tasks performed by the user.
 The Response.Cache class let's you access the
functionality to work with the cache object.
You can change the expiration time on the Cache using
the SetExpires method of the Response.Cache
class.
Response.Cache.SetExpires(System.DateTime.Now.Add
Minutes(10));

.
Data Caching
 Data Caching can tremendously increase performance
since each time the data is requested you can turn to the
Cache object rather than going to the database and
fetching the result.
 Here caching of data as objects is done. We can
store objects in memory and use them across various
pages in our application. This feature is implemented
using the Cache class. This cache has a lifetime
equivalent to that of the application. Objects can be
stored as name value pairs in the cache. A string value
can be inserted into the cache as follows:
 Cache["name"]="Smitha";
Contd…
 The stored string value can be retrieved like this:
 if (Cache["name"] != null)
Label1.Text= Cache["name"].ToString();
 To insert objects into the cache, the Add method or
different versions of the Insert method of the Cache class
can be used. These methods allow us to use the more
powerful features provided by the Cache class. One of the
overloads of the Insert method is used as follows:
Cache.Insert("Name", strName,
new
CacheDependency(Server.MapPath("name.txt"),
DateTime.Now.AddMinutes(2), TimeSpan.Zero);
Contd..
 The first two parameters are the key and the object
to be inserted.
 The third parameter is of type CacheDependency and
helps us set a dependency of this value to the file named
name.txt.
So whenever this file changes, the value in the cache is
removed. We can specify null to indicate no dependency.
 The fourth parameter specifies the time at which the
value should be removed from cache.
 The last parameter is the sliding expiration
parameter which shows the time interval after which the
item is to be removed from the cache after its last accessed
time.
Contd…
 The cache automatically removes the least used items
from memory, when system memory becomes low.
This process is called scavenging. We can specify
priority values for items we add to the cache so that
some items are given more priority than others:
Cache.Insert("Name", strName,
new
CacheDependency(Server.MapPath("name.txt"),
DateTime.Now.AddMinutes(2), TimeSpan.Zero,
CacheItemPriority.High, null);
 The CacheItemPriority enumeration has members to
set various priority values. The
CacheItemPriority.High assigns a priority level to an
item so that the item is least likely to be deleted from the
cache.
Caching Page Fragments
 Fragment Caching refers to caching the sections of the
page. These sections are most commonly UserControls.
Page fragment caching allows you to cache the small
portion of the page instead of caching the whole page.
 @ OutputCache Duration="120"
VaryByParam="CategoryID;SelectedID"

In the Page directive above we have cached CategoryID
and SelectedID for 120 seconds. Both of these are the
query string parameters.

This means that if the first user request CategoryID = 2 and
the second user request the same CategoryID than the
second user will recieve the contents from the cache object
instead of going to the database and pulling records.
Contd…
 @ OutputCache Duration="120"
VaryByParam="none"
VaryByControl="Category"

The VaryByControl attribute can only be used in
fragment caching.
 You can use this to cache the value of the controls.
 These controls can be any server controls like
dropdownlist or datagrid.
When using fragment caching you only need to put the
cache directive in the user control and not on the page.
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.
Sliding expiration
 Specifies how long after an item was last accessed that
it expires. For example, you can set an item to expire 20
minutes after it was last accessed in the cache.
Absolute expiration
 Specifies that an item expires at a set time, regardless
of how often it is accessed. For example, you can set an
item to expire at 6:00 PM or after four hours.
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.
Key dependency

 Items in the application cache are stored in


key/value pairs.
 Key dependency allows an item to be dependent on
the key of another item in the application cache.
When the original item is removed, the item that has
the key dependency is also removed.
 For example, you could add a cache item named
ReportsValid, and then cache several reports that
are dependent on the ReportsValid key. When the
ReportsValid item is removed, all the dependent
cached reports are similarly removed from the
cache.
File dependency and SQL
dependency
 An item in the cache is dependent on an
external file. If the file is modified or deleted,
the cached item is removed.
 An item in the cache is dependent on changes
in a table in a Microsoft SQL Server 2005,
SQL Server 2000, or SQL Server 7.0 database.
For SQL Server 2005, an item can be
dependent on a row in a table.
Aggregate dependency and
Custom dependency
 An item in the cache is dependent on multiple
elements through the use of the
AggregateCacheDependency class. If any of the
dependencies change, the item is removed from
the cache.
 An item in the cache is configured with a
dependency that you create in your own code.
For example, you can create a custom Web
service cache dependency that removes data
from the cache when a call to a Web service
results in a particular value.
Time Based Dependency
Time-based dependency causes an item to expire at a defined
time. Cache.Insert() method of the Cache class is used
to create a time-based dependency. Two Types of Time
based Dependency are available.
 Absolute
 Sliding
 Absolute: Sets an absolute time for a cache item to
expire. Absolute expirations are specified in full-time
format (hh:mm:ss). The object will be expired from the
cache at the specified time.
 Sliding: Resets the time for the item in the Cache to
expire on each request. This is useful when an item in the
cache is to be kept alive so long as requests for that item
are coming in from various clients.
Caching Considerations
 Output Caching Considerations
 Enable output caching on a page that is frequently accessed and returns
the exact same contents for many of those accesses
 When enabling output caching for a page, be sure not to introduce
incorrect behavior and/or rendering for any particular client.
 Determine the duration of the cached page carefully to balance speed of
access (throughput) with memory consumption and cache coherency
correctness.
 Consider enabling sliding expiration on a page if you end up using
VaryByParam='*'.
 Data Caching Consideration
 The data cache is not a container for shared updateable state.
 Cache data that is accessed frequently and is relatively expensive to
acquire.
 If data is dependent on a file, directory, or other cache entry, use a
CacheDependency to be sure it remains current.
Situations and Caching
types
 The generated page generally stays the same, but
there are several tables shown within the output that
change regularly----Use Fragment Caching in
this situation.
 The generated page constantly changes, but there
are a few objects that don’t change very often----
Use Data Caching for the objects.
 The generated page changes every few hours as
information is loaded into a database through
automated processes----Use Output Caching
and set the duration to match the
frequency of the data changes.

You might also like