Caching is a technique of
application performance improvement by storing frequently used data/information
in memory, so that, when the same data/information is needed next time, it
could be retrieved from the memory instead of being generated by the application.
This information get stored in web server, any
proxy servers, or web browser, depending on type of caching and configuration.
ASP.NET
provides following different types of caching:
1. 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.
By default, when you use the [OutputCache] attribute,
content is cached in three locations: the web server, any proxy servers, and
the web browser. You can control exactly where content is cached by modifying
the Location property. By default, the Location property has the value Any, It
can be changes to Client, Downstream, Server, none, ServerAndClient.
[OutputCache] attribute includes a Location
property set to the value OutputCacheLocation. Also includes a NoStore
property. The NoStore property is used to inform proxy servers and browser that
they should not store a permanent copy of the cached content.
Asp.NET Example:-
<%@ OutputCache Duration="15" Location="Client" VaryByParam="None" NoStore=" true" %>
MVC Example:-
[OutputCache(Duration=3600, VaryByParam="none", Location=OutputCacheLocation.Client, NoStore=true)]
VaryByParam: - This
property enables you to create different cached versions of the very same
content when a form parameter or query string parameter varies. When
different values of the Id parameter are passed to the controller action,
different cached versions of the Details view are generated.
The OutputCache directive has the following
attributes, which helps in controlling the behaviour of the output cache:
Attribute
|
Values
|
Description
|
DiskCacheable
|
true/false
|
Specifies
that output could be written to a disk based cache.
|
NoStore
|
true/false
|
Specifies
that should or not store a permanent copy of the cached content.
|
CacheProfile
|
String
|
Name
of a cache profile as to be stored in web.config.
|
VaryByParam
|
None,*,Param
|
Semicolon
delimited list of string specifies query string values in a GET request or
variable in a POST request.
|
VaryByHeader
|
*,Header
|
Semicolon
delimited list of strings specifies headers that might be submitted by a
client.
|
VaryByCustom
|
Browser,Custom string
|
Tells
ASP.NET to vary the output cache by browser name and version or by a custom
string.
|
Location
|
Any, Client, Downstream, Server, None
|
Define cache storing location.
|
Duration
|
Number
|
Number
of seconds the page or control is cached.
|
2. Data Caching: It 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, data is obtained by the data source and the cache is
refilled.
To implement set the
EnableCaching attribute of the data source control to be 'true' and set the
Cacheduration attribute. It will implement caching and the cache will expire
defined seconds. The timestamp changes with every refresh, but if you change
the data in the table within defined seconds, it is not shown before the cache
expires.
3. Object Caching: Object caching is caching the objects on a page, such as
data-bound controls. The cached data is stored in server memory. This will
offer more flexibility while caching.
Example:-
Cache["key"] = item;
OR
Cache.Insert("item", obj, null, DateTime.MaxValue, TimeSpan.FromMinutes(10));
Inserts an item into the
cache with the key name, value, expiration policy, expiration policy and other
details
4. 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.
5. Configuration Caching: Application wide configuration information is stored in a
configuration file. Configuration caching stores the configuration information
in the server memory.
Comments
Post a Comment