Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

NPI – Spring Top – Temp – Non-Geo (Lightrun)

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

NPI – Lightrun – Spring (partner)

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

1. Overview

In this tutorial, we do caching for some basic real-world examples. Notably, we'll demonstrate how we can configure this caching mechanism to be time-limited. We also refer to such time-limitation as time-to-live (TTL) for a cache.

2. Configuration for Spring Caching

Previously, we have demonstrated how we can use @Cacheable annotation from Spring. Meanwhile, a practical use case for caching is when a Hotel booking website's main page is opened frequently. This means that the REST endpoint for providing a list of Hotels is requested often, making frequent calls to the database. Database calls are slower as compared to providing data directly from memory.

First, we'll create SpringCachingConfig:

public class SpringCachingConfig {

    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager("hotels");

We also need SpringCacheCustomizer:

public class SpringCacheCustomizer implements CacheManagerCustomizer<ConcurrentMapCacheManager> {

    public void customize(ConcurrentMapCacheManager cacheManager) {

3. @Cacheable Caching

After setup, we are able to make use of Spring configuration. We can reduce the REST endpoint response time by storing the Hotel objects in memory. We use the @Cacheable annotation for caching a list of Hotel objects as in the code snippet below:

public List<Hotel> getAllHotels() {
    return hotelRepository.getAllHotels();

4. Setting TLL for @Cacheable

However, the cached Hotels list may change in the database over time due to updates, deletions, or additions. We want to refresh the cache by setting a time-to-live interval (TTL), after which the existing cache entries are removed and refilled upon the first call of the method in Section 3 above.

We can do this by making use of @CacheEvict annotation. For instance, in the example below, we set the TTL through caching.spring.hotelListTTL variable:

@CacheEvict(value = "hotels", allEntries = true)
@Scheduled(fixedRateString = "${caching.spring.hotelListTTL}")
public void emptyHotelsCache() {
    logger.info("emptying Hotels cache");

We want the TTL to be 12 hours. The value in terms of milliseconds turns out to be 12 x 3600 x 1000 = 43200000. We define this in environment properties. Additionally, if we are using a properties-based environment configuration, we can set the cache TTL as follows:


Alternatively, if we are using a YAML-based design, we can set it as:

    hotelListTTL: 43200000

5. Conclusion

In this article, we explored how to set TTL caching for Spring-based caching. As always, we can find the complete code over on GitHub.

Spring bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Generic footer banner
Inline Feedbacks
View all comments
Comments are closed on this article!