File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Implementation of cache expiration with Future Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Implementation of cache expiration with Future" Watch "Implementation of cache expiration with Future" New topic
Author

Implementation of cache expiration with Future

Swapnil Shroff
Ranch Hand

Joined: Mar 07, 2006
Posts: 58
I'm trying to extend the cache implementation given in book Concurrency in Practice(by Brian Goetz).

I want to extend the cache such that if any entry is not accessed in 10 seconds then it should expire. i.e removed from cache.

For this I have extended Future and FutureTask as below






As seen above I'm overriding the the get method to calculate the expiration time.

And in the main cache Class i.e Memorizer I will start a new thread that will periodically scan the entries and remove entries where isResultExpired returns true.

I just wanted to know if this implementation would work or there is some bug in my code?

Note I should also override get with timeout, however to keep is concise I have omitted that.

Thanks


SCJP 5, SCDJWS<br /> <br />It's amazing how premature optimisation is both seductive and destructive; even when you know
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Swapnil Shroff wrote:I'm trying to extend the cache implementation given in book Concurrency in Practice(by Brian Goetz).
I want to extend the cache such that if any entry is not accessed in 10 seconds then it should expire. i.e removed from cache.

I'm afraid I'm not familiar with the specific implementation but, if it was me, I think I'd simply wrap each element in a CacheEntry class that contains a mutable lastAccessed field. That way, you could easily create a hasExpired() method.

Now, how you use that is likely to be specific to the implementation of the cache itself. If you need to actually remove expired entries, then I think I'd look at the possibility of a "sweeper" Thread; but there are many other possibilities.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Swapnil Shroff
Ranch Hand

Joined: Mar 07, 2006
Posts: 58
Thats what I'm doing, my FutureTaskWithExpire maps to your CacheEntry. But yes you are correct instead of storing the time the entry would expire, I should just store the lastAccssed time and then calculate if expired in hasExpired or isResultExpiredMethod. That way - get method will have some less work to do and the calculation would be done in sweeper thread.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Swapnil Shroff wrote:Thats what I'm doing, my FutureTaskWithExpire maps to your CacheEntry. But yes you are correct instead of storing the time the entry would expire, I should just store the lastAccssed time and then calculate if expired in hasExpired or isResultExpiredMethod. That way - get method will have some less work to do and the calculation would be done in sweeper thread.

If this is specifically for implementing a Java cache structure, one of the things you might want to look at is LinkedHashMap; it has specific settings and methods for setting up least-recently used caches; however these are normally based on size rather than latency. You should be able to combine the two though, and the retrieval and LRU-cache part is extremely fast.

HIH

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementation of cache expiration with Future
 
Similar Threads
question: why EventDispatcherThread doesn't stop?
Decorator Pattern
FutureTask with Runnable