Skip to content

Overview

Development

Overview

IDEs

API Explorer

CacheMethods

The CacheMethods class contains one-liners for cache usage in TORO Integrate. You can use these methods in both Gloop and Groovy code. Below is a snippet showing how to use CacheMethods's instance extension methods:

Sample Gloop service showing how to use the `CacheMethods` one-liners

 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

Configuration

Before methods from the CacheMethods class can be used, the caches.conf file must be added under the /conf directory of the Integrate package where cache manipulation will take place. Using this file, you will be able to configure the caches of the Integrate package using the HOCON format. For example:

1
2
3
4
cacheName {
    provider = ("guava"|"ehcache")
    (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 an Integrate 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
}