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
An Azure subscription - create one for free.
Java Development Kit (JDK) version 8 or higher.
Apache Maven, version 3.0 or higher.
cURL or a similar HTTP utility to test functionality.
A Redis cache instance. If you don't have one, see Quickstart: Create an open-source Redis cache.
A Spring Boot application. If you don't have one, create a Maven project with the Spring Initializr. Be sure to select Maven Project and, under Dependencies, add the Spring Web and Spring Data Reactive Redis dependencies, and then select Java version 8 or higher.
Code the application
To use a Redis cache to store and retrieve data, configure the application by using the following steps.
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.
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)); } }
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.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for