Caching Depedency in ASP.Net

Using Cache dependency we can set the dependency of the catch with some data or entity to changed. So we can set the depedency of cache by which we can update, remove cache. There are three types of dependencies supported in ASP.NET.
  1. File based Dependency
  2. Key Based Dependency
  3. Time Based Dependency
File Based Dependency : File-based dependency invalidates a particular Cache item when a file(s) on the disk changes.
Using cache dependency we can force ASP.NET to expire the cached data item from the cache when the dependency file changes. We can set Dependency to multiple file also . on such case dependency should be built from an array of files or directories.

It's Uses : File based dependency is very use full when need to update some data that displaing to the user based on some changed on file. As for Example, a New Sites always shows data from a file , now if some breaking news come, they just update the file and cached should expire and during the expire time we can reload the cache with updated data using OnRemoveCallBack.

Key Based Dependency : Key-based dependency invalidates a particular cache item when another cache item changes.

It's Uses : This is use full when we have multiple interrelated object are in cache and if one of the object changed we need to updated or expire all of them, Key Based Dependency will be best option.

Time Based Dependency : Time-based dependency causes an item to expire at a defined time. Cache.Insert() method of the Cache class is used to create a time-based dependency. Two Types of Time based Dependency are available.
  1. Absolute
  2. Sliding
Absolute: Sets an absolute time for a cache item to expire. Absolute expirations are specified in full-time format (hh:mm:ss). The object will be expired from the cache at the specified time.

Sliding: Resets the time for the item in the Cache to expire on each request. This is useful when an item in the cache is to be kept alive so long as requests for that item are coming in from various clients.

With those dependency ASP.NET also support
In addition to the dependencies, ASP.NET allows the following:

Automatic expiration: The cache items that are underused and have no dependencies are automatically expired.

Support for callback: The Cache object can be configured to call a given piece of code that will be executed when an item is removed from the cache. This gives you an opportunity to update the cache. We can use OnRemoveCallback().
Tags: , , , , , , , , , , , , , ,
Hot on Web:


About author