Chapter 13. Cache Stores
- fetch data from the data store when a copy is not in the cache.
- push modifications made to the data in cache back to the data store.
13.1. File System Based Cache Stores
SingleFileCacheStore
.
SingleFileCacheStore
is a simple, file system based implementation and a replacement to the older file system based cache store: the FileCacheStore
.
SingleFileCacheStore
stores all key/value pairs and their corresponding metadata information in a single file. To speed up data location, it also keeps all keys and the positions of their values and metadata in memory. Hence, using the single file cache store slightly increases the memory required, depending on the key size and the amount of keys stored. Hence SingleFileCacheStore
is not recommended for use cases where the keys are too big.
SingleFileCacheStore
can be used in a limited capacity in production environments. It can not be used on shared file system (such as NFS and Windows shares) due to a lack of proper file locking, resulting in data corruption. Furthermore, file systems are not inherently transactional, resulting in file writing failures during the commit phase if the cache is used in a transactional context.
13.1.1. Single File Store Configuration (Remote Client-Server Mode)
Procedure 13.1. Configure the Single File Store
Add the Cache Name
Thename
parameter of thelocal-cache
attribute is used to specify a name for the cache.<local-cache name="default">
<local-cache name="default">
Copy to Clipboard Copied! Per-cache Statistics
Ifstatistics
are enabled at the container level, per-cache statistics can be selectively disabled for caches that do not require monitoring by setting thestatistics
attribute tofalse
.<local-cache name="default" statistics="true">
<local-cache name="default" statistics="true">
Copy to Clipboard Copied! Configure the
file-store
ElementThefile-store
element specifies configuration information for the single file store.Thename
parameter of thefile-store
element is used to specify a name for the file store.<local-cache name="default" statistics="true"> <file-store name="myFileStore" />
<local-cache name="default" statistics="true"> <file-store name="myFileStore" />
Copy to Clipboard Copied! Set the passivation Parameter
Thepassivation
parameter determines whether entries in the cache are passivated (true
) or if the cache store retains a copy of the contents in memory (false
).<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" />
<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" />
Copy to Clipboard Copied! Set the
purge
ParameterThepurge
parameter specifies whether or not the cache store is purged when it is started. Valid values for this parameter aretrue
andfalse
.<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" />
<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" />
Copy to Clipboard Copied! Set the
shared
ParameterTheshared
parameter is used when multiple cache instances share a cache store. This parameter can be set to prevent multiple cache instances writing the same modification multiple times. Valid values for this parameter aretrue
andfalse
. However, theshared
parameter is not recommended forfile-store
.<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" shared="false" />
<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" shared="false" />
Copy to Clipboard Copied! Specify the Directory Path Within the
relative-to
ParameterTherelative-to
property is the directory where thefile-store
stores the data. It is used to define a named path.Thepath
property is the name of the file where the data is stored. It is a relative path name that is appended to the value of therelative-to
property to determine the complete path.<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" shared="false" relative-to="{PATH}" path="{DIRECTORY}" /> </local-cache>
<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" shared="false" relative-to="{PATH}" path="{DIRECTORY}" /> </local-cache>
Copy to Clipboard Copied! Specify the maximum number of entries
ThemaxEntries
parameter provides maximum number of entries allowed. The default value is -1 for unlimited entries.<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" shared="false" relative-to="{PATH}" path="{DIRECTORY}" max-entries="10000"/> </local-cache>
<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" shared="false" relative-to="{PATH}" path="{DIRECTORY}" max-entries="10000"/> </local-cache>
Copy to Clipboard Copied! Set the fetch-state Parameter
Thefetch-state
parameter when set to true fetches the persistent state when joining a cluster. If multiple cache stores are chained, only one of them can have this property enabled. Persistent state transfer with a shared cache store does not make sense, as the same persistent store that provides the data will just end up receiving it. Therefore, if a shared cache store is used, the cache does not allow a persistent state transfer even if a cache store has this property set to true. It is recommended to set this property to true only in a clustered environment. The default value for this parameter is false.<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" shared="false" relative-to="{PATH}" path="{DIRECTORY}" max-entries="10000" fetch-state="true"/> </local-cache>
<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" shared="false" relative-to="{PATH}" path="{DIRECTORY}" max-entries="10000" fetch-state="true"/> </local-cache>
Copy to Clipboard Copied! Set the preload Parameter
Thepreload
parameter when set to true, loads the data stored in the cache store into memory when the cache starts. However, setting this parameter to true affects the performance as the startup time is increased. The default value for this parameter is false.<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" shared="false" relative-to="{PATH}" path="{DIRECTORY}" max-entries="10000" fetch-state="true" preload="false"/> </local-cache>
<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" shared="false" relative-to="{PATH}" path="{DIRECTORY}" max-entries="10000" fetch-state="true" preload="false"/> </local-cache>
Copy to Clipboard Copied! Set the singleton Parameter
Thesingleton
parameter enables a singleton store cache store. SingletonStore is a delegating cache store used when only one instance in a cluster can interact with the underlying store. However,singleton
parameter is not recommended forfile-store
.<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" shared="false" relative-to="{PATH}" path="{DIRECTORY}" max-entries="10000" fetch-state="true" preload="false" singleton="true"/> </local-cache>
<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" shared="false" relative-to="{PATH}" path="{DIRECTORY}" max-entries="10000" fetch-state="true" preload="false" singleton="true"/> </local-cache>
Copy to Clipboard Copied!
13.1.2. Single File Store Configuration (Library Mode)
Procedure 13.2. Configuring the Single File Store in Library Mode
infinispan.xml
.
- Add the name value to the
namedCache
element. The following is an example of this step:<namedCache name="writeThroughToFile">
<namedCache name="writeThroughToFile">
Copy to Clipboard Copied! - In the
persistence
element, set thepassivation
parameter tofalse
. Possible values are true and false.<namedCache name="writeThroughToFile"> <persistence passivation="false" />
<namedCache name="writeThroughToFile"> <persistence passivation="false" />
Copy to Clipboard Copied! - Set up a single file configuration using the
singleFile
element:fetchPersistentState
- If set totrue
, the persistent state is fetched when joining a cluster. If multiple cache stores are chained, only one cache store can have this property set totrue
. The default for this value isfalse
.- The
ignoreModifications
parameter determines whether operations that modify the cache (e.g. put, remove, clear, store, etc.) do not affect the cache store. As a result, the cache store can become out of sync with the cache. - The
purgeOnStartup
parameter specifies whether the cache store is purged when initially started. - The
shared
parameter is set totrue
when multiple cache instances share a cache store, which prevents multiple cache instances writing the same modification individually. The default for this attribute isfalse
. However, theshared
parameter is not recommended forfile-store
. - The
preload
parameter sets whether the cache store data is pre-loaded into memory and becomes immediately accessible after starting up. The disadvantage of setting this to true is that the start up time increases. The default value for this attribute isfalse
. - The
location
parameter points to the location of file store. - The
maxEntries
parameter provides maximum number of entries allowed. The default value is -1 for unlimited entries. - The
maxKeysInMemory
parameter is used to speed up data lookup. The single file store keeps an index of keys and their positions in the file using themaxKeysInMemory
parameter. The default value for this parameter is -1.
<namedCache name="writeThroughToFile"> <persistence passivation="false"> <singleFile fetchPersistentState="true" ignoreModifications="false" purgeOnStartup="false" shared="false" preload="false" location="/tmp/Another-FileCacheStore-Location" maxEntries="100" maxKeysInMemory="100"> </singleFile> </persistence> </namedCache>
<namedCache name="writeThroughToFile"> <persistence passivation="false"> <singleFile fetchPersistentState="true" ignoreModifications="false" purgeOnStartup="false" shared="false" preload="false" location="/tmp/Another-FileCacheStore-Location" maxEntries="100" maxKeysInMemory="100"> </singleFile> </persistence> </namedCache>
Copy to Clipboard Copied! - Add the
async
element to configure the asynchronous settings:- The
enabled
parameter determines whether the file store is asynchronous. - The
threadPoolSize
parameter specifies the number of threads that concurrently apply modifications to the store. The default value for this parameter is5
. - The
flushLockTimeout
parameter specifies the time to acquire the lock which guards the state to be flushed to the cache store periodically. The default value for this parameter is1
. - The
modificationQueueSize
parameter specifies the size of the modification queue for the asynchronous store. If updates are made at a rate that is faster than the underlying cache store can process this queue, then the asynchronous store behaves like a synchronous store for that period, blocking until the queue can accept more elements. The default value for this parameter is1024
elements. - The
shutdownTimeout
parameter specifies the time to stop the cache store. Default value for this parameter is25
seconds.
<namedCache name="writeThroughToFile"> <persistence passivation="false"> <singleFile fetchPersistentState="true" ignoreModifications="false" purgeOnStartup="false" shared="false" preload="false" location="/tmp/Another-FileCacheStore-Location" maxEntries="100" maxKeysInMemory="100"> <async enabled="true" threadPoolSize="500" flushLockTimeout="1" modificationQueueSize="1024" shutdownTimeout="25000"/> </singleFile> </persistence> </namedCache>
<namedCache name="writeThroughToFile"> <persistence passivation="false"> <singleFile fetchPersistentState="true" ignoreModifications="false" purgeOnStartup="false" shared="false" preload="false" location="/tmp/Another-FileCacheStore-Location" maxEntries="100" maxKeysInMemory="100"> <async enabled="true" threadPoolSize="500" flushLockTimeout="1" modificationQueueSize="1024" shutdownTimeout="25000"/> </singleFile> </persistence> </namedCache>
Copy to Clipboard Copied!