Anthony Goubard is a freelance Senior Software Engineer from Amsterdam. He has developed in Java since 1995. He has developed many softwares available at http://www.japplis.com and is the main developer of the Web Services framework XINS. Anthony is a DZone MVB and is not an employee of DZone and has posted 33 posts at DZone. You can read more from them at their website. View Full User Profile

ExpiryFolder: Time Based Cache

07.25.2008
| 5198 views |
  • submit to reddit
I recently blogged about Top ten performance problems and their solutions. Here is a class that can help you when caching. This class is time based. Meaning that unless accessed, the entries will be removed after x minutes.
This can be useful when you want a session time-out for example.

How does it work?

You have to think about boxes and objects moving from boxes to boxes every tick until they reach the last box where they disappear.Let's say you have a time-out of 30 minutes with a tick every 30 seconds.
The ExpiryFolder will create 60 boxes.
Every objects created at about the same time are in the same box.
Every 30 seconds they move to the box n-1.
If an object is accessed, it moves to the top box.
When it reaches the box 0 and a tick is processed, the object is removed.

One of the big challenge is the synchronization. Here is the code:

 

Here is an example:

private final static int DURATION = 5000; //ms
private final static int PRECISION = 1000; //ms
private final static String NAME = "TestFolder";

ExpiryStrategy strategy = new ExpiryStrategy(DURATION, PRECISION);
ExpiryFolder folder = new ExpiryFolder(NAME, strategy);
final String KEY = "something";
final String VAL = "else";
folder.put(KEY, VAL);
assertEquals(1, folder.size());
assertEquals(VAL, folder.find(KEY)); // doesn't count as an access
assertEquals(VAL, folder.get(KEY));

Note that this code is already used on production for several years.

Published at DZone with permission of Anthony Goubard, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Danny Lee replied on Fri, 2008/07/25 - 9:36am

I think I better stick to approved stuff, like JBoss Cache, OSCache or EHCache and read sources of these cache-implementations to learn something new...

Doug McCreary replied on Fri, 2008/07/25 - 1:33pm

This approach is similar to something I built a while ago. One thing that will greatly improve it is to realize that the state of the cache matters only to evictions and accesses. You can change the sleep to sleep until the next expected eviction, tagging each entry with an eviction time rather than maintaining all the buckets. When an item is accessed, its eviction time changes. Now the solution won't have to do a ton of busy work juggling the maps every tick(). Work is only done when it is actually time to evict something, or when the cache is used.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.