A while ago we have created a simple cache for Java application. It was modelled like a Map<K, V>: it cached values for keys.
Map<K, V>
Use cases were:
Cache<String, Object> cache = new Cache<String, Object>(); ... instance = cache.get("key"); cache.put("key", instance);
But now we thought of different implementation like a WeakReference<V> and with map access as additional utility methods.
WeakReference<V>
Consider an examples:
1. Free standing CachedReference<V> instance.
CachedReference<V>
CachedReference<Data> ref = new CachedReference<Data>(1000, true); ... ref.set(data); ... data = ref.get();
2. Map of CachedReference<V> instances.
ConcurrentHashMap<String, CachedReference<Data>> cache = new ConcurrentHashMap<String, CachedReference<Data>>(); CachedReference.put(cache, "key", data, 1000, true); ...data = CachedReference.get(cache, "key");
The first case is faster than original Cache<K, V> as it does not use any hash map at all. The later case provides the same performance as Cache<K, V> but gives a better control over the storage. Incidentally, CachedReference<V> is more compact than Cache<K, V>.
Cache<K, V>
The new implementation is CachedReference.java, the old one Cache.java.
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u