|
Page 1 of 2
ASP.NET 2.0 Free Tutorials : State Management And Caching in ASP.NET 2.0
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.
|