Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implementation of cache expiration with Future

 
Swapnil Shroff
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Swapnil Shroff
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic