Caching Your Spring RestTemplate Get Operations

We just talked about safeguarding RestTemplate HTTP Operations from spitting out exceptions when the API Endpoint is not available. Now we’re going to speed up processing a RestTemplate HTTP Get object with Spring Caching.

We’re using Spring RestTemplate to generate GitHub Project Statistics on the NixMash Spring Home Page. We cover the implementation details in this NixMash Post.

We’re using EhCache 3 as our Spring Cache Provider so we begin by adding a githubStats Cache in our ehcache3.xml file. (For Spring Caching configuration details see this NixMash Post.)

Now we’ll cache the method which retrieves our GitHub Project statistics.

What’s interesting in the above @Cacheable annotation is using the method name as our Cache Key. Let’s show that key in action. Notice the githubCache.get(“getGitHubStats”).get()¬†statement below.

Okay, now for the payoff. Let’s look at the performance improvements of caching our RestTemplate GET object data. Awesome sauce!

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.