DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Best Practices for Securing Infrastructure as Code (Iac) In the DevOps SDLC
  • Revolutionizing Algorithmic Trading: The Power of Reinforcement Learning
  • Operator Overloading in Java
  • Authorization: Get It Done Right, Get It Done Early

Trending

  • Best Practices for Securing Infrastructure as Code (Iac) In the DevOps SDLC
  • Revolutionizing Algorithmic Trading: The Power of Reinforcement Learning
  • Operator Overloading in Java
  • Authorization: Get It Done Right, Get It Done Early
  1. DZone
  2. Data Engineering
  3. Data
  4. Quick Start: How to Use Spring Cache on Redis

Quick Start: How to Use Spring Cache on Redis

Is your Spring app running? Well, you better go cache it!

Nikita Koksharov user avatar by
Nikita Koksharov
·
Updated Nov. 29, 19 · Tutorial
Like (8)
Save
Tweet
Share
36.17K Views

Join the DZone community and get the full member experience.

Join For Free

Dog catching tennis ball in field

Is your Spring app running? Well, you better go cache it!

Spring is a highly popular application framework for the Java programming language that makes it easier to build enterprise Java software. Since version 3.1, Spring has supported adding caching to existing Spring applications, in order to improve performance and decrease response times.

Redis is an open-source, in-memory data structure store that can be used to build NoSQL databases. However, Redis does not include pre-built support for either Java in general or application frameworks such as Spring.

You may also like: How to Boost Redis With Local Caching in Java

The good news is that you can perform caching with Spring and Redis by using a third-party Redis Java client, such as Redisson. Redisson is fully compatible with the Spring framework. In this article, we’ll discuss how you can use Redisson to perform caching in Spring with Redis.

Caching in Spring

The Spring framework provides a caching abstraction layer. The relevant annotations for caching in Spring are as follows:

  • @Cacheable: The @Cacheable annotation denotes that the results of invoking this method will be cached. If the method is called again with the same arguments, the results will be retrieved from the cache instead of invoking the method.

  • @CachePut: The @CachePut annotation denotes that a method will trigger the cache put operation.

  • @CacheEvict: The @CacheEvict annotation denotes that a method will trigger the cache evict operation.

  • @EnableCaching: The @EnableCaching annotation enables Spring’s built-in cache management capabilities.

  • @Caching: The @Caching annotation is a group annotation for multiple cache annotations.

  • @CacheConfig: The @CacheConfig annotation enables the sharing of common cache-related settings, such as cache name and key generator.

Installing Redisson

Installing Redisson is very simple. If you are using Maven, for example, simply add the below dependency to your pom.xml file:

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.11.5</version>
</dependency>


Caching Parameters in Redisson

Each Spring cache in Redisson has two important parameters, which are specified in milliseconds:

  • ttl: The ttl (time to live) parameter determines the maximum lifetime of an object in the cache. All objects in the cache will be deleted after their time to live has expired, no matter how frequently they are requested.

  • maxIdleTime: The maxIdleTime parameter determines the maximum time that can elapse between requests for an object. If this time goes by without a request, the object is automatically deleted from the cache. This parameter is present only in the Redisson implementation of Spring caching.

If these two parameters are not defined or equal to 0, then objects in the cache will be stored indefinitely.

Example Code for Caching With Spring and Redis

The example code below demonstrates how to perform caching in Spring and Redis with Redisson. This code makes use of two Spring beans: the first to create and configure the Redisson client, and the second to set up and test the cache.

@Configuration
@ComponentScan
@EnableCaching
public class Application {

    @Bean(destroyMethod="shutdown")
    RedissonClient redisson() {
        Config config = new Config();
        config.useSingleServer()
              .setAddress("redis://127.0.0.1:6379");
        return Redisson.create(config);
    }

    @Bean
    CacheManager cacheManager(RedissonClient redissonClient) {
        Map<String, CacheConfig> config = new HashMap<>();

        // create "testMap" spring cache with ttl = 24 minutes and maxIdleTime = 12 minutes
        config.put("testMap", new CacheConfig(24*60*1000, 12*60*1000));
        return new RedissonSpringCacheManager(redissonClient, config);
    }

}


Conclusion

Thanks to the power of local caching, Redisson allows you to boost the performance of your Redis-based Spring cache by up to 45 times for read operations.

Further Reading

How to Boost Redis With Local Caching in Java

Redis Cluster on Java for Scaling and High Availability

Spring Framework Cache (computing) Redis (company) Java (programming language)

Opinions expressed by DZone contributors are their own.

Trending

  • Best Practices for Securing Infrastructure as Code (Iac) In the DevOps SDLC
  • Revolutionizing Algorithmic Trading: The Power of Reinforcement Learning
  • Operator Overloading in Java
  • Authorization: Get It Done Right, Get It Done Early

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: