examples package: Caching
cachingDemo.CachedWeatherResponseDemo.gloop service caches the current weather forecast provided by a mock
weather service1, then deletes the entry
10 seconds after it has been created. This service has been exposed as a
REST API and can be accessed through the following URL2:
Please see the following articles for more information:
1 2 3 4 5 6 7 8
Under this package is the
CachedWeatherResponseDemo.gloop service which demonstrates cache usage and the
cachingDemo.api.CachedMockedWeatherApi.api file, which exposes the
as a REST endpoint.
By running the service multiple times3 within the cache entry expiry time (10 seconds by default), the response of the service will be the same. When the cache entry expires, it will respond with a new set of data and will be returning that same data for that same specific amount of time again.
The service will also log messages showing whether the cache was used or not. When the cache doesn't have the entry the service is looking for, it will log a message similar to the following:
If you keep executing the service within the configured expiry time of seeing the previous message, it will log the following:
After the cache entry has expired, the first log messages will appear again, showing that the cache has removed the entry (as configured).
See comments for documentation
You will find a more detailed explanation about this example you if check the Comments tab
(found within the Properties view) of
CachedWeatherResponseDemo.gloop. Each step in the service is
also decorated with comments4 to better explain the implementation.
Each Martini package contains its own
examples package has one already set up
and its configuration is defined in
1 2 3 4 5 6
caches.conf file, there is a
CacheManager configured called
cachedWeatherResponse that's used to
cache the response from the exposed REST API.
1 2 3 4 5 6 7 8 9 10
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. Once configured,
restart the package for the caches to be configured and started.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
The format above uses HOCON - a superset of JSON. At minimum, a cache is configurable with just:
1 2 3 4 5 6
based on a city-country combination ↩
<host>must be substituted with the base URL of your Martini instance. This URL will depend on the where your instance is hosted and how you've configured your server. ↩
You can do this easily via REST API calls to
Open the service file to see comments. Make sure comments are also not hidden. ↩