Many ASP.NET applications today serve high-traffic environments, catering to tens of thousands of users. These applications often struggle with performance issues caused by slow database access, particularly when network traffic is heavy. This latency is becoming increasingly problematic as more users access these applications.
To resolve this performance constraint, ASP.NET and ASP.NET Core provides a built-in caching mechanism called “ASP.NET Cache“. This feature stores application data in memory, for faster access and reduces database trips, thereby enhancing the overall performance of your application.
Built-In ASP.NET Caching Has Limitations
However, the built-in ASP.NET Cache is a stand-alone, in-process cache that lives within your ASP.NET application’s worker process. As a result, it is only useful for a single server environment. Some other limitations of ASP.NET include:
- Multiple Cache Instances Not Synchronized: It does not allow synchronization of cache instances running on multiple ASP.NET applications. This causes data integrity issues.
- NET Worker Process Recycles: All cached data is lost when the ASP.NET worker processes are recycled. The cache then needs to be reloaded from a data source, causing a big performance hit.
- Limited Worker Process Memory Size: There’s limited memory for this process to use for caching.
Solution: ASP.NET Caching with Distributed Cache
To counter such limitations of ASP.NET caching, you need a distributed cache like NCache. It lives in its own process on multiple servers and also provides a mechanism to synchronize caches in a web farm.
Here’s how a distributed cache such as NCache solves your problems with ASP.NET caching:
- A distributed cache synchronizes all cache instances created by multiple ASP.NET application instances, resolving data integrity issues.
- Since it is an OutProc (out of process) cache, it can be shared by multiple servers and worker processes.
- High scalability allows you to use as much memory as you want since there is no process limitation.
- You can scale your ASP.NET app caching servers in real-time.
- The efficient data replication feature keeps you at ease without any data loss issues.
How to Use ASP.NET Caching with Distributed Cache?
NCache provides different types of caching that you can use to get your ASP.NET applications up and running with caching.
Application Data Caching
Frequent retrieval of application data from your data source can slow down your ASP.NET application. By using data caching, you can store this data locally, reducing response times and improving overall performance.
This includes your custom object data such as Product class objects, as shown in the example below. The Product object is fetched from the database for the first time and then added to the cache and retrieved from the cache, the next time the data is accessed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using Alachisoft.NCache.Client; ... ICache _cache = CacheManager.GetCache("demoCache"); string key = "Product:1001"; // Search for key in cache var result = _cache.Get<string>(key); // If it doesn't exist, fetch from DB and add to cache if (result == null) { var product = LoadProductFromDB(1001); CacheItem item = new CacheItem(product); _cache.Add(key, item); } |
ASP.NET Session State Caching
You can use session caching to store user relevant data for your ASP.NET applications. Session data belongs to user interactions on your ASP.NET app. For example, an e-commerce business cannot afford to lose sessions in case the ASP.NET cache goes down. Hence, you can plug in NCache to your ASP.NET application to prevent data loss.
To use NCache for ASP.NET caching, you need no programming effort. Simply add the following configuration to Web.config of your application:
1 2 3 4 5 6 7 8 9 |
... <assemblies> <add assembly ="Alachisoft.NCache.SessionStoreProvider, Version=x.x.x.x, Culture=neutral, PublicKeyToken=CFF5926ED6A53769"/> </assemblies> ... |
Modify session state configuration to enable session state caching in ASP.NET. In web.config add the following section:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<configuration> ... <sessionState cookieless="false" regenerateExpiredSessionId="true" mode="Custom" customProvider="NCacheSessionProvider" timeout="20"> <providers> <add name="NCacheSessionProvider" type="Alachisoft.NCache.Web.SessionState.NSessionStoreProvider" cacheName="demoCache" sessionAppId="NCacheApp" exceptionsEnabled="true" writeExceptionsToEventLog="false" enableLogs="false" enableSessionLocking="true" sessionLockingRetry="-1" emptySessionWhenLocked="false" /> </providers> </sessionState> ... </configuration> ... |
ASP.NET View State Caching
ASP.NET View State provides client-side state management mechanism. It helps preserve page and control values between complete round trips for client requests. You can store ASP.NET view state on the web server and send a unique ID back to the browser. This ID will find the right ASP.NET view state in the cache.
Achieving ASP.NET view state caching in NCache is very easy. Here’s a part of the configuration that needs to be added to Web.config of your ASP.NET application:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
... <ncContentOptimization> <settings viewstateThreshold="12" enableViewstateCaching="true" enableTrace="false" groupedViewStateWithSessions="false" maxViewStatesPerSession="5" > <cacheSettings cacheName="demoCache"> <expiration type="None" duration="100" /> </cacheSettings> </settings> </ncContentOptimization> ... |
ASP.NET Output Caching
For web pages that are being frequently accessed, you can use output caching to improve response times for these specific pages. ASP.NET’s output caching system caches the different versions of pages’ content depending on the various parameters like query string parameters and browser type.
You can enable Output caching with NCache in your ASP.NET application without any code change and simply plugging in the following in Web.config:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
... <caching> <outputCache defaultProvider ="NOutputCacheProvider"> <providers> <add name="NOutputCacheProvider" type= "Alachisoft.NCache.OutputCacheProvider.NOutputCacheProvider, Alachisoft.NCache.OutputCacheProvider, Version=x.x.x.x, Culture=neutral, PublicKeyToken=cff5926ed6a53769" cacheName="demoCache" exceptionsEnabled="true"enableDetailLogs="false" enableLogs="true" writeExceptionsToEventLog="true"/>" </providers> </outputCache> </caching> ... |
Conclusion
To sum it up, a distributed cache like NCache is highly scalable, reliable and performance optimized to handle caching in ASP.NET. It has all the necessary features to overcome all the limitations of ASP.NET caching and is the only distributed cache that can handle ASP.NET caching on its own without you worrying about it. With all flavors of caching available with NCache, you can easily cache any type of data your application requires, by ensuring a boost in performance.
Very good article, Ron. The ASP.NET caching is explained in detail. The distributed cache NCache is reliable and gives a better performance for ASP.NET. It helps to overcome the limitations of ASP.NET caching. The article gives a clear picture of how the distributed caching can be done.