Redis Cache with Java RestAPI
General Setup Process

Redis (Remote Dictionary Server) is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. It is widely used as a cache mechanism because of its fast read/write speeds and support for various data structures like strings, hashes, lists, sets, and sorted sets.
In this section, we will discuss how Redis cache can be integrated with a Java application to improve its performance.
Setting up Redis Server: To use Redis as a cache mechanism, you need to set up a Redis server on your machine. The Redis server can be installed on a Linux or Windows operating system. You can refer to the official Redis documentation for instructions on how to install Redis on your system.
Maven Dependencies: To integrate Redis with a Java application, you need to add the following dependencies to your Maven pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.2-jre</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
Redis Configuration: To use Redis with a Java application, you need to configure it in a configuration file. The following code demonstrates how to configure Redis in a Java application:
@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setHostName("localhost");
jedisConnectionFactory.setPort(6379);
return jedisConnectionFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
@Bean
public CacheManager cacheManager() {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate());
cacheManager.setDefaultExpiration(300);
return cacheManager;
}
}
In the above code, we are configuring Redis to connect to the localhost on port 6379. We are also configuring a RedisTemplate, which is a high-level data access object that can be used to interact with Redis.
Caching the API endpoint: To cache an API endpoint, you need to use the @Cacheable annotation on the endpoint method. The following code demonstrates how to cache an API endpoint:
@RestController
@RequestMapping("/api")
public class RedisController {
private final RedisTemplate<String, Object> template;
@Autowired
public RedisController(RedisTemplate<String, Object> template) {
this.template = template;
}
@Cacheable(value = "data", key = "#id")
@GetMapping("/{id}")
public String getData(@PathVariable String id) {
String data = "Data for id: " + id;
template.opsForValue().set(id, data);
return data;
}
}
In the above example, the API endpoint is cached in the "data" cache, and the key is set to the id parameter passed in the API call. The first time the API is called, the data is fetched and stored in the Redis cache. Subsequent calls to the same API with the same id will retrieve the data from the cache instead of fetching it from the database.
Redis cache provides significant performance benefits compared to fetching data from the database every time, especially when the data is static or frequently accessed. With Redis cache, you can store data in memory, making it accessible quickly, and reducing the load on your database.
This is a basic setup for Redis cache with a Java Rest API. You can customize it further based on your requirements.
Here is an example of setting up Redis Cache with a Java Rest API using the Jedis library:
Add the Jedis library as a dependency in your project. You can add the following to your pom.xml if you are using Maven:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
Connect to Redis Cache in your code.
import redis.clients.jedis.Jedis;
Jedis jedis = new Jedis("localhost");
Store a value in the cache:
jedis.set("key", "value");
Retrieve a value from the cache:
String value = jedis.get("key");
Close the connection to Redis:
jedis.close();
This is just a basic example to get you started. You can find more information and examples in the Jedis documentation: https://github.com/xetorthio/jedis/wiki
About the Creator
srikanth yandrapu
Technical Writer



Comments
There are no comments for this story
Be the first to respond and start the conversation.