SpringBoot 3.2.0 结合Redisson接入Redis
    		       		warning:
    		            这篇文章距离上次修改已过423天,其中的内容可能已经有所变动。
    		        
        		                
                在Spring Boot 3.2.0中,你可以使用Lettuce作为Redis的Java客户端库与Redis进行交互。以下是一个基本的配置示例和使用RedisTemplate的示例代码。
- 添加依赖到你的pom.xml:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>io.lettuce.core</groupId>
    <artifactId>lettuce-core</artifactId>
</dependency>- 在application.properties或application.yml中配置Redis:
# application.properties
spring.redis.host=localhost
spring.redis.port=6379或者使用YAML格式:
# application.yml
spring:
  redis:
    host: localhost
    port: 6379- 注入RedisTemplate并使用:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
 
@Component
public class RedisService {
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
 
    public void setKeyValue(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }
 
    public Object getKeyValue(String key) {
        return redisTemplate.opsForValue().get(key);
    }
}确保你的Spring Boot版本与Redis客户端库版本兼容。上述代码提供了一个简单的RedisService类,它使用RedisTemplate来设置和获取键值对。在实际应用中,你可能需要根据自己的需求进行更复杂的配置和编码。
评论已关闭