tencent cloud

Configuring the Spring Data Redis Component
Last updated:2025-10-11 14:26:39
Configuring the Spring Data Redis Component
Last updated: 2025-10-11 14:26:39
Spring Data Redis is a subproject under the Spring Data project, designed to provide easy and efficient operation support for TencentDB for Redis®. It provides an abstraction layer for interaction between Spring applications and TencentDB for Redis®, simplifying access and operations on TencentDB for Redis® and standardizing data access methods. With Spring Data Redis, developers can operate TencentDB for Redis® using familiar Spring conventions.

Creating Spring Boot Projects

Create the project RedisMonitor by using IntelliJ IDEA as follows, and select Maven for the type.

Configuring the Spring Data Redis Component Dependency

Add the Spring Data Redis component dependency to pom.xml of the project as follows.
Note:
When Spring Boot detects the spring-boot-starter-data-redis dependency, it automatically creates an instance (Bean) of RedisTemplate and registers it in the Spring container. RedisTemplate is the high-level abstraction provided by Spring Data Redis, which is used to simplify TencentDB for Redis® operations. It encapsulates the underlying TencentDB for Redis® commands and provides a more convenient and secure method to operate TencentDB for Redis®.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Configuring TencentDB for Redis® Information

Configure the host, port, and password parameters of TencentDB for Redis® in the configuration file application.properties, and optionally configure information such as the database as needed. The process is as follows.
# Required configuration.
# Obtain the private IPv4 address and port in the network information area on the instance details page of the TencentDB for Redis® console.
spring.data.redis.host=172.27.XX.XX
spring.data.redis.port=6379
# On the account management page of the TencentDB for Redis® console, the account and password can be obtained. For the default account, just specify a password; for the custom account, set the password to account name@password.
spring.data.redis.password=XXXXXX
# Optional configuration.
# Specify a specific database name.
spring.data.redis.database=X
# Specify the client timeout duration (ms).
spring.data.redis.timeout=XXXX
# Specify the Spring Boot client type.
spring.data.redis.client-type=lettuce

Using RedisTemplate to Implement the API Class for TencentDB for Redis®

Implement a tool class RedisInterface.java based on RedisTemplate, encapsulating common TencentDB for Redis® operation commands.
package com.example.redismonitor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
@Component
public class RedisInterface {
@Autowired
private RedisTemplate redisTemplate;

//- - - - - - - - - - - - - - - - - - - - - string - - - - - - - - - - - - - - - - - - - -
public String get(String key) {
return (String) redisTemplate.opsForValue().get(key);
}

public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}

//- - - - - - - - - - - - - - - - - - - - - set - - - - - - - - - - - - - - - - - - - -
public Long sadd(String key, Object... values) {
return redisTemplate.opsForSet().add(key, values);
}

public Long srem(String key, Object... values) {
return redisTemplate.opsForSet().remove(key, values);
}

public Set<Object> smembers(String key) {
return redisTemplate.opsForSet().members(key);
}

public Boolean sismember(String key, Object value) {
return redisTemplate.opsForSet().isMember(key, value);
}

//- - - - - - - - - - - - - - - - - - - - - hash - - - - - - - - - - - - - - - - - - - -
public void hset(String key, String field, Object value) {
redisTemplate.opsForHash().put(key, field, value);
}

public Object hget(String key, String field) {
return redisTemplate.opsForHash().get(key, field);
}

public Map<Object, Object> hgetAll(String key) {
return redisTemplate.opsForHash().entries(key);
}

public Long hdel(String key, Object... fields) {
return redisTemplate.opsForHash().delete(key, fields);
}

//- - - - - - - - - - - - - - - - - - - - - list - - - - - - - - - - - - - - - - - - - -
public void lpush(String key, Object value) {
redisTemplate.opsForList().leftPush(key, value);
}

public void rpush(String key, Object value) {
redisTemplate.opsForList().rightPush(key, value);
}

public String lpop(String key) {
return redisTemplate.opsForList().leftPop(key).toString();
}

public String rpop(String key) {
return redisTemplate.opsForList().rightPop(key).toString();
}

public List<Object> range(String key, long start, long end) {
return redisTemplate.opsForList().range(key, start, end);
}

//- - - - - - - - - - - - - - - - - - - - - zset - - - - - - - - - - - - - - - - - - - -
public Boolean zadd(String key, Object value, double score) {
return redisTemplate.opsForZSet().add(key, value, score);
}

public Set<Object> zrangeByScore(String key, double min, double max) {
return redisTemplate.opsForZSet().rangeByScore(key, min, max);
}

public Long zrank(String key, Object value) {
return redisTemplate.opsForZSet().rank(key, value);
}

public Long zrem(String key, Object... values) {
return redisTemplate.opsForZSet().remove(key, values);
}
}
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback