A cache is made up of a pool of entries. Each entry has data along with a tag, which specifies the identity of the data in the backing store of which the entry is a copy.
In real life, Cache is very simple. If you take the life cycle of an ant, it reserves the food during the summer days for the usage of winter. In a similar line, IT system reserves the frequent usage content for the future request/kit to avoid the redundant operation.
When the cache client (a CPU, web browser, operating system) needs to access the data presumed to exist in the backing store, it first checks the cache. If an entry can be found with a tag matching that of the desired data, the data in the entry is used instead. This situation is known as a cache hit.
CPUs and hard drives frequently use a cache, as do web browsers and web servers. Let's see the caching mechanism at Core level.
Caching at Core Level
A CPU cache is a cache used by the central processing unit of a computer to reduce the average time to access memory. The cache is a smaller, faster memory which stores copies of the data from the most frequently used main memory locations. As long as most memory accesses are cached memory locations, the average latency of memory accesses will be closer to the cache latency than to the latency of main memory. Core/Primary cache is the fastest form of storage. Because it's built in to the chip with a zero wait-state (delay) interface to the processor's execution unit, it is limited in size. With this concept, 3 levels of Core Caching is represented from the OS point of view.
There are few popular Caching algorithms based on key parameters (like hit rate, recent usage, frequent usage). The most popular ones are Belady optimal algorithm, Least recent used (LRU), Random Replacement, Most recently used (MRU), Multi Queue Caching, Segmented LRU, etc.
With these fundamentals, ASP.NET Framework enables programmers to take advantage of caching without requiring to write a lot of code to deal with the complexities of caching, such as cache expiration, updates, and memory management. There are two different types of caching in ASP.NET:
· Application caching
· Page output caching
Application caching is nothing but a collection of in memory objects. It's automatically deallocated based on memory limitation, time limit and few other vital dependencies. It's also termed as application data caching because the scope of these in memory objects are at the application level. In ASP.NET web based context, this cache object exists for the entire web application, can be shared across the user sessions and web requests.
In simple terms, Application caching can be called as Global Data Container (GDC). As the name stands, Global Data Container is in the memory storage box to hold the shared data across the application.
A similar concept is extended to the latest computing, i.e., Cloud Computing. Microsoft Azure platform executes the application caching to improve the system performance. The concept is drafted in the below diagram:
Storing into Application Cache
ASP.NET provides built in Cache object from
namespace. This object contains '
' method to store any collection into the application cache.
Let's see the key parameters for
· This is the primary name to access the cached object in the
collection. The key must be unique in the cache.
· This is the data as an
that you want to cache.
· It is the associated item for
. Framework will trigger the signal during the changes in this dependency object.
· This is the time as a
object at which the object should be removed from the cache.
· This is the time as a
object after which the object should be removed from the cache if it has not been accessed by a user.
· This is a
enumeration value that you can use to determine which objects are removed first during scavenging (i.e., when memory starts to run low). Lower priority objects are removed sooner.
· This is an event handler that is called when the object is removed from the cache. This can be
if you don’t want to specify a callback method.
is the namespace supplied by ASP.NET Framework to manipulate the caching operations. It has been included in the first line.
content is getting loaded in
state of the page.
storage functionality starts from the fourth line of the source code.
Based on the working knowledge with
or similar objects,
object would be similar. Programmers can assign items directly to the cache by giving them a name (key) and assigning them an object (value).
In the fourth line of the above code, Company Name is stored in the cache with the key '
object is the global container object suppliant by Framework at application level. In the similar way, '
' and '
' info are stored in global
By using the
method, add an item to the cache and control how that item gets removed from the cache. This functionality includes automatic removal based on a specific period of time.
has a number of overloads based on the many parameters you can set when adding an item to the cache.
key insert method reserves the content from
, for the next 8 hours. Later,
cache automatically expires.
In ASP.NET Framework, a cache dependency links a cached item to something else such as a file or another item in the cache. ASP.NET monitors the dependency and invalidates the cache if the dependent item changes. In the above code sample, cache dependency is based on file. When this dependent feed file of the cache get updated, the object is removed from the cache. Apparently, we can use '
' delegate to execute the operation on removal event.
Reading from Application Cache
It's a very easy job to retrieve the item from application cache. It's very similar to other collection objects. Programmer needs to pass the key to retrieve the value.
Objects can be retrieved from the cache by checking for the key. In the above example, Company Name is retrieved from
is refreshed with the loaded data. In the similar way, Company Symbol and CEO info are read from
global application object and the retrieved info are assigned in the appropriate label controls.
It is good practice to always verify that an item is not
. If a value is
, that value either hasn’t been cached or it has expired from the cache. If an item is
, you should retrieve it from the original source and, in most cases, place it into the cache.
Page Output Caching
In general, web browser often keeps a copy of the page on the local computer. On next time user requests, the browser simply verifies that the cached version is still valid, and then displays the cached page to the user. This methodology improves the responsiveness using the client side efficiency.
With page output caching concept, ASP.NET can keep a copy of a rendered ASP.NET webpage in memory on the server. If web requests come from the same user or different user, ASP.NET can return the page almost instantly. Let's assume the same page takes a long time to render due to the process intensive like heavy business logic, multiple database queries, etc. ASP.NET page output caching improves significant performance and reduces rendering time. If you have a lot of activity on your server, it can also increase your scalability, because resources used to retrieve data can be freed.
Execution of page output caching is drawn as:
Declarative Page Output Caching
To enable Page Output Caching, ASP .NET provides declarative approach at each page in your site. Using this approach, the programmer will get the granular control over which pages get cached and how they get cached. It's possible by adding the
directive to the top of a page’s markup. Programmers can configure this directive by using the below attributes.
· The number of seconds to cache the page. It's the only mandatory parameter.
· One of the
enumeration values, such as
· The name of the cache settings to associate with the page.
· Flag to determine whether to prevent secondary storage of sensitive information.
· Flag to determine whether user control output can be shared with multiple pages. The default is
· A semicolon-separated list of
s used to vary the output cache.
· A semicolon-separated list of
s used to vary a user control’s output cache.
· It identifies a set of database and table name pairs on which a page or control’s output cache depends.
The above code example demonstrates how to cache a page for 20 minutes, regardless of the parameters passed to the page. At the end of this time span,
get expired automatically.
If the page might display differently based on parameters, provide the names of those query
parameters in the
attribute. The above code example caches a different copy of the page for different values provided in the marketprice query
In a similar line, other attributes of
directive performs the declarative page output caching process in ASP.NET Framework.
Advantages of Caching
· In terms of advantage,
concept can make or break the performance of your computer system. Smart buffer algorithm will make the site loading faster than traditional approach.
takes the heavy load / execution from the server for the repeated operations. By doing so, Servers can be efficiently used.
Disadvantages of Caching
· In terms of Caching approach, it will not be coherent with the actual data content in the delayed writes.
isn't always the freshest information.
Points of Interest
Hope it will be interesting to also know the core cache concepts and ASP.NET caching features, instead of just the regular abstracted code level implementation.