ASP.NET State Management And Caching
In this tutorial you will learn about Cache Dependency, SqlCache Dependency, New methods added to the CacheDependency Class and The process of writing the cache dependency file.
The performance of any web application is incumbent upon the amount of server side processing that is required. Web servers must handle individual requests or multiple requests, give quick response time and reduce the load on intermediate and backend data systems. Output caching is regarded as one of the means of reducing server workload. It allows ASP.NET to send its most recent copy of the page to the browser rather than rerunning the code, querying the database and reassembling the page for every request. Cache can be defined as a hashtable used to store frequently accessed data. The data stored is global to the application and is visible within each currently active session.
Application, session and cache have similar structure and API. However, Session is a block of memory set aside for each user while Cache manages globally accessible data. Application and Cache both share application scope but differ from each other in the kind of support they give to item dependencies. An object added to the cache can be configured with file based, key based, time based dependencies. If the associated key changes or the time period expires, the object gets automatically removed from the cache. When the object is removed an event is triggered and code can be written to run on that event and load an updated version to Cache.
Cache is a server based approach to preserving state in an ASP.NET application. The dependency mechanism is encapsulated in the CacheDependency class. This class can represent a single file or directory or an array of files and directories or even items logically related to a particular item and added to the cache. The dependency between a cached item and an external component the developer has to use a specific overload of the Insert method or the Add method.
CacheDependency dep=new CacheDependency(filename);
Cache.Insert(Key.Value.dep);
As stated earlier, if the specified file changes the cached item is automatically removed. This is because the file dependency is based on a file monitor object which is an instance of the FileSystemWatcher class. This is a class that is a managed wrapper around the Windows operating system feature—the file notification change functionality.
Cached items can be bound to other cached items in addition to file dependencies that are created. The item is subordinate to files and folders and arrays of keys. When either of them changes, the item will be invalidated and removed from the cache. If the developer wants to make the item dependent only on the Cache, the file name parameter will have to be set to Null. A number of different combinations with Cache items are supported in ASP.NET 2.0 to create effective dependencies between cached items and other elements. A Cache dependency can be made subordinate to other cache dependencies. This feature comes handy when changes have to be cascaded. Finally ASP.NET 2.0 supports customization of dependencies by making the CacheDependency class inheritable and providing a made to measure SqlCacheDependency cache that provides built in database dependency limited to SQL Server 7.0 and later.
Designing a Cache dependency file is not an easy task. Moreover, the developer must have a very good reason for writing one. Nevertheless, ASP.NET 2.0 makes the process of writing the cache dependency file as comfortable as possible.
1. The CacheDependency class is inheritable
2. The CacheDependency class picks up all of the base class functionality including constructors that accept array files or create dependencies for other cache items. The developer can review and retain only those constructors which are required by him.
3. The public constructor in the CacheDependency class reduces the amount of coding that needs to be done.
4. The base class handles all the dependencies and all issues relating to synchronization and disposal.
5. The start time feature also need not be framed from point zero. The capacity is inherited from the base class of constructors.
.
.
6. The number of new methods added to the CacheDependency Class make it extensible.
a. DependencyDispose is a protected method that releases the resources used by the class
b. GetUniqueID is a public method that retrieves a unique string identifier for the object
c. NotifyDependencyChanged is a protected method that informs the base class that the dependency .represented by an object has changed.
………………d. SetUtcLastModified is a protected method that marks the time when a dependency
………………changes.
………………e. HasChanged is a public Boolean read only property that indicates whether the
………………dependency has changed
………………f. UtcLastModified is a public read only property that gets the time from the
………………SetUtcLastModified.
7. The CacheDependency class has been designed to support only a limited set of well known dependencies. For instance the user cannot insert code to check whether a dependency condition has been meant.
………………a. The CacheDependency object internally sets up a file monitor object and receive a call
………………from it whenever the monitored object changes. This object is called a
………………FileSystemWatcher object .
………………b. It has an event handler and establishes a link between the CacheDependency object
………………and the Cache object and its items.
………………c. An event is fired whenever the monitored object changes.
8. In addition to creating a single dependency on an entry ASP.NET Cache also aggregates dependencies. For instance, a Cache entry can be made dependent both on a file and a SQL Server table.
………………a. Custom cache dependency objects inherit CacheDependency, so the array of
………………dependencies can be extremely wide.
………………b. The AggregateCacheDependency class is built as a custom cache dependency object
………………and inherits from the CacheDependency class.
9. The SqlCacheDependency class which inherits from CacheDependency class supports dependencies on SQL Server tables.
………………a. It implements features that are compatible with MSDE, SQL Server 7.0 and subsequent
………………versions.
………………b. The class works only when the tables on which dependencies have to be created are
………………enabled.
………………c. The SqlCacheDependency class has two constructors. The first takes the
………………SqlCommand object and the second accepts two strings—the database name and
………………the table name.
………………………..i. The SqlCacheDependency class sets up a dependency relationship with SQL
…………………………..Server 2005 known as command notification.
………………………..ii. Whenever there are changes in the tables involved the notification is sent to the
……………………………caller.
………………………..iii. The caller then handles the event and lets the parent know through the
……………………………. NotifyDependencyChanged.
………………d. Triggers and stored procedures will have to be created to handle any incoming
………………UPDATE, INSERT and DELETE statements.
………………e. This must be done before the application is published.
………………f. The command line tool aspnet_regsqlcache or the methods of
………………SqlCacheDependencyAdmin can be used to enable the database for notifications.
………………g. The database entry is to be bound to a connection string entry
………………h. The poll component then, accesses the newly created table to see whether other tables
………………have changed.
………………i. If one or more tables have to be monitored for changes they can be added to a list
………………programmatically by defining a trigger on the table that is sensitive to insert, updates and
………………deletions. The trigger can increment the value of the ChangeID column.
j. A key architectural component is the cache dependency manager object. This component accesses the table and reads the ChangeID field and informs the corresponding cache object if a change has occurred.
k. The cache dependency object is dependent on a helper cache key, whose name is defined in accordance with the table and the database. This helper cache entry is removed so that the original SqlCacheDependency object is invalidated.