There is a utility called jcs worker that will do what you need. http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/utils/access/JCSWorker.java?revision=332196&view=markup
--- emily chen <[EMAIL PROTECTED]> wrote: > Hi Aaron, > > If I don't use a lock, thread 1 gets the report > from cache by key, not data found, so it goes to DB > to get the report; before it puts the report into > cache, thread 2 accesses the cache by the same key, > and can not find the report, so it goes to DB to get > the data again. That means two thread will access DB > twice. But I want they just acces DB only one time > to get the same report. > > In my web application, it takes serval mins to run > a long report. Does it make sense to use a lock to > reduce DB access? > > Thanks, > > Lucy > > > > Aaron Smuts <[EMAIL PROTECTED]> wrote: > I don't understand what you need a lock for. You > can > go get the report, and then put it in the cache > under > the same key. You don't need to lock the key. Until > the new report is ready, client will get the old one > from the cache. Simply put the new value in the > cache > under the smae key name and it will replace the old > value. > > Aaron > > --- emily chen wrote: > > > Hi Aaron, > > > > I got you. I will implement the lock in my web > > application. > > > > Many thanks, > > > > Lucy > > > > Aaron Smuts wrote: > > Of course JCS is Thread safe, if you mean that it > > can > > handle multiple threads accessing the same regions > > at > > the same time. > > > > It doesn't work the way you described, but it is > > thread safe. You won't get a corrupted value for a > > key. > > > > As for the memory cache, it is a reference cache. > It > > returns references to objects in memory. It does > not > > clone the values. If you are manipulating objects > > that are in the cache, any other Thread could get > a > > reference to that same object. . .. It is best to > > clone objects that are retrieved from the cache > > prior > > to modifying them or just don't modify objects > that > > have been put in the cache. > > > > Aaron > > > > --- emily chen wrote: > > > > > Hi there, > > > > > > I want to use JCS for my web application, and > have > > > some thread safe question. > > > > > > Here is scenario: > > > > > > I have a JCS cache named > > �ePINBatchReportCache�? > > > which is used to save the ePIN Batch Report > data. > > > > > > Thread 1: > > > 1) Get report data from Cache for key. > > > 2) If not data found in the cache, then lock > > > the cache for that key. > > > 3) Get report data from database. > > > 4) Put the report data in cache for key > > > 5) Release lock for that key. > > > > > > Thread 2: > > > 1) Get the data from cache for key. > > > 2) Wait for lock to be released (should have > > > max wait time here). > > > 3) Get the data from cache. > > > > > > Does JCS have the above functionality -- lock > the > > > key of cache? Or I have to put lock on the key > in > > > my web application source code? > > > > > > Thank for your help. > > > > > > Lucy > > > > > > > > > --------------------------------- > > > Yahoo! Messenger with Voice. Make PC-to-Phone > > Calls > > > to the US (and 30+ countries) for 2�?min or > less. > > > > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: > > [EMAIL PROTECTED] > > For additional commands, e-mail: > > [EMAIL PROTECTED] > > > > > > > > > > --------------------------------- > > Yahoo! Messenger with Voice. PC-to-Phone calls for > > ridiculously low rates. > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: > [EMAIL PROTECTED] > For additional commands, e-mail: > [EMAIL PROTECTED] > > > > > --------------------------------- > Ring'em or ping'em. Make PC-to-phone calls as low > as 1�/min with Yahoo! Messenger with Voice. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]