Many ASP.NET applications today are high-traffic and are used by tens of thousands of users. These applications face performance issues because accessing the database is slow, especially when network traffic is high. This latency is now becoming unaffordable as thousands of users are accessing these applications.
To resolve this performance constraint, ASP.NET and ASP.NET Core provides a built-in caching mechanism called “ASP.NET Cache” that stores application data in memory for faster access and reduces database trips. Hence improving the performance of your application.
NCache Details ASP .NET Performance .NET Core
Built-In ASP.NET Caching Has Limitations
However, there is a problem that 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.
Here are some of the limitations of ASP.NET Cache:
- Multiple Cache Instances Not Synchronized: Since an ASP.NET cache is part of the ASP.NET app process, it does not allow synchronization of cache instances running on multiple ASP.NET apps. This causes data integrity issues.
- NET Worker Process Recycles: As the ASP.NET cache is in-process, 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: ASP.NET cache is a part of the ASP.NET app’s worker process. Hence, there’s limited memory of this process to use for caching.
Solution: ASP.NET Caching with Distributed Cache
To counter these limitations of ASP.NET caching, you need a distributed cache like NCache that 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 apps instances which solves your data integrity problem.
- Since it is an out-proc (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. It also allows you to have as many cache servers as you want.
- You can scale your ASP.NET app caching servers in real-time.
- NCache also has a highly intelligent system of data replication without any performance issues. The efficient data replication feature keeps you at ease without any data loss issues.
NCache Details ASP .NET Performance ASP.NET Core Performance
How to Use ASP.NET Caching with Distributed Cache?
NCache provides different types of caching that you can use to get your ASP.NET apps up and running with caching.
App Data Caching
App data that needs to be fetched frequently from your data source can be cached in your ASP.NET application using data caching to reduce response time for this frequently accessed data. 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 fetched 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); } |
NCache Details ASP.NET Caching ASP.NET Core Sessions
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 losing 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> ... |
NCache Details ASP.NET Session Caching ASP.NET Session Caching Docs
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> ... |
NCache Details ASP.NET View State Caching NCache Docs
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> ... |
NCache Details NCache Output Caching Docs ASP.NET Output Cache
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.