Pages

Thursday, January 13, 2011

What is a dependency?

Any change in the resources which our cache item depends on[*], will make our cache item expire.

 [*] Expiration policy of the item based on other resources such as physical file(s) in the server, another cache item(s), or time that can be set to be dependency for item to be cached.

There are three types of dependencies:
  • File based dependency
  • Key based dependency
  • Time based dependency
File based dependency
We can set the validity of the cache item based on an external file or files. When this resource changes, the cached object becomes obsolete and is removed from the cache.
e.g.
CacheDependency dependency = new CacheDependency(Server.MapPath("isbn.xml"));

For multiple file dependencies, we can pass a string array containing the filename to the constructor.
Key based dependency
Key based dependency enables you to associate one cached item with another item in the cache and invalidates the first item when the second item changes. The following code fragment demonstrates how to insert an item into your application's Cache with a dependency on a key to another item placed in the cache. Since this method uses array syntax, you must define the number of keys the item you are adding to the Cache is dependent on.

e.g.

String[] dependencyKey = new String[1];
dependencyKey[0] = "key1"; 
CacheDependency dependency =  
new CacheDependency(null, dependencyKey);
 
You can use multiple key dependencies by increasing the array size and adding the keys to the array.
Time based dependency.
A time based dependency expires the item at a predefined time. There are two types of time-based dependency: Absolute and Sliding explained in under ASP.NET Cache.

No comments: