public List<? extends V> getCachedObjects() { LinkedList<V> list = new LinkedList<V>(); for (ObjectRef<K, V> objectRef : state.map.values()) { V object = objectRef.getObject(); if (objectRef.isValid()) { list.add(object); } } return list; }
public V get(Serializable name) { ObjectRef<K, V> entry = map.get(name); if (entry != null) { V o = entry.getObject(); if (entry.isValid()) { config.hits.incrementAndGet(); config.onGet(entry.name, o); return o; } else { config.misses.incrementAndGet(); if (map.remove(name, entry)) { queue.remove(entry); } config.onExpire(entry.name, o); } } return null; }
public V remove(Serializable name) { boolean trace = isTraceEnabled(); ObjectRef<K, V> item = map.remove(name); if (item != null) { if (trace) { trace("Removed item=" + item.serial + " from the map going to remove it"); } boolean removed = queue.remove(item); boolean valid = removed && item.isValid(); V object = item.getObject(); if (valid) { config.onRemove(item.name, object); return object; } else { config.onExpire(item.name, object); return null; } } else { return null; } }
/** * Do a put with the provided expiration time. * * @param expirationTime the expiration time * @param name the cache key * @param obj the cached value */ void put(long expirationTime, K name, V obj) { boolean trace = isTraceEnabled(); ObjectRef<K, V> nextRef = new SimpleObjectRef<K, V>(expirationTime, name, obj); ObjectRef<K, V> previousRef = map.put(name, nextRef); // Remove previous (promoted as first element) if (previousRef != null) { queue.remove(previousRef); if (trace) { trace( "Replaced item=" + previousRef.serial + " with item=" + nextRef.serial + " in the map"); } } else if (trace) { trace("Added item=" + nextRef.serial + " to map"); } // Add to the queue queue.add(nextRef); // Perform eviction from queue ArrayList<ObjectRef<K, V>> evictedRefs = queue.trim(config.maxSize); if (evictedRefs != null) { for (ObjectRef<K, V> evictedRef : evictedRefs) { // We remove it from the map only if it was the same entry // it could have been removed concurrently by an explicit remove // or by a promotion map.remove(evictedRef.name, evictedRef); // Expiration callback config.onExpire(evictedRef.name, evictedRef.getObject()); } } // Put callback config.onPut(name, obj); }