Easy LRU cache with Java

May 5th, 2009 § 4

The JDK is full of gems.  My latest discovery began with a need for a simple in memory “least recently used” (LRU) cache.  I wanted a hash map with limited size.  When the cache is full, it should remove the least recently used item, keeping the more popular items in memory.  I figured there was probably an existing implementation in java.util, but nothing fit, until I ran across some posts that mentioned “LinkedHashMap” as a possible solution…Bingo.  The java doc even mentions this type of application and provides an abstraction ready for implementation when you extend LinkedHashMap,

 removeEldestEntry()

Today I found a good example with documentation, for anyone who is curious, or would like an off the shelf ready to use example of LRU cache for java, see

http://www.source-code.biz/snippets/java/6.htm

§ 4 Responses to “Easy LRU cache with Java”

What's this?

You are currently reading Easy LRU cache with Java at The Web Semantic.

meta