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 Copy linkLink copied to clipboard!
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) Copy linkLink copied to clipboard!
Procedure 13.1. Configure the Single File Store
Add the Cache Name
Thenameparameter of thelocal-cacheattribute is used to specify a name for the cache.<local-cache name="default">Per-cache Statistics
Ifstatisticsare enabled at the container level, per-cache statistics can be selectively disabled for caches that do not require monitoring by setting thestatisticsattribute tofalse.<local-cache name="default" statistics="true">Configure the
file-storeElementThefile-storeelement specifies configuration information for the single file store.Thenameparameter of thefile-storeelement is used to specify a name for the file store.<local-cache name="default" statistics="true"> <file-store name="myFileStore" />Set the passivation Parameter
Thepassivationparameter 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" />Set the
purgeParameterThepurgeparameter specifies whether or not the cache store is purged when it is started. Valid values for this parameter aretrueandfalse.<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" />Set the
sharedParameterThesharedparameter 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 aretrueandfalse. However, thesharedparameter is not recommended forfile-store.<local-cache name="default" statistics="true"> <file-store name="myFileStore" passivation="true" purge="true" shared="false" />Specify the Directory Path Within the
relative-toParameterTherelative-toproperty is the directory where thefile-storestores the data. It is used to define a named path.Thepathproperty 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-toproperty 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>Specify the maximum number of entries
ThemaxEntriesparameter 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>Set the fetch-state Parameter
Thefetch-stateparameter 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>Set the preload Parameter
Thepreloadparameter 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>Set the singleton Parameter
Thesingletonparameter 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,singletonparameter 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>
13.1.2. Single File Store Configuration (Library Mode) Copy linkLink copied to clipboard!
Procedure 13.2. Configuring the Single File Store in Library Mode
infinispan.xml.
- Add the name value to the
namedCacheelement. The following is an example of this step:<namedCache name="writeThroughToFile"> - In the
persistenceelement, set thepassivationparameter tofalse. Possible values are true and false.<namedCache name="writeThroughToFile"> <persistence passivation="false" /> - Set up a single file configuration using the
singleFileelement: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
ignoreModificationsparameter 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
purgeOnStartupparameter specifies whether the cache store is purged when initially started. - The
sharedparameter is set totruewhen multiple cache instances share a cache store, which prevents multiple cache instances writing the same modification individually. The default for this attribute isfalse. However, thesharedparameter is not recommended forfile-store. - The
preloadparameter 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
locationparameter points to the location of file store. - The
maxEntriesparameter provides maximum number of entries allowed. The default value is -1 for unlimited entries. - The
maxKeysInMemoryparameter is used to speed up data lookup. The single file store keeps an index of keys and their positions in the file using themaxKeysInMemoryparameter. 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> - Add the
asyncelement to configure the asynchronous settings:- The
enabledparameter determines whether the file store is asynchronous. - The
threadPoolSizeparameter specifies the number of threads that concurrently apply modifications to the store. The default value for this parameter is5. - The
flushLockTimeoutparameter 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
modificationQueueSizeparameter 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 is1024elements. - The
shutdownTimeoutparameter specifies the time to stop the cache store. Default value for this parameter is25seconds.
<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>