Class IndexedDiskCache<K,V>
java.lang.Object
org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCache<K,V>
org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCacheEventLogging<K,V>
org.apache.commons.jcs3.auxiliary.disk.AbstractDiskCache<K,V>
org.apache.commons.jcs3.auxiliary.disk.indexed.IndexedDiskCache<K,V>
- All Implemented Interfaces:
AuxiliaryCache<K,,V> ICache<K,,V> ICacheType
Disk cache that uses a RandomAccessFile with keys stored in memory. The maximum number of keys
stored in memory is configurable. The disk cache tries to recycle spots on disk to limit file
expansion.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassClass for recycling and lru.classClass for recycling and lru.protected static final classDeprecated.Use lambda insteadNested classes/interfaces inherited from class org.apache.commons.jcs3.auxiliary.disk.AbstractDiskCache
AbstractDiskCache.MyCacheListenerNested classes/interfaces inherited from interface org.apache.commons.jcs3.engine.behavior.ICacheType
ICacheType.CacheType -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final StringCache name used in log messagesprotected ReentrantReadWriteLockUse this lock to synchronize reads and writes to the underlying storage mechanism.Fields inherited from interface org.apache.commons.jcs3.engine.behavior.ICache
NAME_COMPONENT_DELIMITER -
Constructor Summary
ConstructorsConstructorDescriptionIndexedDiskCache(IndexedDiskCacheAttributes cacheAttributes) Constructor for the DiskCache object.IndexedDiskCache(IndexedDiskCacheAttributes cattr, IElementSerializer elementSerializer) Constructor for the DiskCache object. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidAdd descriptor to recycle bin if it is not null.protected booleancheckForDedOverlaps(IndexedDiskElementDescriptor[] sortedDescriptors) Detects any overlapping elements.protected voidInternal method that handles the disposal.protected voidPerforms the check for optimization, and if it is required, do it.voiddump()For debugging.voiddump(boolean dumpValues) For debugging.This returns the generic attributes for an auxiliary cache.protected longReturns the number of bytes that are free.protected longThis is for debugging and testing.protected StringThis is used by the event logging.Return the keys in this cache.protected intReturns the size of the recycle bin in number of elements.protected intReturns the number of times we have used spots from the recycle bin.intgetSize()Returns the current cache size.Returns info about the disk cache.protected intThis is exposed for testing.protected voidloadKeys()Loads the keys from the .key file.protected voidFile optimization is handled by this method.voidDispose of the disk cache in a background thread.protected ICacheElement<K,V> processGet(K key) Gets the key, then goes to disk to get the object.Map<K,ICacheElement<K, V>> processGetMatching(String pattern) Gets matching items from the cache.protected booleanprocessRemove(K key) Returns true if the removal was successful; or false if there is nothing to remove.voidRemove all the items from the disk cache by resetting everything.protected voidprocessUpdate(ICacheElement<K, V> ce) Update the disk cache.protected voidsaveKeys()Saves key file to disk.Methods inherited from class org.apache.commons.jcs3.auxiliary.disk.AbstractDiskCache
dispose, doDispose, doGet, doGetMatching, doRemove, doRemoveAll, doUpdate, get, getCacheName, getCacheType, getEventLoggingExtraInfo, getMatching, getStats, getStatus, isAlive, remove, removeAll, setAlive, updateMethods inherited from class org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCacheEventLogging
disposeWithEventLogging, getMatchingWithEventLogging, getMultiple, getMultipleWithEventLogging, getWithEventLogging, removeAllWithEventLogging, removeWithEventLogging, updateWithEventLoggingMethods inherited from class org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCache
createICacheEvent, createICacheEvent, getCacheEventLogger, getElementSerializer, getKeyMatcher, logApplicationEvent, logError, logICacheEvent, processGetMultiple, setCacheEventLogger, setElementSerializer, setKeyMatcher
-
Field Details
-
logCacheName
Cache name used in log messages -
storageLock
Use this lock to synchronize reads and writes to the underlying storage mechanism.
-
-
Constructor Details
-
IndexedDiskCache
Constructor for the DiskCache object.- Parameters:
cacheAttributes-
-
IndexedDiskCache
Constructor for the DiskCache object.- Parameters:
cattr-elementSerializer- used if supplied, the super's super will not set a null
-
-
Method Details
-
loadKeys
Loads the keys from the .key file. The keys are stored in a HashMap on disk. This is converted into a LRUMap. -
checkForDedOverlaps
Detects any overlapping elements. This expects a sorted list.The total length of an item is IndexedDisk.RECORD_HEADER + ded.len.
- Parameters:
sortedDescriptors-- Returns:
- false if there are overlaps.
-
saveKeys
Saves key file to disk. This converts the LRUMap to a HashMap for deserialization. -
processUpdate
Update the disk cache. Called from the Queue. Makes sure the Item has not been retrieved from purgatory while in queue for disk. Remove items from purgatory when they go to disk.- Specified by:
processUpdatein classAbstractAuxiliaryCacheEventLogging<K,V> - Parameters:
ce- The ICacheElement<K, V> to put to disk.
-
processGet
Gets the key, then goes to disk to get the object.- Specified by:
processGetin classAbstractAuxiliaryCacheEventLogging<K,V> - Parameters:
key-- Returns:
- ICacheElement<K, V> or null
- See Also:
-
processGetMatching
Gets matching items from the cache.- Specified by:
processGetMatchingin classAbstractAuxiliaryCacheEventLogging<K,V> - Parameters:
pattern-- Returns:
- a map of K key to ICacheElement<K, V> element, or an empty map if there is no data in cache matching keys
-
getKeySet
Return the keys in this cache.- Specified by:
getKeySetin interfaceAuxiliaryCache<K,V> - Specified by:
getKeySetin classAbstractDiskCache<K,V> - Returns:
- a set of the key type TODO This should probably be done in chunks with a range passed in. This will be a problem if someone puts a 1,000,000 or so items in a region.
- Throws:
IOException- if access to the auxiliary cache fails- See Also:
-
processRemove
Returns true if the removal was successful; or false if there is nothing to remove. Current implementation always result in a disk orphan.- Specified by:
processRemovein classAbstractAuxiliaryCacheEventLogging<K,V> - Parameters:
key-- Returns:
- true if at least one item was removed.
-
processRemoveAll
Remove all the items from the disk cache by resetting everything.- Specified by:
processRemoveAllin classAbstractAuxiliaryCacheEventLogging<K,V>
-
processDispose
Dispose of the disk cache in a background thread. Joins against this thread to put a cap on the disposal time.TODO make dispose window configurable.
- Specified by:
processDisposein classAbstractAuxiliaryCacheEventLogging<K,V>
-
disposeInternal
Internal method that handles the disposal. -
addToRecycleBin
Add descriptor to recycle bin if it is not null. Adds the length of the item to the bytes free.This is called in three places: (1) When an item is removed. All item removals funnel down to the removeSingleItem method. (2) When an item on disk is updated with a value that will not fit in the previous slot. (3) When the max key size is reached, the freed slot will be added.
- Parameters:
ded-
-
doOptimizeRealTime
Performs the check for optimization, and if it is required, do it. -
optimizeFile
File optimization is handled by this method. It works as follows:- Shutdown recycling and turn on queuing of puts.
- Take a snapshot of the current descriptors. If there are any removes, ignore them, as they will be compacted during the next optimization.
- Optimize the snapshot. For each descriptor:
- Obtain the write-lock.
- Shift the element on the disk, in order to compact out the free space.
- Release the write-lock. This allows elements to still be accessible during optimization.
- Obtain the write-lock.
- All queued puts are made at the end of the file. Optimize these under a single write-lock.
- Truncate the file.
- Release the write-lock.
- Restore system to standard operation.
-
getSize
Returns the current cache size. -
getRecyleBinSize
Returns the size of the recycle bin in number of elements.- Returns:
- The number of items in the bin.
-
getRecyleCount
Returns the number of times we have used spots from the recycle bin.- Returns:
- The number of spots used.
-
getBytesFree
Returns the number of bytes that are free. When an item is removed, its length is recorded. When a spot is used form the recycle bin, the length of the item stored is recorded.- Returns:
- The number bytes free on the disk file.
-
getDataFileSize
This is for debugging and testing.- Returns:
- the length of the data file.
- Throws:
IOException
-
dump
For debugging. This dumps the values by default. -
dump
For debugging.- Parameters:
dumpValues- A boolean indicating if values should be dumped.
-
getAuxiliaryCacheAttributes
Description copied from interface:AuxiliaryCacheThis returns the generic attributes for an auxiliary cache. Most implementations will cast this to a more specific type.- Returns:
- Returns the AuxiliaryCacheAttributes.
-
getStatistics
Returns info about the disk cache.- Specified by:
getStatisticsin interfaceAuxiliaryCache<K,V> - Overrides:
getStatisticsin classAbstractDiskCache<K,V> - Returns:
- the historical and statistical data for a region's auxiliary cache.
- See Also:
-
getTimesOptimized
This is exposed for testing.- Returns:
- Returns the timesOptimized.
-
getDiskLocation
This is used by the event logging.- Specified by:
getDiskLocationin classAbstractDiskCache<K,V> - Returns:
- the location of the disk, either path or ip.
-