Hi readers,
The previous post involved an overview of ASP.NET Cache with special emphasis on the types of caching available and the main operations that can be performed on the cache. In today’s post we’ll be discussing some important features related to the ASP.NET Caching mechanism and their significance.
But first, I wanted to focus on the in-process (InProc) nature of ASP.NET Cache as this is a prominent characteristic. As discussed in the previous post, the Cache instance is created within the Application scope and resides within the application’s worker process memory. This results in extremely fast Cache access since data does not have to travel across process boundaries or across the network to access the database. Additionally, no serialization and deserialization cost is incurred by the application. For stand-alone single server environments, this is God-send.
As specified in the previous post, data items are not supposed to be kept in the cache indefinitely. They are only temporarily stored there due to many reasons as explained below:
- The available memory for the Cache is limited to the memory size of the application processes. Therefore, if memory starts getting scant, the Cache starts throwing items out – based on a priority that was set initially or if the items have not been used recently – to compensate for the loss of memory. This is called scavenging.
- New items have to be brought in because they are needed by the application.
- Data in the Cache can also get out of sync with the database where another application or user might update it. Therefore, in most situations, applications choose to keep data in the cache for a limited period of time. And, after this time is up, the data is automatically removed from Cache. This is called expirations.
- Cached data might be related to other data either inside the cache or a file in the file system. This relationship is captured by the ASP.NET Cache through (dependencies). This allows a cached item to be removed from the cache automatically if whatever it was depending on is updated or removed.
- Keeping the above requirements in view, the ASP.NET Cache class also has features that make it possible for you to determine the way data items are cached and the amount of time they would remain there.
Scavenging:
When you have an in-proc cache and you do not want it to be too much of a burden on your memory resources, you have to be able to proactively look for items that you can retire or take out of the cache. At the time of insertion, ASP.NET allows you to specify the priority of an item as high or low by using the CacheItemPriority object. When the memory becomes low due to too much items being added (or some other reason), the items with low priority can be removed from cache.
Expirations:
In the same manner, items in the Cache become “stale” meaning they may become out of sync with the database because somebody else changed the corresponding data in the database. So, you specify an expiration period after which the cached item is automatically removed from the cache. ASP.NET Cache offers you two options when it comes to expirations:
2.1 Fixed time:
At the time of its addition, a certain item can be pre-determined to stay inside cache for a fixed amount of time. It is automatically ousted from cache when that time is up. The fixed time for example can be set to 5 minutes and after that a data item, no matter how many times it has been accessed in the past, whether recently or a long time ago, is forcibly taken out.
2.2 Sliding time (Idle time):
If a data item that resides inside cache sits idle for a certain amount of time, it can be retired from cache using this option. It basically means that the mechanism has to wait for a specified amount of time after a data item was last accessed and withdraw it from Cache. The most common analogy that can be given is that of a session which expires after a set amount of time if no activity takes place.
Notification of Item Removal From Cache:
This neat feature allows you to be notified in case a data item is removed from cache that besides being frequently accessed takes a lot of computing to be created. Once your application knows that it has been removed, it can take an appropriate action including reloading it from the database and adding it back into the cache.
Dependencies:
There are certain instances where you want an item in cache to be removed if another related cached item, a file, or a database record was updated or removed. This is done to ensure that the cached item is always correct. ASP.NET provides various dependencies that help you achieve this by removing the cache item whenever a corresponding cached item, a file, or a row in the database changes. Since most data is relational, you can easily maintain data integrity in this manner.
The various types of dependencies are discussed below:
4.1 SQL dependency:
ASP.NET contains a SqlCacheDependency class that helps you set up a data item inside cache to be dependent on some database table or row (record). The table could belong to a SQL Server 7, SQL Server 2000, or SQL Server 2005 database. However, only SQL Server 2005 has the option of selecting a dependency on a certain row or record. For other databases, you must select an entire table to depend on.
An example of such a dependency is a stock reporting web page having a data item dependent on a database record. Whenever that database record is altered – meaning a field’s value is changed – the cache removes the dependent entry. The next time it is required, it has to be freshly created by loading it from the database again. This besides keeping the cache current also helps to synchronize it with your database (a concept we’ll be discussing in detail in the limitations section). And if the Cache is current, your web application can quickly utilize it to generate the report instead of having to make frequent database trips.
A greater synchronization effort is required when you have a web garden where multiple processors are incorporated into a single server and most importantly in the case of a web farm where your application is running on a number for web servers simultaneously.
It must be mentioned here briefly (as it will be covered in detail in the limitations category of this web log) that in case of web gardens you can only synchronize the various caches with the data store and not among themselves. Similarly, web farms where the need for synchronization and freshness is exponentially enhanced since the application’s scalability, availability and performance heavily depend on it; ASP.NET cache does provide an up-to date cache using the above mentioned dependency but fails to directly synchronize the various caches with each other across the different web servers. As mentioned earlier, this would be covered in great detail in the limitations section of this blog.
4.2 File Dependency:
You can specify a cache item to be dependent on a file. As a result, whenever the file in question is removed or altered, the cache item that depends on it is removed automatically helping to keep the cache current.
4.3 Key Based Dependency:
This is an excellent way of handling relationships among items that are cached. It allows you to mention that one cache item is dependent on another item in the cache. Remember when placing a data item in cache you have to specify a key and give its value. When the original data item is removed from Cache, the data item which depends on its key would also be removed.
For example, you are caching customer objects and order objects that belong to a specific customer. If a certain customer object is removed from cache, all the orders that are associated with it that reside inside the cache would also be removed.
4.4 Other Dependencies:
There are other types of cache dependencies that can be created. ASP.NET also allows you to create Custom Cache Dependencies if such a need arises in your application. You can write code and make a cache item dependent on other elements.
There is also something called an Aggregate Dependency that can be used to make a cached item dependent on severak elements by using the AggregateCacheDependency class. If any of the elements or dependencies is altered, the cached item is removed.
To recap, we have tried to discuss some main features that make ASP.NET Cache an appealing choice for .NET applications that require performance enhancements. We went through the INProc nature of ASP.NET Cache, its various policies like notifications, expirations and dependencies. In my opinion, for single server, standalone applications, ASP.NET Cache is a wonderful addition and goes a bang up job of making it perform faster.
However, my coming posts are going to highlight the various limitations that it has when considered for web gardens or for multi-server environments such as web farms. For a sneak peak of what’s to come please visit www.alachisoft.com
Until next time!!!