• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Implementation of cache expiration with Future

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic