Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCMJD
Search Coderanch
Advance search
Google search
Register / Login
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:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Developer Certification (OCMJD)
Cache Implementaion
mohamed sulibi
Ranch Hand
Posts: 169
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi every rancher;
I don't implement any cache for data but i have a good implementation of the cache that i use on the work, may it help you.
/** * An LRU cache, based on <code>LinkedHashMap</code>.<br> * This cache has a fixed maximum number of elements (<code>cacheSize</code>). * If the cache is full and another entry is added, the LRU (least recently * used) entry is dropped. */ class LRUCache { private static final float hashTableLoadFactor = 0.75f; private Map<String, String> map; private int cacheSize; /** * Creates a new LRU cache. * * @param cacheSize * the maximum number of entries that will be kept in this cache. */ public LRUCache(int cacheSize) { this.cacheSize = cacheSize; map = new LinkedHashMap<String, String>(cacheSize, hashTableLoadFactor, true) { private static final long serialVersionUID = 1; @Override protected boolean removeEldestEntry(Map.Entry<String, String> eldest) { return size() > LRUCache.this.cacheSize; } }; map = Collections.synchronizedMap(map); } /** * Retrieves an entry from the cache.<br> * The retrieved entry becomes the MRU (most recently used) entry. * * @param key * the key whose associated value is to be returned. * @return the value associated to this key, or null if no value with this * key exists in the cache. */ public String get(String key) { return map.get(key); } /** * Adds an entry to this cache. If the cache is full, the LRU (least * recently used) entry is dropped. * * @param key * the key with which the specified value is to be associated. * @param value * a value to be associated with the specified key. */ public void put(String key, String value) { map.put(key, value); } /** * Clears the cache. */ public void clear() { map.clear(); } @Override public String toString(){ return this.map.toString(); } } public class Main { public static void main(String[] args){ LRUCache cache = new LRUCache(10); for(int i = 0; i < 20; i++){ cache.put(String.valueOf(i), String.valueOf(i)); System.out.println(cache.toString()); System.out.println(cache.get(String.valueOf(1))); System.out.println(cache.get(String.valueOf(1))); System.out.println(cache.get(String.valueOf(i))); System.out.println(cache.get(String.valueOf(5))); } } }
regards.
Mohamed Sulibi.
SCJP
,
SCJD
in progress ( from 1/8/2007 till now )
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Object Pool
List box initialization through managed bean
Object Pool
JSF backingbean value is null always.
LRU cache
More...