🎯Redis

What is Redis?

Redis (Remote Dictionary Server) is an open-source in-memory data structure store that can be used as a database, cache, and message broker. Redis supports a wide range of data structures like strings, hashes, lists, sets, and sorted sets.

Why use Redis with Spring Boot?

Redis can be used as a caching solution to improve application performance by reducing the number of times data needs to be fetched from the database. Additionally, Redis can be used as a messaging platform to allow for asynchronous communication between services.

Redis Operations

  • SET key value: Set the value of a key.

  • GET key: Get the value of a key.

  • DEL key: Delete a key.

  • EXISTS key: Check if a key exists.

  • INCR key: Increment the value of a key by 1.

  • DECR key: Decrement the value of a key by 1.

  • HSET key field value: Set the value of a field in a hash.

  • HGET key field: Get the value of a field in a hash.

  • HGETALL key: Get all fields and values in a hash.

  • LPUSH key value: Push a value onto the front of a list.

  • RPUSH key value: Push a value onto the end of a list.

  • LPOP key: Remove and get the first element in a list.

  • RPOP key: Remove and get the last element in a list.

  • SADD key member: Add a member to a set.

  • SMEMBERS key: Get all members of a set.

  • ZADD key score member: Add a member to a sorted set with a score.

  • ZRANGE key start stop: Get a range of members in a sorted set.

Redis Integration with Spring Boot

Spring Boot Redis Starter

The spring-boot-starter-data-redis dependency provides auto-configuration for Redis in Spring Boot applications. It includes the necessary Redis client and connection pool dependencies.

RedisTemplate

The RedisTemplate class is the main interface for interacting with Redis in Spring Boot. Here are some of its most commonly used methods:

  • opsForValue(): returns a ValueOperations object for performing value-based operations

  • opsForList(): returns a ListOperations object for performing list-based operations

  • opsForSet(): returns a SetOperations object for performing set-based operations

  • opsForZSet(): returns a ZSetOperations object for performing sorted set-based operations

  • opsForHash(): returns a HashOperations object for performing hash-based operations

Redis Repositories

Spring Data Redis also provides a repository abstraction for working with Redis data structures. Here are some of the most commonly used interfaces:

  • RedisRepository: a generic repository interface for working with Redis

  • KeyValueRepository: a repository interface for working with key-value data structures

  • ListRepository: a repository interface for working with list data structures

  • SetRepository: a repository interface for working with set data structures

  • ZSetRepository: a repository interface for working with sorted set data structures

  • HashRepository: a repository interface for working with hash data structures

Redis Cache

Spring Boot provides a caching abstraction that can be used with Redis as the caching provider. To enable Redis caching, the @EnableCaching annotation needs to be added to a configuration class.

Spring Boot provides support for caching data in Redis using the @Cacheable and @CacheEvict annotations. Here's how to use them:

@Service
public class MyService {
 
  @Cacheable("my-cache")
  public String getFromCache(String key) {
    // This method will only be executed once for a given key, as the result will be cached
  }
 
  @CacheEvict("my-cache")
  public void removeFromCache(String key) {
    // This method will remove the value associated with the given key from the cache
  }
 
}

In order to use caching, you'll need to configure it in your application.properties file:

spring.cache.type=redis
spring.cache.cache-names=my-cache

Redis Pub/Sub

Redis can be used as a messaging platform by utilizing Redis' Pub/Sub system. Spring Boot provides a RedisMessageListenerContainer and a RedisMessageListenerAdapter that can be used to subscribe to Redis channels and receive messages.

Redisson

Redisson is a Redis client for Java that provides a wide range of features like distributed locks, collections, and objects. Redisson can be easily integrated with Spring Boot applications through the redisson-spring-boot-starter dependency.

Last updated