[Opensymphony-oscache] threading issues with NeedsRefreshException
snafu <[email protected]> Tue, 12 Jul 2005 14:55:31 CDT
| Newsgroups | gmane.comp.java.open-symphony.os-cache |
|---|---|
| Message-ID | <31467770.1121198196172.JavaMail.os-j2ee@opensymphony01.contegix.com> |
here is the oft cited example for handling a potentially expired cache item:
String myKey = "myKey";
String myValue;
int myRefreshPeriod = 1000;
try {
// Get from the cache
myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);
} catch (NeedsRefreshException nre) {
try {
// Get the value (probably from the database)
myValue = "This is the content retrieved.";
// Store in the cache
admin.putInCache(myKey, myValue);
} catch (Exception ex) {
// We have the current content if we want fail-over.
myValue = (String) nre.getCacheContent();
// It is essential that cancelUpdate is called if the
// cached content is not rebuilt
admin.cancelUpdate(myKey);
}
}
say the item is expired and so you catch NeedsRefreshException and try to rebuild the entry (i.e. the line myValue = "This is the content retrieved.";). Fine and dandy if this is a quick retrieval but what if it requires a huge query that might take 30-60 secs to execute and load into memory? If you have cache.blocking set to 'false' then all subsequent requests will recieve a stale copy but at least they wont have to wait. The problem is with the poor bastard who hits the expired item first, since he has to catch the exception he has to wait until the put completes. Can anyone propose a better way to do this (prefferably with a code example). The only way I can think is to kickoff a thread to handle the put while I return nre.getCacheContent() to serve the unfortunate user his data immediately.
much thanx
S
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=4200&messageID=10401#10401