Skip to content

Toro Cloud Dev Center


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:

Sample service showing how to use the `Cache` functions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// Declare the cache
def discovery = ...

// Add the key "HAL" and set its value to 9000.
discovery.cachePut('HAL', 9000)

// Fetch the value of the key "HAL"
assert discovery.cacheGet('HAL') == 9000

// Removes the key "HAL" and its value
discovery.cacheInvalidate('HAL')

// If we try to fetch the value of "HAL", we should now get null because we have invalidated its entry
assert discovery.cacheGet('HAL') == null

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
cacheName {
    provider = ("guava"|"ehcache"|"redis")
    (expireAfterWrite|expireAfterAccess) = (number, milliseconds by default)
}

... where:

  • cacheName is the name of the cache
  • provider defines the cache provider to use
  • expireAfterAccess is the duration in milliseconds before a cache entry is evicted after the entry is created, replaced, or last accessed
  • expireAfterWrite 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 tables
  • heap, 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
myGuavaCache {
    provider = "guava"
    expireAfterWrite = 30s
    heap = 1000
    initialCapacity = 100
}

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 hold
  • offHeap, which is the size of the off-heap memory
  • disk, which is the size of the disk storage
  • diskStore, which is the path of the disk storage; required if disk persistence is used
  • key
    • type, which is the class of the key elements
    • serializer, which is the class of the key serializer
  • value
    • type, which is the class of the value elements
    • serializer, 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
myEhcache {
    provider = "ehcache"
    expireAfterWrite = 30m
    expireAfterAccess = 1h
    key {
        type = "java.lang.String"       // default
        serializer = ""
    }
    value {
        type = "java.io.Serializable"   // default
        serializer = ""
    }
    heap = 100
    offHeap = 10MB
    disk = 20MB
    diskStore = /tmp/disk
}

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 the RedisCodec to use.
  • asyncWrites, which is a boolean property (defaults to false) that tells the cache whether to perform write operations asynchronously.

Below is a sample Redis cache configuration:

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