Cache with Callback problems... threading?

Discussion in 'ASP .Net' started by Brian Vallelunga, Jul 23, 2003.

  1. I'm having a problem using the Cache object with asp.net. Here is what I
    have:
    1) I put something in cache and set its callback method.
    2) The object times out after X minutes and calls the callback method
    specified.
    3) The callback method gets the newest version of the item from the db and
    then calls a helper method to put the item back into cache. It looks like
    this:

    //Pseudocode
    // Called once by app
    Initialize()
    {
    Data myData = DataTier.GetData();
    AddDataToCache("key", myData);
    }

    // Puts item into cache with 10 minute expiration AddDataToCache(string key,
    object data) {
    // Delegate for expiring callback
    CacheItemRemovedCallback onRemove;
    onRemove = new CacheItemRemovedCallback(RemovedCallback);
    // Add data to the cache
    HttpContext.Current.Cache.Add(key, data, null, DateTime.Now.AddMinutes(10),
    TimeSpan.Zero, CacheItemPriority.High, onRemove);
    }

    // Called when item expires
    RemovedCallback(string key, object value, CacheItemRemovedReason reason)
    {
    Data myData = DataTier.GetData();
    AddDataToCache(key, myData);
    // Any code here won't be called!
    }



    That's basically the scenario. Ideally, I would initialize the system once,
    and then it would automatically refresh the data within it every time the
    cache expired.
    When debugging though, after the cache times out, the item is not put back
    in. It is very strange. RemovedCallback is called, but if I put a trace
    statement after the AddDataToCache method call, the trace is never written.
    Does anyone have an idea as to what is happening?
    Thanks,
    Brian
     
    Brian Vallelunga, Jul 23, 2003
    #1
    1. Advertising

  2. Brian Vallelunga

    Natty Gur Guest

    Hi,

    Here is the problem ... you are using the current context to get the
    application and set the cache. This works perfectly when the application
    init. But when the cache remove callback called there isn’t any request
    and the current context not existing. To overcome this issue add private
    variable to the global class that will hold the application then set him
    when the application start. Now you can use the application variable.

    you can see my example (and download) from :
    http://www.developersdex.com/gurus/code/653.asp

    Natty Gur, CTO
    Dao2Com Ltd.
    28th Baruch Hirsch st. Bnei-Brak
    Israel , 51114

    Phone Numbers:
    Office: +972-(0)3-5786668
    Fax: +972-(0)3-5703475
    Mobile: +972-(0)58-888377

    Know the overall picture


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Natty Gur, Jul 24, 2003
    #2
    1. Advertising

  3. Hmm... thanks for the help. Unfortunately, I don't think the method
    described below will help me. The page I am using this on actually caches
    many different things, and I don't know what all of them will be at the
    start of the application. Do you have an idea of how I could still go about
    doing this?

    Brian


    "Natty Gur" <> wrote in message
    news:%...
    > Hi,
    >
    > Here is the problem ... you are using the current context to get the
    > application and set the cache. This works perfectly when the application
    > init. But when the cache remove callback called there isn't any request
    > and the current context not existing. To overcome this issue add private
    > variable to the global class that will hold the application then set him
    > when the application start. Now you can use the application variable.
    >
    > you can see my example (and download) from :
    > http://www.developersdex.com/gurus/code/653.asp
    >
    > Natty Gur, CTO
    > Dao2Com Ltd.
    > 28th Baruch Hirsch st. Bnei-Brak
    > Israel , 51114
    >
    > Phone Numbers:
    > Office: +972-(0)3-5786668
    > Fax: +972-(0)3-5703475
    > Mobile: +972-(0)58-888377
    >
    > Know the overall picture
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    Brian Vallelunga, Jul 25, 2003
    #3
  4. Brian Vallelunga

    Natty Gur Guest

    I think you miss understand me. What I suggested is to save the
    application in a class level variable inside the global class so you can
    use it when the cache period will be finish and you want to refill the
    cache.

    Natty Gur, CTO
    Dao2Com Ltd.
    28th Baruch Hirsch st. Bnei-Brak
    Israel , 51114

    Phone Numbers:
    Office: +972-(0)3-5786668
    Fax: +972-(0)3-5703475
    Mobile: +972-(0)58-888377

    Know the overall picture


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Natty Gur, Jul 27, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?b25l?=
    Replies:
    1
    Views:
    5,331
    Karl Seguin [MVP]
    Mar 8, 2006
  2. H. M?ller

    RMI Callback and Threading

    H. M?ller, Dec 21, 2004, in forum: Java
    Replies:
    0
    Views:
    633
    H. M?ller
    Dec 21, 2004
  3. Francois De Serres

    (Win32 API) callback to Python, threading hiccups

    Francois De Serres, Jul 5, 2005, in forum: Python
    Replies:
    5
    Views:
    757
    Gregory Bond
    Jul 8, 2005
  4. jiing
    Replies:
    1
    Views:
    378
    Richard Bos
    Apr 15, 2005
  5. Replies:
    7
    Views:
    180
    Roger Pack
    Jul 15, 2009
Loading...

Share This Page