Package org.postgresql.util
Class LruCache<Key,Value extends CanEstimateSize>
- java.lang.Object
-
- org.postgresql.util.LruCache<Key,Value>
-
- All Implemented Interfaces:
Gettable<Key,Value>
public class LruCache<Key,Value extends CanEstimateSize> extends java.lang.Object implements Gettable<Key,Value>
Caches values in simple least-recently-accessed order.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceLruCache.CreateAction<Key,Value>When the entry is not present in cache, this create action is used to create one.static interfaceLruCache.EvictAction<Value>Action that is invoked when the entry is removed from the cache.private classLruCache.LimitedMap
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<Key,Value>cacheprivate LruCache.CreateAction<Key,Value>createActionprivate longcurrentSizeprivate ResourceLocklockprivate longmaxSizeBytesprivate intmaxSizeEntriesprivate LruCache.EvictAction<Value>onEvict
-
Constructor Summary
Constructors Constructor Description LruCache(int maxSizeEntries, long maxSizeBytes, boolean accessOrder)LruCache(int maxSizeEntries, long maxSizeBytes, boolean accessOrder, LruCache.CreateAction<Key,Value> createAction, LruCache.EvictAction<Value> onEvict)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Valueborrow(Key key)Borrows an entry from the cache.private voidevictValue(Value value)Valueget(Key key)Returns an entry from the cache.voidput(Key key, Value value)Returns given value to the cache.voidputAll(java.util.Map<Key,Value> m)Puts all the values from the given map into the cache.
-
-
-
Field Detail
-
onEvict
private final LruCache.EvictAction<Value extends CanEstimateSize> onEvict
-
createAction
private final LruCache.CreateAction<Key,Value extends CanEstimateSize> createAction
-
maxSizeEntries
private final int maxSizeEntries
-
maxSizeBytes
private final long maxSizeBytes
-
currentSize
private long currentSize
-
cache
private final java.util.Map<Key,Value extends CanEstimateSize> cache
-
lock
private final ResourceLock lock
-
-
Constructor Detail
-
LruCache
public LruCache(int maxSizeEntries, long maxSizeBytes, boolean accessOrder)
-
LruCache
public LruCache(int maxSizeEntries, long maxSizeBytes, boolean accessOrder, LruCache.CreateAction<Key,Value> createAction, LruCache.EvictAction<Value> onEvict)
-
-
Method Detail
-
evictValue
private void evictValue(Value value)
-
get
public Value get(Key key)
Returns an entry from the cache.- Specified by:
getin interfaceGettable<Key,Value extends CanEstimateSize>- Parameters:
key- cache key- Returns:
- entry from cache or null if cache does not contain given key.
-
borrow
public Value borrow(Key key) throws java.sql.SQLException
Borrows an entry from the cache.- Parameters:
key- cache key- Returns:
- entry from cache or newly created entry if cache does not contain given key.
- Throws:
java.sql.SQLException- if entry creation fails
-
put
public void put(Key key, Value value)
Returns given value to the cache.- Parameters:
key- keyvalue- value
-
-