CacheManager
final class ConcurrentCache extends java.lang.Object implements CacheManager
java.util.concurrent
package. It allows multiple threads to
access the cache concurrently without blocking each other, given that they
request different objects and the requested objects are present in the
cache.
All methods of this class should be thread safe. When exclusive access to an
entry is required, it is achieved by calling the lock()
method
on the CacheEntry
object. To ensure that the entry is always
unlocked, all calls to CacheEntry.lock()
should be followed by
a try
block with a finally
clause that unlocks the
entry.
Modifier and Type | Field | Description |
---|---|---|
private java.util.concurrent.ConcurrentHashMap<java.lang.Object,CacheEntry> |
cache |
Map with all the cached objects.
|
private BackgroundCleaner |
cleaner |
Background cleaner which can be used to clean cached objects in a
separate thread to avoid blocking the user threads.
|
private boolean |
collectAccessCounts |
Flag that tells if hit/miss/eviction counts should be collected.
|
private java.util.concurrent.atomic.AtomicLong |
evictions |
The number of evictions from the cache.
|
private java.util.concurrent.atomic.AtomicLong |
hits |
The number of cache hits.
|
private CacheableFactory |
holderFactory |
Factory which creates
Cacheable s. |
private int |
maxSize |
The maximum size (number of elements) for this cache.
|
private java.lang.Object |
mbean |
The identifier of the MBean that allows monitoring of this instance.
|
private java.util.concurrent.atomic.AtomicLong |
misses |
The number of cache misses.
|
private java.lang.String |
name |
Name of this cache.
|
private ReplacementPolicy |
replacementPolicy |
Replacement policy to be used for this cache.
|
private boolean |
stopped |
Flag that indicates whether this cache instance has been shut down.
|
Constructor | Description |
---|---|
ConcurrentCache(CacheableFactory holderFactory,
java.lang.String name,
int initialSize,
int maxSize) |
Creates a new cache manager.
|
Modifier and Type | Method | Description |
---|---|---|
void |
ageOut() |
Remove all objects that are not kept and not dirty.
|
void |
clean(Matchable partialKey) |
Clean all dirty objects matching a partial key.
|
void |
cleanAll() |
Clean all dirty objects in the cache.
|
(package private) void |
cleanAndUnkeepEntry(CacheEntry entry,
Cacheable item) |
Clean an entry in the cache and decrement its keep count.
|
private void |
cleanCache(Matchable partialKey) |
Clean all dirty objects matching a partial key.
|
(package private) void |
cleanEntry(CacheEntry entry) |
Clean an entry in the cache.
|
private void |
countEviction() |
Count an eviction from the cache.
|
private void |
countHit() |
Count a cache hit.
|
private void |
countMiss() |
Count a cache miss.
|
Cacheable |
create(java.lang.Object key,
java.lang.Object createParameter) |
Create an object in the cache.
|
void |
deregisterMBean() |
Deregister the MBean that monitors this cache.
|
boolean |
discard(Matchable partialKey) |
Discard all unused objects that match a partial key.
|
(package private) void |
evictEntry(java.lang.Object key) |
Evict an entry to make room for a new entry that is being inserted into
the cache.
|
Cacheable |
find(java.lang.Object key) |
Find an object in the cache.
|
Cacheable |
findCached(java.lang.Object key) |
Find an object in the cache.
|
(package private) long |
getAllocatedEntries() |
Get the number of allocated entries in the cache.
|
(package private) BackgroundCleaner |
getBackgroundCleaner() |
|
(package private) boolean |
getCollectAccessCounts() |
Check if collection of hit/miss/eviction counts is enabled.
|
private CacheEntry |
getEntry(java.lang.Object key) |
Get the entry associated with the specified key from the cache.
|
(package private) long |
getEvictionCount() |
Get the number of evictions from the cache.
|
(package private) long |
getHitCount() |
Get the number of cache hits.
|
(package private) long |
getMaxEntries() |
Get the maximum number of entries in the cache.
|
(package private) long |
getMissCount() |
Get the number of cache misses.
|
(package private) ReplacementPolicy |
getReplacementPolicy() |
Return the
ReplacementPolicy instance for this cache. |
private static java.lang.Object |
getSystemModule(java.lang.String factoryInterface) |
Privileged module lookup.
|
(package private) long |
getUsedEntries() |
Get the number of cached objects.
|
private Cacheable |
insertIntoFreeSlot(java.lang.Object key,
CacheEntry entry) |
Insert a
CacheEntry into a free slot in the
ReplacementPolicy 's internal data structure, and return a
Cacheable that the caller can reuse. |
void |
registerMBean(java.lang.String dbName) |
Register an MBean that allows user to monitor this cache instance.
|
void |
release(Cacheable item) |
Release an object that has been fetched from the cache with
find() , findCached() or create() . |
void |
remove(Cacheable item) |
Remove an object from the cache.
|
private void |
removeEntry(java.lang.Object key) |
Remove an entry from the cache.
|
(package private) void |
setCollectAccessCounts(boolean collect) |
Enable or disable collection of hit/miss/eviction counts.
|
private void |
settingIdentityComplete(java.lang.Object key,
CacheEntry entry,
Cacheable item) |
Complete the setting of the identity.
|
void |
shutdown() |
Shut down the cache.
|
void |
useDaemonService(DaemonService daemon) |
Specify a daemon service that can be used to perform operations in
the background.
|
java.util.Collection<Cacheable> |
values() |
Return a collection view of all the
Cacheable s in the
cache. |
private final java.util.concurrent.ConcurrentHashMap<java.lang.Object,CacheEntry> cache
private final CacheableFactory holderFactory
Cacheable
s.private final java.lang.String name
private final int maxSize
private final ReplacementPolicy replacementPolicy
private java.lang.Object mbean
private volatile boolean collectAccessCounts
private final java.util.concurrent.atomic.AtomicLong hits
private final java.util.concurrent.atomic.AtomicLong misses
private final java.util.concurrent.atomic.AtomicLong evictions
private volatile boolean stopped
find()
, findCached()
and
create()
will return null
. The flag is
declared volatile
so that no synchronization is needed when
it is accessed by concurrent threads.private BackgroundCleaner cleaner
ConcurrentCache(CacheableFactory holderFactory, java.lang.String name, int initialSize, int maxSize)
holderFactory
- factory which creates Cacheable
sname
- the name of the cacheinitialSize
- the initial capacity of the cachemaxSize
- maximum number of elements in the cacheReplacementPolicy getReplacementPolicy()
ReplacementPolicy
instance for this cache.private CacheEntry getEntry(java.lang.Object key)
key
- the identity of the cached objectprivate void removeEntry(java.lang.Object key)
Cacheable
is cleared
and made available for other entries. This method should only be called
if the entry is present in the cache and locked by the current thread.key
- the identity of the entry to removevoid evictEntry(java.lang.Object key)
Cacheable
and set it to
null
. When this method is called, the caller has already chosen
the Cacheable
for reuse. Therefore, this method won't call
CacheEntry.free()
as that would make the Cacheable
free
for reuse by other entries as well.
The caller must have locked the entry that is about to be evicted.
key
- identity of the entry to removeprivate Cacheable insertIntoFreeSlot(java.lang.Object key, CacheEntry entry) throws StandardException
CacheEntry
into a free slot in the
ReplacementPolicy
's internal data structure, and return a
Cacheable
that the caller can reuse. The entry must have been locked
before this method is called.key
- the identity of the object being insertedentry
- the entry that is being insertedCacheable
object that the caller can reuseStandardException
- if an error occurs while inserting the entry
or while allocating a new Cacheable
private void settingIdentityComplete(java.lang.Object key, CacheEntry entry, Cacheable item)
key
- the identity of the object being insertedentry
- the entry which is going to hold the cached objectitem
- a Cacheable
object with the identity set (if
the identity was successfully set), or null
if setting the
identity failedpublic Cacheable find(java.lang.Object key) throws StandardException
release()
is
called.find
in interface CacheManager
key
- identity of the object to findnull
if it cannot be foundStandardException
- Standard Derby error policy.Cacheable.setIdentity(java.lang.Object)
public Cacheable findCached(java.lang.Object key) throws StandardException
null
. The returned object is kept until
release()
is called.findCached
in interface CacheManager
key
- identity of the object to findnull
if it's not in the cacheStandardException
- Standard Derby error policy.public Cacheable create(java.lang.Object key, java.lang.Object createParameter) throws StandardException
release()
is called.create
in interface CacheManager
key
- identity of the object to createcreateParameter
- parameters passed to
Cacheable.createIdentity()
null
if the
object cannot be createdStandardException
- if the object is already in the cache, or
if some other error occursCacheable.createIdentity(Object,Object)
public void release(Cacheable item)
find()
, findCached()
or create()
.release
in interface CacheManager
item
- a Cacheable
valuepublic void remove(Cacheable item) throws StandardException
find()
,
findCached()
or create()
. The user of the
cache must make sure that only one caller executes this method on a
cached object. This method will wait until the object has been removed
(its keep count must drop to zero before it can be removed).remove
in interface CacheManager
item
- the object to remove from the cacheStandardException
- Standard Derby error policy.public void cleanAll() throws StandardException
cleanAll
in interface CacheManager
StandardException
- Standard Derby error policy.Cacheable.clean(boolean)
,
Cacheable.isDirty()
public void clean(Matchable partialKey) throws StandardException
clean
in interface CacheManager
partialKey
- the partial (or exact) key to matchStandardException
- Standard Derby error policy.private void cleanCache(Matchable partialKey) throws StandardException
partialKey
- the partial (or exact) key to match, or
null
to match all keysStandardException
void cleanEntry(CacheEntry entry) throws StandardException
entry
- the entry to cleanStandardException
- if an error occurs while cleaningvoid cleanAndUnkeepEntry(CacheEntry entry, Cacheable item) throws StandardException
Cacheable
.entry
- the entry to cleanitem
- the cached object contained in the entryStandardException
- if an error occurs while cleaningpublic void ageOut()
ageOut
in interface CacheManager
Cacheable.clean(boolean)
,
Cacheable.clearIdentity()
public void shutdown() throws StandardException
shutdown
in interface CacheManager
StandardException
- Standard Derby error policy.public void useDaemonService(DaemonService daemon)
useDaemonService
in interface CacheManager
daemon
- the daemon service to useBackgroundCleaner getBackgroundCleaner()
public boolean discard(Matchable partialKey)
discard
in interface CacheManager
partialKey
- the partial (or exact) key, or null
to
match all keystrue
if all matching objects were removed,
false
otherwisepublic java.util.Collection<Cacheable> values()
Cacheable
s in the
cache. There is no guarantee that the objects in the collection can be
accessed in a thread-safe manner once this method has returned, so it
should only be used for diagnostic purposes. (Currently, it is only used
by the StatementCache
VTI.)values
in interface CacheManager
public void registerMBean(java.lang.String dbName) throws StandardException
CacheManager
Register an MBean that allows user to monitor this cache instance. This is a no-op if the platform does not support Java Management Extensions (JMX).
The MBean will be automatically deregistered when CacheManager.shutdown()
is called, or it can be manually deregistered by calling
CacheManager.deregisterMBean()
.
registerMBean
in interface CacheManager
dbName
- the unique name of the database to which the cache belongsStandardException
- if an error occurs when registering the MBeanpublic void deregisterMBean()
CacheManager
deregisterMBean
in interface CacheManager
private void countHit()
private void countMiss()
private void countEviction()
void setCollectAccessCounts(boolean collect)
boolean getCollectAccessCounts()
long getHitCount()
long getMissCount()
long getEvictionCount()
long getMaxEntries()
long getAllocatedEntries()
long getUsedEntries()
private static java.lang.Object getSystemModule(java.lang.String factoryInterface)
Apache Derby V10.14 Internals - Copyright © 2004,2018 The Apache Software Foundation. All Rights Reserved.