Partner – Microsoft – NPI (cat= Spring)
announcement - icon

Azure Spring Apps is a fully managed service from Microsoft (built in collaboration with VMware), focused on building and deploying Spring Boot applications on Azure Cloud without worrying about Kubernetes.

And, the Enterprise plan comes with some interesting features, such as commercial Spring runtime support, a 99.95% SLA and some deep discounts (up to 47%) when you are ready for production.

>> Learn more and deploy your first Spring Boot app to Azure.

You can also ask questions and leave feedback on the Azure Spring Apps GitHub page.

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:

@Configuration
@EnableCaching
public class SpringCachingConfig {

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

We also need SpringCacheCustomizer:

@Component
public class SpringCacheCustomizer implements CacheManagerCustomizer<ConcurrentMapCacheManager> {

    @Override
    public void customize(ConcurrentMapCacheManager cacheManager) {
        cacheManager.setCacheNames(asList("hotels"));
    }
}

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:

@Cacheable("hotels")
public List<Hotel> getAllHotels() {
    return hotelRepository.getAllHotels();
}

4. Setting TTL 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:

caching.spring.hotelListTTL=43200000

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

caching:
  spring:
    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.

Course – LS (cat=Spring)

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

>> THE COURSE
res – REST with Spring (eBook) (everywhere)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.