CacheFunctions
The Cache
class contains functions for cache usage in
Martini. You can use these functions in both Gloop and Groovy
code. Below is a snippet showing how to use Cache
's instance extension functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Cache configuration
Before functions from the CacheFunctions
class can be used, the caches.conf
file must be added under the /conf
directory of the Martini package where cache manipulation will take place. Using this file, you will
be able to configure the caches of the Martini package using the HOCON format. For example:
1 2 3 4 |
|
... where:
cacheName
is the name of the cacheprovider
defines the cache provider to useexpireAfterAccess
is the duration in milliseconds before a cache entry is evicted after the entry is created, replaced, or last accessedexpireAfterWrite
is the duration in milliseconds before a cache entry is evicted after the entry is created or last replaced
Cache config changes
After saving the caches.conf
file, you will need to restart the package for the changes to take effect.
Cache providers
Guava Cache
Google's Guava Cache is a caching utility that uses an in-memory only caching mechanism. Caches created by this provider are local to a single run of an application only (or in this case, a single run of a Martini package). Guava caches provide the following additional properties which can be defined in the cache configuration file:
initialCapacity
, which sets the minimum size of Guava Cache's internal hash tablesheap
, which sets the maximum number of entries that the cache may hold
Below is a sample Guava cache configuration:
1 2 3 4 5 6 |
|
Ehcache
Ehcache is a full-featured Java-based cache provider. It supports caches that store data on disk or in memory. It is also built to be scalable and can be tuned for loads requiring high concurrency. Ehcache provides the following additional properties that you can use on the cache configuration file:
heap
, which is the maximum number of entries that the cache may holdoffHeap
, which is the size of the off-heap memorydisk
, which is the size of the disk storagediskStore
, which is the path of the disk storage; required if disk persistence is usedkey
type
, which is the class of the key elementsserializer
, which is the class of the key serializer
value
type
, which is the class of the value elementsserializer
, which is the class of the value serializer
Below is a sample Ehcache configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Redis
Redis is an in-memory data structure project implementing a distributed, in-memory key-value database with optional durability. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster. Redis provides the following additional properties that you can use on the cache configuration file:
connectionName
, which sets the Redis database connection to use.codecClassName
, which sets the implementation of theRedisCodec
to use.asyncWrites
, which is a boolean property (defaults tofalse
) that tells the cache whether to perform write operations asynchronously.
Below is a sample Redis cache configuration:
1 2 3 4 5 6 |
|