The FRED Client version 0.9.11 adds caching functionality that will increase performance significantly for applications that make repeated calls to the St. Louis Federal Reserve for the same data.

Introduction to Caching with The FRED Client

When the application invokes the doGet method on the QueryBuilder a URI is created and we can use this URI as a key, since it is unique and will be associated with exactly one object.

An attempt will be made to lookup the object associated with this key in the cache — if a non-null reference is returned, then no call to the St. Louis Fed is executed since the application already has this result in memory, otherwise the web service call is made and the resultant object is added to the cache using the URI as a key — the object is then returned to the caller.

The script we used to perform this test was executed in the demonstration application and is shown here:

return queryBuilder
    .series ()
    .observations ()
    .setSeriesId("EXJPUS")
    .doGet(Observations.class)

The URI that this script generates is below and this can be used as a key:

https://api.stlouisfed.org/fred/series/observations?api_key=[redacted]&series_id=EXJPUS

In this example we used a simple HashMap to cache calls to the St. Louis Fed. While a HashMap may be appropriate for a simple test like this, a serious application would want to evict data from the cache every now and then, as well as take advantage of other features that a full-blown cache such as JBoss Infinispan, Oracle Coherence, GigaSpaces, IBM WebSphere eXtreme Scale, etc. provide.

Note that the cache is passed in to the QueryBuilder via a constructor parameter — the example below demonstrates how this is accomplished.

Map cache = new HashMap ();

CacheServiceProviderSpecification cacheProvider =
    new MapCompliantCacheServiceProvider (cache);

QueryBuilder queryBuilder = new QueryBuilder (restTemplate, uri, cacheProvider);

Performance Comparison

Below we have a table which contains a side-by-side comparison between the performance of the FRED Client without caching and with caching enabled.

Note that in each example we make the call five times.

Name Value without caching (ms) Value with caching (ms)
LastValue 1959.0 21.0
Avg 1038.6 281.6
Total 5193.0 1408.0

Leave a Reply