SQLcache Dependency and cache API

Items in the cache are kept up-to date with the changes in the database. This code sample explains to programmatically specify the cache and set their attributes.This is known as Programmatic output caching.

Event fired when static methods of the Cache class such as Insert, Remove, Add and so on gets started.

Defining key for sqldependency in web.config



<Caching>
<sqldependency enabled="true" pollTime="1000">
<databases>
<add name="consdb" connectionStringName="consdb" pollTime="1000"/>
</databases>
</sqldependency>
</caching>



The code behind file is given below


Response.Write("Page created on:" + DateTime.Now.ToString());
DataSet ds= (DataSet)Cache["ds"];

if (ds== null)
{
SqlConnection conn = new SqlConnection("server=royalserver;database=db;UID=uid;PWD=pwd");
SqlDataAdapter adapter = new SqlDataAdapter("select * from tablename", conn);
ds= new DataSet();
adapter.Fill(ds);
//consdb is the key for sqldependency configured in web.config
SqlCacheDependency depend = new SqlCacheDependency("consdb", "ds");

//caache API--Starts here
//Response.AddCacheDependency(depend);
//Response.Cache.SetValidUntilExpires(true);
//Response.Cache.SetExpires(DateTime.Now.AddMinutes(60));
//Response.Cache.SetCacheability(HttpCacheability.Public);
//cache API--Ends here

Cache.Insert("ds", ds, depend);
Response.Write("
Clients retrieved from the database");
}
else
{
Response.Write("
Categories retrived from the cache");
}
gridView.DataSource = climst;
gridView.DataBind();



Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: