Use Azure Redis Cache in Spring

Azure Cache for Redis provides an in-memory data store based on the Redis software. Redis improves the performance and scalability of an application that uses backend data stores heavily.

This tutorial demonstrates how to use a Redis cache to store and retrieve data in a Spring Boot application.

Prerequisites

Code the application

To use a Redis cache to store and retrieve data, configure the application by using the following steps.

  1. Configure Redis cache credentials in the application.properties configuration file, as shown in the following example.

    # Specify the DNS URI of your Redis cache.
    spring.data.redis.host=<your-redis-name>.redis.cache.windows.net
    
    # Specify the port for your Redis cache.
    spring.data.redis.port=6379
    
    # Specify the access key for your Redis cache.
    spring.data.redis.password=<your-redis-access-key>
    

    Note

    If you were using a different Redis client like Jedis that enables SSL, you would specify that you want to use SSL in your application.properties file and use port 6380. For example:

    # Specify the DNS URI of your Redis cache.
    spring.data.redis.host=<your-redis-name>.redis.cache.windows.net
    # Specify the access key for your Redis cache.
    spring.data.redis.password=<your-redis-access-key>
    # Specify that you want to use SSL.
    spring.data.redis.ssl.enabled=true
    # Specify the SSL port for your Redis cache.
    spring.data.redis.port=6380
    

    For more information, see Quickstart: Use Azure Cache for Redis in Java.

  2. Edit the startup class file to show the following content. This code stores and retrieves data.

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    
    @SpringBootApplication
    public class DemoCacheApplication implements CommandLineRunner {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(DemoCacheApplication.class);
    
        @Autowired
        private StringRedisTemplate template;
    
        public static void main(String[] args) {
            SpringApplication.run(DemoCacheApplication.class, args);
        }
    
        @Override
        public void run(String... args) {
            ValueOperations<String, String> ops = this.template.opsForValue();
            String key = "testkey";
            if(!this.template.hasKey(key)){
                ops.set(key, "Hello World");
                LOGGER.info("Add a key is done");
            }
            LOGGER.info("Return the value from the cache: {}", ops.get(key));
        }
    
    }
    
  3. Start the application. The application will retrieve data from your Redis cache. You'll see logs similar to the following example:

    Add a key is done
    Return the value from the cache: Hello World
    

Deploy to Azure Spring Apps

Now that you have the Spring Boot application running locally, it's time to move it to production. Azure Spring Apps makes it easy to deploy Spring Boot applications to Azure without any code changes. The service manages the infrastructure of Spring applications so developers can focus on their code. Azure Spring Apps provides lifecycle management using comprehensive monitoring and diagnostics, configuration management, service discovery, CI/CD integration, blue-green deployments, and more. To deploy your application to Azure Spring Apps, see Deploy your first application to Azure Spring Apps.

Next steps

To learn more about Spring and Azure, continue to the Spring on Azure documentation center.