First of all, your code is incomplete because there is no get(key) on HashSet (so I assume that you are referring to a certain Map ), and your code does not mention “marking”. There are also many ways to do caching, and it's hard to choose the best solution without knowing what you are trying to cache and why.
When implementing a cache, it is usually assumed that multiple threads will access the data structure at the same time. So, the first thing you need to do is use the backup data structure, which is thread safe. HashMap not thread safe, but ConcurrentHashMap is. There are also a number of other parallel implementations of Map , namely in Guava , Javolution, and a high-level library . There are other ways to create caches besides cards, and their usefulness depends on your use case. Despite this, you will most likely need to make the stream data structure redundant, even if you decide that you do not need a background thread, and instead expire the expired objects, trying to extract them from the cache. Or let GC delete entries with SoftReference s.
After you have made the cache internals thread safe, you can simply start a new (most likely demonized) thread that periodically searches / iterates the cache and deletes old entries. The thread will do this in a loop (until it is interrupted if you want to stop it again), and then sleeps for a while after each sweep.
However, you should consider whether you should do this and create your own cache implementation. Writing thread-safe code is not easy, and I recommend that you study it before trying to write your own cache implementation. I can recommend the Java Concurrency book in practice.
An easier way to do this, of course, is to use an existing cache implementation. There are many options available on Java-land, all with their own unique set of tradeoffs.
- EhCache and JCS are both general purpose caches that suit most of the caching needs found in a typical "enterprise" application.
- Infinispan is a cache optimized for distributed use, and thus can cache more data than what can fit on a single machine. I also like its
ConcurrentMap API. - As already mentioned, the Googles Guava library has a Cache API, which is very useful for small caches in memory.
Since you want to limit the number of entries in the cache, you might be interested in the object pool, not the cache.
- Apache Commons-Pool is widely used and has APIs that resemble what you are trying to create yourself.
- Stormpot , on the other hand, has a completely different API, and I almost mentioned it just because I wrote it. This is probably not what you want, but who can be sure without knowing what you are trying to cache and why?
Chris west
source share