I was trying to cache a datatble based on userid as key and caching the datatable. But when the userid key is not changed , each time datatable is generated from database not from the cache.
I am using the following code in which getMenutable method gets menutable from database.
****************************************************
// code
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Configuration;
using DataAccess;
using System.Web.Caching;
namespace app.demo
{
/// <summary>
///</summary>
public class clsMenu
{
HttpContext context = HttpContext.Current;
public DataTable DataLoad(string asuserid)
{
DataTable dtMenu = new DataTable();
object CacheDataSetMenu = (DataTable)HttpContext.Current.Cache.Get("CacheDataSetMenu");
if (CacheDataSetMenu == null)
{
context.Cache["dtmenu"] = context.Session["userid"].ToString(); //"value";
dtMenu = getMenutable(asuserid);
string[] keys = new String[1];
keys[0] = context.Cache["dtmenu"].ToString(); //context.Session["userid"].ToString();
CacheDependency dependency = new CacheDependency(null, keys);
//HttpContext.Current.Cache.Insert("CacheDataSetMenu",dtMenu,dependency,DateTime.Now.AddHours(8),TimeSpan.Zero,CacheItemPriority.Normal,new CacheItemRemovedCallback(this.onremove));
context.Cache.Insert("CacheDataSetMenu",dtMenu,dependency);
}
else
{
dtMenu = (DataTable)context.Cache.Get("CacheDataSetMenu");
}
return dtMenu;
}
private DataTable getMenutable(string asuserid)
{
//logic to get datatable from db.
return dtMenu;
}
public void onremove(String k,Object v,CacheItemRemovedReason r)
{
// Write code here to check the reason for expiration
// and to repopulate using new values
getMenutable(context.User.Identity.Name);
}
}
}
Kindly look into this and advise me.
Thanks
Siva
I am using the following code in which getMenutable method gets menutable from database.
****************************************************
// code
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Configuration;
using DataAccess;
using System.Web.Caching;
namespace app.demo
{
/// <summary>
///</summary>
public class clsMenu
{
HttpContext context = HttpContext.Current;
public DataTable DataLoad(string asuserid)
{
DataTable dtMenu = new DataTable();
object CacheDataSetMenu = (DataTable)HttpContext.Current.Cache.Get("CacheDataSetMenu");
if (CacheDataSetMenu == null)
{
context.Cache["dtmenu"] = context.Session["userid"].ToString(); //"value";
dtMenu = getMenutable(asuserid);
string[] keys = new String[1];
keys[0] = context.Cache["dtmenu"].ToString(); //context.Session["userid"].ToString();
CacheDependency dependency = new CacheDependency(null, keys);
//HttpContext.Current.Cache.Insert("CacheDataSetMenu",dtMenu,dependency,DateTime.Now.AddHours(8),TimeSpan.Zero,CacheItemPriority.Normal,new CacheItemRemovedCallback(this.onremove));
context.Cache.Insert("CacheDataSetMenu",dtMenu,dependency);
}
else
{
dtMenu = (DataTable)context.Cache.Get("CacheDataSetMenu");
}
return dtMenu;
}
private DataTable getMenutable(string asuserid)
{
//logic to get datatable from db.
return dtMenu;
}
public void onremove(String k,Object v,CacheItemRemovedReason r)
{
// Write code here to check the reason for expiration
// and to repopulate using new values
getMenutable(context.User.Identity.Name);
}
}
}
Kindly look into this and advise me.
Thanks
Siva