SlideShare a Scribd company logo
Cache
Caching in ASP.NET is a very useful feature. It allows you to build complex and
sophisticated applications without worrying too much about performance issues.
Basically it saves already rendered HTML of your website, controls or some other
objects in memory and later when they are needed by client's browser they are
downloaded directly from cache without a need to generate them again. If data
requested by client actually is in cache then it's called a cache hit. The more
cache hits the better because then your application simply works faster. The
number of cache hits depends of course on the size of the memory and the
implementation of caching.In ASP.NET there are three types of caching:
Page Level Caching - it's simply caching whole HTML code of generated
pageFragment Caching - caching small fragments of your website like for example
user controlsCaching Data Objects - allows you to cache objects from your
application such as a table for instance Now I'll describe briefly all three
kinds of caching:

PAGE LEVEL CACHING

  In this type of caching we want to save whole web page. To do so in ASP.NET is
very easy, all you have to do is to put the following directive in your code:
view plaincopy to clipboardprint?
<%@ OutputCache Duration="30" VaryByParam="*" %>
The parameters used in this directive are the following (both are required):
Duration - it defines time in seconds for how long the page should be in
cacheVaryByParam - parameters for which the page should be cached separately,
value "none" means that there will be one page cached for all parameters, value
"*" means that for each parameter the page will be cached separately. Each
parameter should be separated using semicolon character (;)And that's all you
have to do to cache the whole page. As you see it's quite easy but it can
provide huge performance gains depending on the content of your page.
FRAGMENT CACHING
Instead of caching whole website there is also a possibility to cache only a
fragment such as a control. Implementation is exactly the same as for caching
the whole page. The only difference is that OutputCache directive should be
placed within a given control.While using caching for a user control you can
also use parameter VaryByControl with value being the name of some user-control
on your web page. Then different options of this control (like for example
different choices in drop down list) will be cached separately.
view plaincopy to clipboardprint?
<%@ OutputCache Duration="60" VaryByParam="none"
VaryByControl="CategoryDropDownList" %>
CACHING DATA OBJECT
You can also save data to cache simply by using cache object, it's working like
the standard dictionary with key-pairs value. It's best to show this on an
example. Take a look how to add some data to cache:
view plaincopy to clipboardprint?
Cache["name"] = "Steve";
Retrieving data is equally simple:
view plaincopy to clipboardprint?
if (Cache["name"] != null)
{ Label1.Text = Cache["name"].ToString();
}
Please note that firstly we have to check if data stored in cache still exists,
it could have been deleted by client's browser.
Session
Session state is simply a dictionary with key-value pairs and you can read or
write it whenever you need during duration of a session. Session is a time when
one concrete user interacts with your web application. Session state unlike
application state is separate for each user. Using session state is very easy,
you can for example save data from a user control to it:
view plaincopy to clipboardprint?
Session["name"] = TextBox1.Text;
Then you can use this data in any other part of your application simply by
referring to it like in the following example:
view plaincopy to clipboardprint?
string welcomeString = "Hello " + Session["name"];
One of the biggest advantages of session state in ASP.NET is that they are
process independent, that means that session state can be saved in separate
process so other processes can't affect it. Another advantage is that it also
can work without cookies enabled in client's browser.If you want to learn more
about types of sessions check out our previous tutorials.
Cookies
Cookies are small files saved on client's disc used to store data concerning a
concrete user. It can for example contain some preferences that user have chosen
before so later the same preferences can be applied instantly. You should keep
in mind that you don't have complete control over cookies. Some browsers may
accept cookies smaller than some size or even they may not accept cookies at
all. What's more user can always delete all cookies from their browser so you
shouldn't rely on them. To create a cookie we'll use HttpCookie class like in
the following example:
view plaincopy to clipboardprint?
HttpCookie cookie = new HttpCookie("user");
cookie["name"] = "John";
Response.Cookies.Add(cookie);
And now take a look at code that will allow you to read your cookie:
view plaincopy to clipboardprint?
if (Request.Cookies["user"] != null)
{ if (Request.Cookies["user"]["name"] != null) { Label1.Text =
Request.Cookies["user"]["name"]; } }
COMPARISON BETWEEN CACHING, SESSIONS AND COOKIES
You should already have an idea about differences between these techniques but
I'll present here a short summary. Choosing the right method of storage is very
easy and straightforward if you follow few basic rules:
WHEN TO USE CACHING
Cache is shared between all users of a given web application, that's why you
should not store there data concerning individual users. You should use it to
store whole web pages, controls or complex objects to enhance performance of
your application.
WHEN TO USE SESSION
Session state unlike cache is separate for each session so you should store
there data concerning only current session. Each session is valid only for one
user so data stored there can't be accessed by other user's application. You can
treat it as a storage for user data. Session state can also be store externally
(for example in the database).
WHEN TO USE COOKIES
Cookies should be used to store only data concerning user. What's more this data
should be persistent like for example login name or some setting from your web
application.
SUMMARY
In this tutorial I have described caching, session state and cookies and
explained also how to use them. It can be very useful in web design. As you see
in ASP.NET it's quite easy and does not require much work. After reading the
last part of this article choosing which technique of storing data to use should
not be a big deal for you.T

More Related Content

DOCX
State management
PPTX
Caching in Backbone Application
PDF
Html5 web storage
ODP
Caching idea for midcom
PPTX
Offline db
PPTX
Advance Java
PPTX
State management
PPTX
State management
Caching in Backbone Application
Html5 web storage
Caching idea for midcom
Offline db
Advance Java
State management

What's hot (15)

PPTX
State management
PPT
Drupalcamp Estonia - High Performance Sites
PPTX
APEX & Cookie Monster
PDF
Manual trial storage cloud services
PDF
Database Multitenancy in Ruby
PDF
Link Header-based Invalidation of Caches
PPTX
ASP.NET State management
PPTX
State Management in ASP.NET
PPTX
13 deploying cloud applications
PPTX
Session and cookies,get and post
PPTX
ASP.NET Lecture 4
PPTX
Sessions&cookies
PDF
ASP.NET-Web Programming - Sessions and Cookies
PPTX
Web storage
PPT
Session and state management
State management
Drupalcamp Estonia - High Performance Sites
APEX & Cookie Monster
Manual trial storage cloud services
Database Multitenancy in Ruby
Link Header-based Invalidation of Caches
ASP.NET State management
State Management in ASP.NET
13 deploying cloud applications
Session and cookies,get and post
ASP.NET Lecture 4
Sessions&cookies
ASP.NET-Web Programming - Sessions and Cookies
Web storage
Session and state management
Ad

Similar to Cache (20)

PDF
WEB MODULE 5.pdf
PDF
WEB Mod5@AzDOCUMENTS.in.pdf
PPTX
PPTX
Caching in Kentico 11
PPTX
Ror caching
DOCX
Why use .net by naveen kumar veligeti
PDF
How to optimize your Magento store
PDF
C sharp and asp.net interview questions
PPSX
05 asp.net session07
PPTX
Caching and Its Main Types
PPTX
Improving Drupal Performances
PPT
IEEE KUET SPAC presentation
PDF
Introducing windows server_app_fabric
PDF
Browser Caching
DOCX
High performance coding practices code project
PDF
FLOWER SHOP BILLING MANAGEMENT SYSTEM PROJECT REPORT
PPTX
Sharepoint Performance - part 2
PDF
Web technology and commerce unit 2
PPT
Session,cookies
WEB MODULE 5.pdf
WEB Mod5@AzDOCUMENTS.in.pdf
Caching in Kentico 11
Ror caching
Why use .net by naveen kumar veligeti
How to optimize your Magento store
C sharp and asp.net interview questions
05 asp.net session07
Caching and Its Main Types
Improving Drupal Performances
IEEE KUET SPAC presentation
Introducing windows server_app_fabric
Browser Caching
High performance coding practices code project
FLOWER SHOP BILLING MANAGEMENT SYSTEM PROJECT REPORT
Sharepoint Performance - part 2
Web technology and commerce unit 2
Session,cookies
Ad

Cache

  • 1. Cache Caching in ASP.NET is a very useful feature. It allows you to build complex and sophisticated applications without worrying too much about performance issues. Basically it saves already rendered HTML of your website, controls or some other objects in memory and later when they are needed by client's browser they are downloaded directly from cache without a need to generate them again. If data requested by client actually is in cache then it's called a cache hit. The more cache hits the better because then your application simply works faster. The number of cache hits depends of course on the size of the memory and the implementation of caching.In ASP.NET there are three types of caching: Page Level Caching - it's simply caching whole HTML code of generated pageFragment Caching - caching small fragments of your website like for example user controlsCaching Data Objects - allows you to cache objects from your application such as a table for instance Now I'll describe briefly all three kinds of caching: PAGE LEVEL CACHING In this type of caching we want to save whole web page. To do so in ASP.NET is very easy, all you have to do is to put the following directive in your code: view plaincopy to clipboardprint? <%@ OutputCache Duration="30" VaryByParam="*" %> The parameters used in this directive are the following (both are required): Duration - it defines time in seconds for how long the page should be in cacheVaryByParam - parameters for which the page should be cached separately, value "none" means that there will be one page cached for all parameters, value "*" means that for each parameter the page will be cached separately. Each parameter should be separated using semicolon character (;)And that's all you have to do to cache the whole page. As you see it's quite easy but it can provide huge performance gains depending on the content of your page. FRAGMENT CACHING Instead of caching whole website there is also a possibility to cache only a fragment such as a control. Implementation is exactly the same as for caching the whole page. The only difference is that OutputCache directive should be placed within a given control.While using caching for a user control you can also use parameter VaryByControl with value being the name of some user-control on your web page. Then different options of this control (like for example different choices in drop down list) will be cached separately. view plaincopy to clipboardprint? <%@ OutputCache Duration="60" VaryByParam="none" VaryByControl="CategoryDropDownList" %> CACHING DATA OBJECT You can also save data to cache simply by using cache object, it's working like the standard dictionary with key-pairs value. It's best to show this on an example. Take a look how to add some data to cache: view plaincopy to clipboardprint? Cache["name"] = "Steve"; Retrieving data is equally simple: view plaincopy to clipboardprint? if (Cache["name"] != null) { Label1.Text = Cache["name"].ToString(); } Please note that firstly we have to check if data stored in cache still exists, it could have been deleted by client's browser. Session Session state is simply a dictionary with key-value pairs and you can read or write it whenever you need during duration of a session. Session is a time when one concrete user interacts with your web application. Session state unlike application state is separate for each user. Using session state is very easy, you can for example save data from a user control to it: view plaincopy to clipboardprint? Session["name"] = TextBox1.Text; Then you can use this data in any other part of your application simply by
  • 2. referring to it like in the following example: view plaincopy to clipboardprint? string welcomeString = "Hello " + Session["name"]; One of the biggest advantages of session state in ASP.NET is that they are process independent, that means that session state can be saved in separate process so other processes can't affect it. Another advantage is that it also can work without cookies enabled in client's browser.If you want to learn more about types of sessions check out our previous tutorials. Cookies Cookies are small files saved on client's disc used to store data concerning a concrete user. It can for example contain some preferences that user have chosen before so later the same preferences can be applied instantly. You should keep in mind that you don't have complete control over cookies. Some browsers may accept cookies smaller than some size or even they may not accept cookies at all. What's more user can always delete all cookies from their browser so you shouldn't rely on them. To create a cookie we'll use HttpCookie class like in the following example: view plaincopy to clipboardprint? HttpCookie cookie = new HttpCookie("user"); cookie["name"] = "John"; Response.Cookies.Add(cookie); And now take a look at code that will allow you to read your cookie: view plaincopy to clipboardprint? if (Request.Cookies["user"] != null) { if (Request.Cookies["user"]["name"] != null) { Label1.Text = Request.Cookies["user"]["name"]; } } COMPARISON BETWEEN CACHING, SESSIONS AND COOKIES You should already have an idea about differences between these techniques but I'll present here a short summary. Choosing the right method of storage is very easy and straightforward if you follow few basic rules: WHEN TO USE CACHING Cache is shared between all users of a given web application, that's why you should not store there data concerning individual users. You should use it to store whole web pages, controls or complex objects to enhance performance of your application. WHEN TO USE SESSION Session state unlike cache is separate for each session so you should store there data concerning only current session. Each session is valid only for one user so data stored there can't be accessed by other user's application. You can treat it as a storage for user data. Session state can also be store externally (for example in the database). WHEN TO USE COOKIES Cookies should be used to store only data concerning user. What's more this data should be persistent like for example login name or some setting from your web application. SUMMARY In this tutorial I have described caching, session state and cookies and explained also how to use them. It can be very useful in web design. As you see in ASP.NET it's quite easy and does not require much work. After reading the last part of this article choosing which technique of storing data to use should not be a big deal for you.T