Hi All,
I have written a function which gives me dataset either from database or
from cache.
1. First time when the page is hitted there is no cachekeyname in cache so
it fires a query to database and insert the object into cache.
2. With the second hit it should pick up the data from cache as per the code
but still it picks up the data from database once again it insert a object
in cache
Is there any problem with the code,
Regards,
Shailesh
public DataSet GetCachedData(string CacheKeyName, DateTime ExpiryTime,string
SQLQueryorSP,CommandType cmdType,params SqlParameter[] commandParameters)
{
DataSet dsCached = null;
if(HttpContext.Current.Cache[CacheKeyName]!=null)
{
dsCached = (DataSet)HttpContext.Current.Cache[CacheKeyName];
//System.Web.HttpContext.Current.Response.Write("Comming data frm cache"
+ HttpContext.Current.Cache[CacheKeyName] + "<BR>");
}
else
{
if(cmdType == CommandType.StoredProcedure)
{
dsCached = SqlHelper.ExecuteDataset(SqlHelper.SqlConnectionString(),
CommandType.StoredProcedure, SQLQueryorSP, commandParameters);
}
else
{
dsCached = SqlHelper.ExecuteDataset(SqlHelper.SqlConnectionString(),
CommandType.Text, SQLQueryorSP, commandParameters);
}
// Inserting the data into Cache
HttpContext.Current.Cache.Insert(CacheKeyName, dsCached , null,
ExpiryTime, TimeSpan.Zero);
//System.Web.HttpContext.Current.Response.Write("Inserted into cache KEY
: " + CacheKeyName + "<BR>");
}
return dsCached;
}