Transcript aspcaching
ASP .NET Caching
-
Pradeepa Chandramohan
What is Caching?
•
•
•
•
•
Storing data in memory for quick access.
In Web Application environment, data that is cached is
usually, commonly displayed database values.
Repeated database calls are avoided.
Demand on Web server’s and database server’s system
resources are decreased.
Increases performance by keeping frequently accessed
data in memory.
Types of Caching
•
•
•
•
Output Caching
Fragment Caching
Data Caching
Time based Caching
Output Caching
•
•
•
•
Also known as Page level Caching.
Implementation is through an Output Cache Engine.
Each time an ASP .NET page request comes in, the
engine checks for a cached output entry. If found, this
cached HTML is sent as response, otherwise, the page
is dynamically created and stored in the Output Cache
Engine.
Useful in cases of many static pages.
Output Caching Implementation
•
•
By using the OutputCache page directive.
Syntax is as shown below:
<%@OutputCache Duration=“60” VaryByParam=“none”%>
•
•
Duration – number of seconds the HTML output of
the Web page is held in Cache.
VaryByParam – Specifies how the caching should be
performed based on the query string supplied to the
page.
Output Caching - Example
•
Consider the following URL:
http://localhost/Caching/WebForm1.aspx?name=John&newsid=12
•
Query String passed to the page is
name=John&newsid=12
•
Changing the VaryByParam attribute:
<%@ OutputCache Duration=“15” VaryByParam=“Name” %>
•
•
Page will be cached according to the Name key.
Consider the following two URLs
http://localhost/Caching/WebForm1.aspx?name=John&newsid=12
http://localhost/Caching/WebForm1.aspx?name=John&newsid=45
•
Second page will be from cache (Cache will be
refreshed only if the Name value changes)
Output Caching – Example (Contd..)
•
•
•
•
Consider the following two URLs:
http://localhost/Caching/WebForm1.aspx?name=John&newsid=12
http: //localhost/Caching/WebForm1.aspx?name=John&newsid=45
For the page to be regenerated, Name and NewsID
keys have to be changed.
<% Output Cache Duration=“15” VaryByParam=“Name;NewsID” %>
This causes the second page to be refreshed.
Equivalent to using a *, which means change in any key
would regenerate the page.
<% Output Cache Duration=“15” VaryByParam=“*” %>
•
If the page has to be cached regardless of query string
‘none’ can be used.
<% Output Cache Duration=“15” VaryByParam=“none” %>
Fragment Caching
•
•
•
•
Caches regions of the page content.
More powerful than Output Caching.
This technique separates portions of a page that takes
more time to create (such as database queries) from
other parts of the page.
The part of the page that requires less system resources
can be generated dynamically for each request.
Fragment Caching - Example
•
•
Consider a Web Application that displays the news
titles in a Combo Box.
The dataset containing the news is cached in order to
minimize the number of times the application wants to
connect to the SQL server to retrieve the news.
Fragment Caching – Example (Contd ..)
•
•
•
•
First time the page loads, user clicks on the Get News button
to get the news from the SQL Server.
We assume that the news changes daily.
Page is refreshed each time the date changes.
The cached version is displayed to all the users visiting the
page on the same day.
Data Caching
•
•
•
Storing data in memory for quick access.
Items in the data cache will be evicted from memory, if
memory becomes scarce.
While adding items to a data cache, the duration of how
long it can persist can be specified.
Data Caching - Implementation
•
•
•
•
.NET data caching API is comprised of two classes in the
System.Web.Caching namespace.
The first class (Cache) is used to add and remove items
from the data cache.
The second class (Cache dependency) is used to assign a
cache dependency to an item in the data cache.
To add an item:
Cache[“key”] = value;
•
•
This adds the item value to the data cache with the key
key.
Key is used to reference the item at some later point.
Data Caching – Implementation (Contd ..)
•
To extract the value inserted above:
value = Cache.Get(“Key”)
•
To remove item from the cache:
Cache.Remove(“Key”)
Time based Caching
•
•
•
•
Caching based on time i.e. the data is available only for
a certain period of time.
Two ways to use:
- Absolute Expiration
- Sliding Expiration
Absolute Expiration – Cache is set to expire on a
particular date and time.
Sliding Expiration – Cache is set to expire after certain
period of inactivity.
Time based Caching - Implementation
•
•
•
•
Absolute Expiration
Cache.Insert(“News”,ds,null,DateTime.Now.AddMinutes(2),Cache.NoS
lidingExpiration)
The cache is set to expire exactly two minutes after the
user has retrieved the data.
Sliding Expiration
Cache.Insert(“News”,ds,null,Cache.NoAbsoluteExpiration,TimeSpan.Fr
omMinutes(1))
This causes the cache to be cleared if the user does not
reload the page within one minute.
Use Caching Sparingly
•
•
•
•
Caching has to be used sparingly.
Takes up valuable system resources and eats up
available memory.
When server runs out of memory, contents of cache will
be evicted.
Eviction will be based on the priority of the data in the
cache which can be set as shown:
Cache.Insert(“News”,ds,null,Cache.NoAbsoluteExpiration,TimeSpan.Fr
omMinutes(1),System.Web.Caching.CacheItemPriority.High,null)
•
There are seven levels of priority: NotRemovable, High,
AboveNormal, Default, Normal, BelowNormal, Low.
Conclusion
•
•
•
•
Effective way of increasing performance.
Minimizes the use of server resources.
Choosing the appropriate level for caching data is
important to balance caching versus memory usage.
Most effective strategy in Web Application – Cache data
only when necessary.
THANK YOU