There are 3 types of
caching in ASP.NET:
- Output Caching
- Fragmentation Caching
- Data Caching
We implemented
"Data Caching" in Cost and scheduling tool.
Caching takes place where
frequently-used data is stored so that the application can quickly access the
data rather than accessing the data from the source. Caching can improve the
performance and scalability of the application dramatically and can help us to
remove the unnecessary requests to the external data sources for the data that
changes infrequently.
This is the simplest way of
caching. In this technique, cache is stored in the memory of the local Web
Server.
An in-memory cache is stored
into the Server memory which is hosting the ASP.NET application. In the case of
Web farm (where the application is hosted on multiple servers) or cloud hosting
environments, all servers may have different values of in-memory cache. So,
in-memory caching cannot be used in web farm or cloud hosting environments. For
this type of environment, distributed cache is best suited.
Session vs Caching
The first main
difference between session and caching is: a session is per-user based but
caching is not per-user based, so what does that mean? Session data is stored
at the user level but caching data is stored at the application level and
shared by all the users. It means that it is simply session data that will be
different for the various users for all the various users, session memory will
be allocated differently on the server but for the caching only one memory will
be allocated on the server and if one user modifies the data of the cache for
all, the user data will be modified.
Create Cache
using System.Runtime.Caching;
MemoryCache memoryCache = MemoryCache.Default;
Assign data to cache
CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
cacheItemPolicy.AbsoluteExpiration =DateTime.UtcNow.AddMinutes(15);
memoryCache.Add(new CacheItem("AllFilterData",
allTablesForFilter.Value), cacheItemPolicy);
Read data from Cache
if (memoryCache.Contains("AllFilterData"))
{
return (AllTablesForFilter)memoryCache.Get("AllFilterData");
}
Comments
Post a Comment