Simple Synchronized MRU Cache for Java
Introduction
A very simple and synchronized cache, based on Most Recently Used (MRU) algorithm, written in Java. Cache is based on LinkedHashMap using Generics.
About MRU Cache (wiki)
"When a file is being repeatedly scanned in a Looping Sequential reference pattern, MRU is the best replacement algorithm". In random access patterns and repeated scans over large datasets (sometimes known as cyclic access patterns) MRU cache algorithms have more hits than LRU due to their tendency to retain older data. MRU algorithms are most useful in situations where the older an item is the more likely it is to be accessed
Source Code
|
Requirements
- Java Platform, Standard Edition 5 and higher (2004)
Usage
Using the MRU cache is easy:
- Inherit from the MRUCache class
- Set your key and value in your new class
- Implement the getValue method
- Start using the cache by calling the getValueFromCache method
Example
public static
void
main
(
String
[]
args
) {
MRUCache<String, String> cache =
new
MRUCache<String, String>
(
3
) {
protected
String getValue
(
String key
) {
System.out.println
(
key
)
;
return
key;
}
}
;
cache.getValueFromCache
(
"A"
)
;
cache.getValueFromCache
(
"B"
)
;
cache.getValueFromCache
(
"C"
)
;
cache.getValueFromCache
(
"D"
)
;
// A will be removed from the cache
cache.getValueFromCache
(
"B"
)
;
// B will be retrieved from the cache
cache.getValueFromCache
(
"A"
)
;
// A will be added to the cache again
}
Help and Support
For questions and suggestions please contant support@small-software.com