examples package: Caching

The service CachedWeatherResponseDemo.gloop caches the current weather forecast provided by a mock
weather service1, then deletes the entry 10 seconds after its creation. This service has been exposed as a REST API
and can be accessed through the following URL2:

Try it!

Under the Coder Navigator, expand the examplespackage entry and navigate to the
code folder. Afterwards, look for the cachingDemo package. This package contains the
files and/or directories shown below:

Under this package is the CachedWeatherResponseDemo.gloop service which demonstrates cache usage and the
cachingDemo.api.CachedMockedWeatherApi.api file, which exposes the CachedWeatherResponseDemo.gloop service
as a REST endpoint.

By running the service multiple times3, you will notice that after a certain amount of time, the response the
service gives will be the same. When the cache expires, it will respond with a new set of data and will be returning the
same data for a specific amount of time again.

See comments for documentation

You will find a more detailed explanation about this example you if check the Comments tab
(found at the right side of the Coder UI) of CachedWeatherResponseDemo.gloop. Each step in the service is
also decorated with comments to better explain the implementation.

Examining the service

Each Integrate package contains its own CacheManager. The examples package has one already set up
and its configuration is defined in conf/caches.conf.

1
2
3
4
5
6

examples├──...├──code└──conf├──caches.conf└──...

Inside the caches.conf file, we have a CacheManager configured called cachedWeatherResponse that we use to cache
the response from our exposed REST API.

1
2
3
4
5
6
7
8
9
10

cachedWeatherResponse{provider="guava"//(required)//Specifiesthateachentryshouldbeautomaticallyremovedfromthecacheonceafixeddurationhaselapsed//aftertheentry's creation, or the most recent replacement of its value. expireAfterWrite = 10s // (optional) <long>ns | us | ms | s | m | h | d default is ms // Each entry should be automatically removed from the cache once a fixed duration has elapsed // after the entry'screation,themostrecentreplacementofitsvalue,oritslastaccess.//expireAfterAccess=5s//(optional)<long>ns|us|ms|s|m|h|ddefaultisms}

If you want to set up your own CacheManager, simply create a caches.conf file for your package and define your
CacheManager there. You can define multiple CacheManagers in the caches.conf file.

TORO Integrate currently supports Ehcache and Guava. Below is a sample configuration you can
use to set up your own CacheManager: