In this article we will learn how to implement file dependency in caching.
In this method the cache["messageDependency"] loads the file "DependencyFile.txt" in memory(cache) only when its content is changed in the disk.
public void displayMessage()
{
String message;
String Path = "~/DependencyFile.txt";
if (Cache["messageDependency"] == null)
{
System.IO.StreamReader sReader = new StreamReader(Server.MapPath(Path));
message = sReader.ReadToEnd();
sReader.Close();
System.Web.Caching.CacheDependency msgDependency = new System.Web.Caching.CacheDependency(Server.MapPath(Path));
Cache.Insert("messageDependency", message, msgDependency);
}
Response.Write(Cache["messageDependency"].ToString());
}
Comments
Post a Comment