[[sfs_cache_store]]
==== Single File Store

The single file cache store keeps all data in a single file. The way it
looks up data is by keeping an in-memory index of keys and the positions of
their values in this file. This results in greater performance compared to old
file cache store. There is one caveat though. Since the single file based
cache store keeps keys in memory, it can lead to increased memory consumption,
and hence it's not recommended for caches with big keys.

In certain use cases, this cache store suffers from fragmentation: if you
store larger and larger values, the space is not reused and instead the entry
is appended at the end of the file. The space (now empty) is reused only if you
write another entry that can fit there. Also, when you remove all entries from
the cache, the file won't shrink, and neither will be de-fragmented.

These are the available configuration options for the single file cache store:

* `path` where data will be stored. (e.g., `path="/tmp/myDataStore"`).
By default, the location is `{brandname}-SingleFileStore`.

* `max-entries` specifies the maximum number of entries to keep in this file
store. As mentioned before, in order to speed up lookups, the single file
cache store keeps an index of keys and their corresponding position in the
file. To avoid this index resulting in memory consumption problems, this
cache store can bounded by a maximum number of entries that it stores. If
this limit is exceeded, entries are removed permanently using the LRU
algorithm both from  the in-memory index and the underlying file based
cache store. So, setting a maximum limit only makes sense when {brandname} is
used as a cache, whose contents can be recomputed or they can be retrieved
from the authoritative data store. If this maximum limit is set when the
{brandname} is used as an authoritative data store, it could lead to data
loss, and hence it's not recommended for this use case. The default value is
`-1` which means that the file store size is unlimited.

//Exclude segmentation support from productized docs.
ifndef::productized[]
===== Segmentation support
The single file cache store supports segmentation and creates a separate
instance per segment. Segmentation results in multiple directories under the
configured directory, where each directory is a number that represents the
segment to which the data maps.
endif::productized[]

===== Configuration
The following examples show single file cache store configuration:

[source,xml,options=nowrap]
----
<persistence>
   <file-store path="/tmp/myDataStore" max-entries="5000"/>
</persistence>
----

[source,java,options=nowrap]
----
ConfigurationBuilder b = new ConfigurationBuilder();
b.persistence()
    .addSingleFileStore()
    .location("/tmp/myDataStore")
    .maxEntries(5000);
----
