Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Caching / October 2003

Tip: Looking for answers? Try searching our database.

CacheItemRemovedCallback only executing once

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
martin - 03 Oct 2003 15:06 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.