More on CacheDependency 2.0
A traditional file-based cache dependency (like that available in ASP.NET 1.x) uses the file notification change mechanism and asynchronously receive an event when the file system detects a change to the timestamp of a particular file or folder.
In ASP.NET 2.0, a custom CacheDependency object will be responsible for detecting any changes that affect the controlled item(s). The behavior of the object depends on the characteristics of the invalidation mechanism of choice. For example, the SqlCacheDependency class uses a timer and polls a built-in, helper table for changes. When instantiated, the class does some work on the database that contains the table to monitor. This work includes creating an helper table to track changes and a trigger to detect changes to all the tables in the database.
- Administratively or programmatically configure the database (e.g., Northwind) to support notification. This entails creating a helper table and adding a trigger
- The helper table will contain one row per each table in the database to monitor
- Programmatically define the table to monitor (e.g., Employees)
The trigger writes to the helper table after each update operation that involves the monitored table(s).
The polling mechanism just looks for changes in the helper database using the table name as the key. The SqlCacheDependency class periodically accesses the helper table, reads a column that indicates if changes occurred, and invalidates the cached item.
When created, each SQL cache dependency object is made dependent upon an helper Cache key, whose name depends on the table and the database. When the SqlCacheDependency object detects a change in the monitored table, it deletes the helper Cache item thus invalidating the item in turn to the SQL Server table.
In the end, a custom cache dependency object is always bound to a helper cache entry that is created and maintained just to invalidate the cached data. And polling is essential for the success of the implementation.
Sounds a bit complex? This is what we need to know and do to implement a custom cache dependency object in ASP.NET 2.0.
Unless we're so fortunate to bind to a resource that has some sort of built-in notification framework. Want an example? SQL Server Yukon or a message queue system.
More in a moment...