473,467 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Cache with Callback problems... threading?

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
Nov 17 '05 #1
3 2905
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!
Nov 17 '05 #2
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" <na***@dao2com.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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!

Nov 17 '05 #3
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!
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Francois De Serres | last post by:
Hiho, could somebody please enlighten me about the mechanics of C callbacks to Python? My domain is more specifically callbacks from the win32 API, but I'm not sure that's where the problem...
6
by: Stanley | last post by:
has anyone managed to use cache application block in a web application? thanks stanley
1
by: Mohamed Fysal | last post by:
I have written a Regular DLL with many Export Functions and one CALLBACK fun ction . The callback function declared in the .cpp file of the Regular DLL is as fol lows: typedef BOOL...
1
by: Glenn | last post by:
Hi, I have a config XML file that I am using from the application cache. I have configured the entry with a remove callback to re-populate cache automatically when the XML file changes. All is...
4
by: NWx | last post by:
Hi, I' trying to implement a callback method when a cache object expires I want to do this to automatically logout user after a timeout (for demo purposes) My thought is, when user logon,...
4
by: Erick | last post by:
i have an asp.net application and I want to save the results of an sql query in cache. Because the queries take 28 seconds to run (there are twelve similar queries) I want to run it all at 4am, ...
5
by: greg.merideth | last post by:
I have a class that I've provided an event for to be called when the processing in the class is complete (a callback). The class spins up a series of threads for the background operation and I'm...
1
by: an | last post by:
I've implemented a call-back function to repopulate the stored Cache objects when they expires or are removed it works great but in one situation only it doesen't work (that is the callback...
5
by: Joseph Geretz | last post by:
Of course, I can store a C# class instance to the Server Cache (this.Context.Cache). I've tried it. My question is, will this destroy the scalability of my application? My background is VB6....
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.