Pages

Thursday, January 13, 2011

Cache Insert() Method

Insert() ---> it overwrites the item if same key already exists in the cache.

public void Insert(string key, object value,CacheDependency dependencies,
DateTime absoluteExpiration,
TimeSpan slidingExpiration,
CacheItemPriority priority,
CacheItemRemovedCallback onRemoveCallback );

Parameter Description
key The cache key used to reference the object.
value The object to be inserted in the cache.
dependencies The file or cache key dependencies for the item. When any dependency changes, the object becomes invalid and is removed from the cache. If there are no dependencies, this parameter contains a null reference (Nothing in Visual Basic).
absoluteExpiration The time at which the inserted object expires and is removed from the cache.
slidingExpiration The interval between the time the inserted object was last accessed and when that object expires. If this value is the equivalent of 20 minutes, the object will expire and be removed from the cache 20 minutes after it was last accessed.
priority The cost of the object relative to other items stored in the cache, as expressed by the CacheItemPriority enumeration. This value is used by the cache when it evicts objects; objects with a lower cost are removed from the cache before objects with a higher cost.
onRemoveCallback A delegate that, if provided, will be called when an object is removed from the cache. You can use this to notify applications when their objects are deleted from the cache.



e.g.


Cache.Insert ("DSN", connectionString,new CacheDependency("filepath") 
  DateTime.Now.AddMinutes(2), 
  TimeSpan.Zero, 
  CacheItemPriority.High,
  new CacheItemRemovedCallback(this.RemovedCallback)
);
 
dependency and a call back function executed when the cache expires.

CacheItemRemovedCallback is a delegate, which defines a callback method for notifying applications when a cached item is removed from the Cache.

e.g. 1. Insert xml data from a file into the cache, eliminating
the need to read from the file on subsequent requests.

Cache.Insert("key", myXMLFileData, new
System.Web.Caching.CacheDependency(Server.MapPath("users.xml")));

CacheDependency ----> ensures that cache immediately expires while files changes and updated with changes. Similary can happen with array of filenames, if cached data needs to depend on several files.

e.g. 2. Inserting second piece of data that depends on the existing first piece of data ["key" (say)].
Cache.Insert("dependentkey", myDependentData, new
System.Web.Caching.CacheDependency(new string[] {}, new string[]
{"key"}));

  If no key existed in the cache called "key", or if the item associated with that key expires or is updated, then the cache entry for "dependentkey" would expire.


e.g. 3. Absolute Expiration:
Cache.Insert("key", myTimeSensitiveData, null,
DateTime.Now.AddMinutes(1), TimeSpan.Zero);

Caches time sensitive data for one minute, at which point the cache will expire.
Note: absolute expiration and sliding expiration (below) cannot be used together
e.g. 4.  Sliding Expiration:

Cache.Insert("key", myFrequentlyAccessedData, null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes(1));

Caches some frequently used data and remain in cache until one minute passes without anything referencing it.
If anyone references even at 59th second then again time of expiration become one minute instead of one second that was expected to expire the data in case it might not referenced during the current minute.


 

No comments: