In my Session startup method, I execute this code:
School myschool = new School(schoolId);
Context.Cache.Insert(schoolCacheKey, myschool, null,
DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration,
CacheItemPriority.Normal, new CacheItemRemovedCallback
(this.CacheItemRemoved));
Then I have the CacheItemRemoved method as follows:
public void CacheItemRemoved(string pKey, object pValue,
CacheItemRemovedReason pReason)
{
string schoolId = pKey.Substring(6);
if (pReason == CacheItemRemovedReason.Expired)
{
School myschool = new School(Convert.ToInt64
(schoolId));
Context.Cache.Insert(pKey, school, null,
DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration,
CacheItemPriority.Normal, new CacheItemRemovedCallback
(this.CacheItemRemoved));
}
}
The problem is the callback method gets executed the first
time the cache object expires, but not again. Can anyone
tell me what I'm missing here?
Thanks in advance for any help.
Jona Kee - 07 Oct 2003 20:47 GMT
Hi Martin,
I have essentially the same scenario and I don't run into the problem
you are describing. The only real difference I see is encapsulation
and a little aesthetics. See code below
//start with calling a static method from Global.asax
protected void Session_Start(Object sender, EventArgs e)
{
//load up the variables and datasets used for every user
Common.LoadCacheData();
}
//in some other class define the static methods
Class Common
{
public static void LoadCacheData ()
{
BaseLogic.CachedData _CachedData;
string cacheKey = BaseLogic.Common.CONST_CachedData;
//this ensures we are not loading up the data again when a
user logs in
if(BaseLogic.Common.IsCacheEmpty(cacheKey))
{
_CachedData = new BaseLogic.CachedData();
BaseLogic.Common.StoreInCache(cacheKey,_CachedData,4,new
CacheItemRemovedCallback(OnCachedDataRemoval));
}
}
private static void OnCachedDataRemoval(string CacheKey, object
obj, System.Web.Caching.CacheItemRemovedReason eReason)
{
if (eReason == CacheItemRemovedReason.Expired || eReason ==
CacheItemRemovedReason.Removed)
{
switch (CacheKey)
{
case BaseLogic.Common.CONST_CachedData:
{
LoadCachedData();
break;
}
//do other cases here
default:
{
break;
}
}
}
}
}
If anything it's a little cleaner and reusable. Help this helps
> In my Session startup method, I execute this code:
>
[quoted text clipped - 26 lines]
>
> Thanks in advance for any help.