The sweetest fruit salad recipe for energy saving services

Chapter 6. Caching

Pau García
DEXMA Engineering Blog
2 min readMar 1, 2021

--

(This story begins in Chapter 0. Launching an energy-saving rocket to the stars)

Our service receives IoT trends that come every hour from a large number of hardware devices, which do not change very often. One of the steps in our service logic consists of retrieving the datasource assigned to the IoT device. Since it would result in a call to our API for each device and hour, based on the initial assumption, we decided to add a client-side cache to keep this configuration and therefore avoid calling our API unnecessarily.

We are using Caffeine, a client-side cache, configured as follows:

In our maven’s pom file:

And defining injection as configuration:

At this point, we agreed to keep each cache element during one day. After that, a new trend of such device will perform a real call to the API. This way we considerably increase the performance of our component but, more important, we protect our internal service of thousands of unnecessary hourly calls.

Cache is injected when constructing our repository, and used as follows:

Our trip continues in Chapter 7. Human-friendly software lifecycle where we explain how we used Cucumber as a BDD adoption with Kotlin

--

--

Pau García
DEXMA Engineering Blog

Software Engineer who loves to find the simplest and most robust solution for each use case by applying architectural, design and clean-code principles