Skip to content

Martini Cache Functions

Martini's Cache class encompasses a range of functions essential for cache manipulation in services and Groovy code. These functions allow developers to perform operations like data retrieval, storage, and cache management effectively.

Configuration

Setting Up caches.conf

For the Cache Functions to be operational, a configuration file named caches.conf must be placed in the /conf directory of your Martini package. This configuration, using the HOCON format, details the properties and behavior of your caches. The general structure of this configuration is as follows:

1
2
3
4
5
cacheName {
    provider = ("guava"|"ehcache"|"redis")
    (expireAfterWrite|expireAfterAccess) = (number, milliseconds by default)
    // Additional provider-specific properties
}

Elements: - cacheName: The designated name for the cache. - provider: The cache provider, which can be Guava, Ehcache, or Redis. - expireAfterAccess: Duration in milliseconds before eviction post last access. - expireAfterWrite: Duration in milliseconds before eviction post creation/replacement.

Activation of Changes

Modifications in the caches.conf file require a restart of the Martini package to take effect.

Cache Providers

Guava Cache

Guava Cache is an in-memory caching mechanism by Google, applicable to a single application instance. Its configurations include: - initialCapacity: Sets the initial size of internal hash tables. - heap: Defines the maximum number of cache entries.

Example configuration:

1
2
3
4
5
6
myGuavaCache {
    provider = "guava"
    expireAfterWrite = 30s
    heap = 1000
    initialCapacity = 100
}

Ehcache

Ehcache is a scalable, Java-based cache provider for both in-memory and disk storage. Its configurable properties are: - heap, offHeap, disk: Specify sizes for respective storage types. - diskStore: Path for disk storage, necessary for disk persistence. - key and value: Settings for element type and serializer.

Example configuration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
myEhcache {
    provider = "ehcache"
    expireAfterWrite = 30m
    expireAfterAccess = 1h
    heap = 100
    offHeap = 10MB
    disk = 20MB
    diskStore = /tmp/disk
    // Key and Value configurations
}

Redis

Redis is a distributed, in-memory database known for high availability and optional durability. Its additional properties include: - connectionName: Designates the Redis database connection. - codecClassName: Specifies the RedisCodec implementation. - asyncWrites: Boolean setting for asynchronous write operations.

Example configuration:

1
2
3
4
5
6
myRedisCache {
    provider = "redis"
    connectionName = "MyRedisConnection"
    expireAfterWrite = 30s
    asyncWrites = false
}

Utilizing Cache Functions

Cache Functions can be integrated into your service through the “cache” folder found in the functions node of your navigator. Integration methods include drag-and-drop or using content assist with the cache. keyword.

Implementation

Select the relevant function and tailor its input properties to your specific needs. This flexibility enables efficient and dynamic cache management within Martini services.