tencent cloud

Tencent Cloud Distributed Cache (Redis OSS-Compatible)

Release Notes and Announcements
Release Notes
Announcements
User Tutorial
Product Introduction
Overview
Product Strengths
Use Cases
Storage Engine
Product Series
Product Versions
Specifications and Performance
Read/Write Separation
Multi-AZ Deployment
Regions and AZs
Terms
Service Regions and Service Providers
Purchase Guide
Billing Overview
Pricing Center
Instance Purchasing
Renewal (Yearly/Monthly Subscription)
Refund (Yearly/Monthly Subscription)
Overdue Payments
Switching from Pay-as-You-Go to Yearly/Monthly Subscription
Getting Started
Quickly Creating an Instance
Connecting to Redis Instance
Operation Guide
Operation Overview
Connecting to a Database Instance
Managing Instances
Upgrade Instance
Management Node (Redis/ValKey Edition)
Multi-AZ Deployment Management
Backup and Restoration
Managing Accounts
Parameter Configuration
Slow Query
Access Management
Network and Security
Monitoring and Alarms
Event Management (Redis/ValKey Edition)
Data Migration
Global Replication for Redis Edition
Database Audit
Performance Optimization
Sentinel Mode
Development Guidelines
Naming Rules
Basic Usage Guidelines
Design Principles of Key and Value
Command Usage Guidelines
Design Principles of Client Programs
Connection Pool Configuration
Command Reference
Command Reference Overview
Redis Edition and Valkey Edition Command Compatibility
Version Command Usage Differences
Differences Between the Proxy Architecture and Direct Connection Mode
More Command Operations (Redis/Valkey Edition)
Memcached Edition Command Compatibility
Practical Tutorial
Building TencentDB for Redis® Client Monitoring Based on Spring Boot
Redis Client Connection Configuration Policy and Practice
Global SCAN Guide for Cluster Architecture
Eliminating Instances Securely
Hot Key and Big Key
AZ Migration Scheme
Troubleshooting
Connection Exception
Exception Analysis and Solution of Redisson Client Timeout Reconnection
Performance Troubleshooting and Fine-Tuning
API Documentation
History
Introduction
API Category
Making API Requests
Instance APIs
Parameter Management APIs
Other APIs
Backup and Restoration APIs
Region APIs
Monitoring and Management APIs
Log APIs
Data Types
Error Codes
FAQs
General
Connection and Login
Purchase
Service Agreement
Service Level Agreement
Terms of Service
Glossary
Contact Us

Configuring the Spring Data Redis Component

PDF
Modo Foco
Tamanho da Fonte
Última atualização: 2026-03-17 18:23:47
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);
}
}

Ajuda e Suporte

Esta página foi útil?

comentários