C# .NET - Example of cache dependency

Asked By Mahesh B on 05-Jan-12 02:13 AM
Hi
i need simple example of cache dependency
and what is a key and value

i tried this example

 CacheDependency dep = new CacheDependency("C:\\Users\\Administrator\\Desktop\\New.txt");
      Cache.Insert("key", "C:\\Users\\Administrator\\Desktop\\New.txt", dep);
 if (dep.HasChanged)
        Response.Write("changed");
      else
        Response.Write("not changed");

when i change the content in New.txt
i am getting the output as "not changed"
what is the reason
Suchit shah replied to Mahesh B on 05-Jan-12 02:42 AM
use below example for it

protected void Page_Load(object sender, EventArgs e)
{
  Response.AddCacheItemDependency("ProcessIntensiveReport");
 
  // Set additional properties to enable caching.
  Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
  Response.Cache.SetCacheability(HttpCacheability.Public);
  Response.Cache.SetValidUntilExpires(true);
}

File-based Dependency
 
File-based dependency invalidates a particular Cache item when file(s) on disk change. For example, if instead of loading our ProductData from a database, we loaded it from an XML file:

Dim dom As XmlDocument()
dom.Load(Server.MapPath("product.xml")
Cache("ProductData") = dom

 
We obviously would also want to invalidate the data within the Cache if product.xml changes. Assuming product.xml is in the same directory as the requesting application:

Dim dependency as new CacheDependency(Server.MapPath("product.xml"))
Cache.Insert("ProductData", dom, dependency)
 
In the above code sample, we're creating an instance of a CacheDependency class, dependency, and passing in the path to the product.xml file. We then use the Insert() method of the Cache to create our ProductData key that is dependent upon the file it retrieves its data from.

Key-based Dependency
 
Key-based dependency invalidates a particular Cache item when another Cache item changes. For example, if our application added multiple DataSets to the Cache, such as ProductData, SalesData, and MarketingData and SalesData and MarketingData rely upon ProductData being valid. We could use a key-based dependency to invalidate SalesData and MarketingData if ProductData changes. We set up this dependency when we create the Cache entries for SalesData and MarketingData:

Dim dependency (1) As String
dependencyKey(0) = "ProductData"
Dim productDataDependency As new CacheDependency(nothing, dependencyKey)

Cache.Insert("SalesData", LoadDataSet("Sales"), productDataDependency)

 
In the above example code, we use the Insert() method to create a new Cache entry named SalesData passing in an instance of a CacheDependency class named productDataDependency which names the Cache key ProductData. Now, whenever ProductData changes, SalesData will be removed from the Cache.

Time-based Dependency
 
Time-based dependency simply expires the item at a defined point in time. Again, we would use the Insert() method of the Cache to create this dependency. We have two options for time-based dependency:
 •Absolute—Sets an absolute time; for example, current time + 10 minutes for the Cache entry to expire.
 •Sliding—Resets the time for the item in the Cache to expire on each request.
 
We can use the Sliding expiration option to Cache our ProductData DataSet for a maximum of 10 minutes. As long as requests for ProductData are made within a 10-minute window, the data is made valid for another 10 minutes:

' 10 minute time span
Dim span As New TimeSpan(0,10,0)
Cache.Insert("ProductData", LoadDataSet(), nothing, nothing, span)

Srikanth K replied to Mahesh B on 05-Jan-12 03:56 AM

a cache dependency represents a logical dependency between a cached item and a file(s), folder(s), or another cached item(s). When the dependency is broken (i.e., the file/folder/item changes), the bound cached item is removed from the cache.

CacheDependency dep = new CacheDependency(fileName, dateTime);
cache.Insert(key, value, dep);
http://www.asp.net/web-forms/tutorials/data-access/caching-data/using-sql-cache-dependencies-cs
Jitendra Faye replied to Mahesh B on 05-Jan-12 04:22 AM
The CacheDependency class monitors the dependency relationships so that when any of them changes, the cached item will be automatically removed.

example-

// Insert the cache item. CacheDependency dep = new CacheDependency(fileName, dt); 
cache.Insert("key", "value", dep);



// Check whether CacheDependency.HasChanged is true. if (dep.HasChanged) Response.Write("<p>The dependency has changed."); else Response.Write("<p>The dependency has not changed.");
kalpana aparnathi replied to Mahesh B on 05-Jan-12 04:34 AM
hi,

// Insert the cache item.
CacheDependency dep = new CacheDependency(fileName, dt);
cache.Insert("key", "value", dep);
 
// Check whether CacheDependency.HasChanged is true.
if (dep.HasChanged)
  Response.Write("<p>The dependency has changed."); 
else Response.Write("<p>The dependency has not changed.");


Thanks,