This [[Module Library|XQuery Module]] provides functions to organize values in a main-memory key value store.
Caching The store is advisable useful if data (a system configuration, maps serving as indexes) needs to be repeatedly accessed. The cache store is persistent: Contents will be written to disk at shutdown time, and the serialized cache store will be retrieved from disk as soon as the cache store is used for the first time. The cache store will be stored in a binary {{Code|cachestore.basex}} file in the database directory.
In addition, custom caches stores can be read and written. Custom cache files use stores have filenames with the filename pattern {{Code|cachestore-NAME.basex}}. The implicit write of the standard cache store at shutdown time will be disabled if a custom cache store is used.
Functions of this module are non-deterministic and side-effecting: Updates will immediately be visible, and a repeated call of the same function may yield different results if the contents of the cache store have changed.
=Conventions=
All functions and errors in this module are assigned to the <code><nowiki>http://basex.org/modules/cachestore</nowiki></code> namespace, which is statically bound to the {{Code|cachestore}} prefix.<br/>
=Cache Store Entries=
==cachestore:get==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|cachestore:get|$key as xs:string|item()*}}<br/ >
|-
|'''Summary'''
|Retrieves an entry from the cache store with the given {{Code|$key}}. If the addressed entry does not exist, an empty sequence is returned.
|}
==cachestore:put==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|cachestore:put|$key as xs:string, $value as item()*|empty-sequence()}}<br/ >
|-
|'''Summary'''
|Stores an entry with the given {{Code|$key}} and {{Code|$value}} in the cachestore:
* If the value is an empty sequence, the entry is removed.
* If a value refers to an opened database or is [[Lazy Module|a lazy item]], its contents are materialized in main memory.
|}
==cachestore:get-or-put==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|cachestore:get-or-put|$key as xs:string, $put as function() as item()*|item()*}}<br/ >
|-
|'''Summary'''
|Retrieves an entry from the cache store with the given {{Code|$key}}. The {{Code|$put}} function will only be invoked if the entry does not exist, and its result will be stored and returned instead.
|}
==cachestore:remove==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|cachestore:remove|$key as xs:string|empty-sequence()}}<br/ >
|-
|'''Summary'''
|Removes an entry with the given {{Code|$key}} from the cachestore. No error will be raised if an addressed entry does not exist.
|}
==cachestore:keys==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|cachestore:keys||xs:string*}}
|-
|'''Summary'''
|}
==cachestore:clear==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|cachestore:clear||empty-sequence()}}<br/ >
|-
|'''Summary'''
|Resets the cache store by removing all its entries.
|}
=Cache Store Operations=
==cachestore:read==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|cachestore:read||empty-sequence()}}<br/ >{{Func|cachestore:read|$name as xs:string|empty-sequence()}}
|-
|'''Summary'''
|Retrieves the standard cache store from disk, or a custom cache store if a {{Code|$name}} is supplied.
|-
|'''Errors'''
|{{Error|io|#Errors}} The cache store could not be read.<br/>{{Error|name|#Errors}} The specified name is invalid.<br/>{{Error|not-found|#Errors}} A cache store with the specified name does not exist.
|}
==cachestore:write==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|cachestore:write||empty-sequence()}}<br/ >{{Func|cachestore:write|$name as xs:string|empty-sequence()}}
|-
|'''Summary'''
|Writes the standard cache store to disk, or to a custom cache store file if a {{Code|$name}} is supplied. If the standard cache store is empty, the cache store file will be deleted.
|-
|'''Errors'''
|{{Error|io|#Errors}} The cache store could not be written.<br/>{{Error|name|#Errors}} The specified name is invalid.
|}
==cachestore:list==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|cachestore:list||xs:string*}}
|-
|'''Summary'''
|Lists the names of all custom cachesstores.
|}
==cachestore:delete==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|cachestore:delete|$name as xs:string|empty-sequence()}}
|-
|'''Summary'''
|Deletes a custom cache store from disk.
|-
|'''Errors'''
|{{Error|name|#Errors}} The specified name is invalid.<br/>{{Error|not-found|#Errors}} A cache store with the specified name does not exist.
|}
<syntaxhighlight lang="xquery">
(: store an integer :)
cachestore:put('version', 1),
(: retrieve existing or new value, store an element :)
let $license := cachestore:get-or-put('license', function() { 'free' })return cachestore:put('info', <info>{ $license = 'free' ?? 'Free' !! 'Professional' } License</info>),
(: store a map :)
cachestore:put('data', map { 'year': 2022 }),
(: serialize configuration to disk :)
cachestore:write()
</syntaxhighlight>
<syntaxhighlight lang="xquery">
cachestore:get('version')
</syntaxhighlight>
The cache store will still be available if BaseX is restarted until it is cleared.
'''Use Case 2: Create index for fast lookup operations in the GUI:
return map:entry($religion, data($country/@name))
)
return cachestore:put('religions', $map)
</syntaxhighlight>
<syntaxhighlight lang="xquery">
cachestore:get('religions')?Buddhism
</syntaxhighlight>
Note that the cache store will eventually be written to disk unless it is invalidated before closing the GUI.
=Errors=
|-
|{{Code|io}}
| The cache store could not be read or written.
|-
|{{Code|name}}
|-
|{{Code|not-found}}
| A cache store with the specified name does not exist.
|}