Package com.strobel.collections
Class Cache<K,V>
java.lang.Object
com.strobel.collections.Cache<K,V>
- Direct Known Subclasses:
SatelliteCache
,ThreadLocalCache
,ThreadLocalIdentityCache
,TopLevelCache
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract V
Places a value in the cache only if no value exists with the same key.boolean
Gets a value indicating whether a cached value exists for the given key.boolean
Gets a value indicating whether the cached value matches the given value for a specified key.static <K,
V> Cache <K, V> Creates an unsynchronized, concurrency-unsafe Level 1 cache that can only be used safely by a single thread.static <K,
V> Cache <K, V> createSatelliteCache
(Cache<K, V> parent) Creates an unsynchronized, concurrency-unsafe Level 2 cache that can only be used safely by a single thread.static <K,
V> Cache <K, V> Creates an unsynchronized, concurrency-unsafe Level 1 cache that can only be used safely by a single thread.static <K,
V> Cache <K, V> createSatelliteIdentityCache
(Cache<K, V> parent) Creates an unsynchronized, concurrency-unsafe Level 2 cache that can only be used safely by a single thread.static <K,
V> Cache <K, V> Creates a Level 1 cache that internally maintains a separate satellite cache for each thread that accesses it.static <K,
V> Cache <K, V> createThreadLocalCache
(Cache<K, V> parent) Creates a Level 2 cache that internally maintains a separate satellite cache for each thread that accesses it.static <K,
V> Cache <K, V> Creates a Level 1 cache that internally maintains a separate satellite cache for each thread that accesses it.static <K,
V> Cache <K, V> createThreadLocalIdentityCache
(Cache<K, V> parent) Creates a Level 2 cache that internally maintains a separate satellite cache for each thread that accesses it.static <K,
V> Cache <K, V> Creates a concurrency-safe Level 1 cache that may be used in isolation or as the root cache in a multi-level cache design.abstract V
Gets the value associated with the given key.Returns a thread-specific satellite cache chained to this cache.abstract boolean
Replaces the value associated with a given key if the current value matches the expected value.
-
Constructor Details
-
Cache
protected Cache()
-
-
Method Details
-
contains
Gets a value indicating whether a cached value exists for the given key. -
contains
Gets a value indicating whether the cached value matches the given value for a specified key. -
getSatelliteCache
Returns a thread-specific satellite cache chained to this cache. If the target cache is already a satellite cache, it will simply return itself. Note that the returned cache is completely unsynchronized and is not safe for concurrent access.- Returns:
- A thread-specific satellite cache.
-
replace
Replaces the value associated with a given key if the current value matches the expected value.
Note that the replaced value will not be propagated to child caches that already have a value for the same key. As such, this method is of limited usefulness and should only be called on isolated Level 1 caches.
- Parameters:
key
- The key for which to change the associated value.expectedValue
- The expected value to be replaced.updatedValue
- The new value.- Returns:
true
if the expected value was replaced; otherwise,false
.
-
get
Gets the value associated with the given key.- Parameters:
key
- The key associated with the desired value.- Returns:
- The value corresponding the given key, or
null
if no value was found.
-
cache
Places a value in the cache only if no value exists with the same key.- Parameters:
key
- The key associated with the given value.value
- The value to insert into the cache.- Returns:
- The cached value associated with the given key, which will be the provided value if no existing value was found.
-
createTopLevelCache
Creates a concurrency-safe Level 1 cache that may be used in isolation or as the root cache in a multi-level cache design.- Type Parameters:
K
- The type of keys used to identify values in the cache.V
- The type of values stored in the cache.- Returns:
- The newly created cache.
-
createSatelliteCache
Creates an unsynchronized, concurrency-unsafe Level 1 cache that can only be used safely by a single thread.- Type Parameters:
K
- The type of keys used to identify values in the cache.V
- The type of values stored in the cache.- Returns:
- The newly created cache.
-
createSatelliteCache
Creates an unsynchronized, concurrency-unsafe Level 2 cache that can only be used safely by a single thread. On a cache miss, the parent cache will be checked. On an insert, the value will propagate up to the parent cache.- Type Parameters:
K
- The type of keys used to identify values in the cache.V
- The type of values stored in the cache.- Returns:
- The newly created cache.
-
createSatelliteIdentityCache
Creates an unsynchronized, concurrency-unsafe Level 1 cache that can only be used safely by a single thread. Keys are compared by reference identity.- Type Parameters:
K
- The type of keys used to identify values in the cache.V
- The type of values stored in the cache.- Returns:
- The newly created cache.
-
createSatelliteIdentityCache
Creates an unsynchronized, concurrency-unsafe Level 2 cache that can only be used safely by a single thread. On a cache miss, the parent cache will be checked. On an insert, the value will propagate up to the parent cache. Keys are compared by reference identity.- Type Parameters:
K
- The type of keys used to identify values in the cache.V
- The type of values stored in the cache.- Returns:
- The newly created cache.
-
createThreadLocalCache
Creates a Level 1 cache that internally maintains a separate satellite cache for each thread that accesses it.- Type Parameters:
K
- The type of keys used to identify values in the cache.V
- The type of values stored in the cache.- Returns:
- The newly created cache.
-
createThreadLocalIdentityCache
Creates a Level 1 cache that internally maintains a separate satellite cache for each thread that accesses it. Keys are compared by reference identity.- Type Parameters:
K
- The type of keys used to identify values in the cache.V
- The type of values stored in the cache.- Returns:
- The newly created cache.
-
createThreadLocalCache
Creates a Level 2 cache that internally maintains a separate satellite cache for each thread that accesses it. On a cache miss, the parent cache will be checked. On an insert, the value will propagate up to the parent cache.- Type Parameters:
K
- The type of keys used to identify values in the cache.V
- The type of values stored in the cache.- Returns:
- The newly created cache.
-
createThreadLocalIdentityCache
Creates a Level 2 cache that internally maintains a separate satellite cache for each thread that accesses it. On a cache miss, the parent cache will be checked. On an insert, the value will propagate up to the parent cache. Keys are compared by reference identity.- Type Parameters:
K
- The type of keys used to identify values in the cache.V
- The type of values stored in the cache.- Returns:
- The newly created cache.
-