Uğur UMUTLUOĞLU
Web Technologies, Microsoft and .NET Technologies

Caching More Than One Table with SqlCacheDependency

Monday, 14 December 2009 19:31 by ugur

SqlCacheDependency object provides caching according to updates in database in ASP.NET applications. We can use this object which is independent from time and provides us important gains according to speed and performance, for caching records from one table. But, in most of the applications by using JOIN statements, we can match records of different tables. Well, how can we do caching operation by SqlCacheDependency object in this kind of situation? Because, depending on its structure, this object was envisioned to work with only one table. The answer of the question: AggregateCacheDependency object (If I am not wrong, comes with .NET Framework 2.0). AggregateCacheDependency object will remove the information of cache object stored in memory in case the state of one of the objects changes and will make caching operation successfully be done by keeping more than one CacheDependency objects inside. The only thing we must do is that adding SqlCacheDependency objects formed for each table to AggregateCacheDependency object by using Add method and adding this object to Cache by using Insert method. You can see how to do this operation below by following these code snippets.

SqlCacheDependency dep1 = new SqlCacheDependency("dbCon", "Products");

SqlCacheDependency dep2 = new SqlCacheDependency("dbCon", "Orders");

SqlCacheDependency dep3 = new SqlCacheDependency("dbCon", "Customers");

AggregateCacheDependency aggDep = new AggregateCacheDependency();

aggDep.Add(dep1,dep2,dep3);

Cache.Insert("ProductOrder", dt, aggDep);


SqlCacheDependency provides us to do caching operations in ASP.NET applications very efficiently. But, the important point that must be considered in caching operations is that if we store large amount of data in Cache and system resources aren’t sufficient, then OutOfMemoryException error might be occurred. So, both errors and to take up a lot of space in memory is not good for the performance of the application. Because of this reason, the size of the data that will be stored in Cache is an important criterion for us, we shouldn’t forget this detail.

 
Add to Technorati Favorites