Skip to content

🗄️ Redis 客户端配置指南

📖 功能介绍

TIP

基于Spring Data Redis官方客户端实现,提供高性能的缓存解决方案,支持多种数据类型和连接池管理。

⚙️ 配置说明

1. Redis连接配置

yaml
spring:
  data:
    redis:
      # 基础配置
      database: 0              # 数据库索引
      host: 127.0.0.1         # Redis服务器地址
      port: 6379              # 服务器端口
      password: 123456        # 访问密码
      timeout: 30000          # 连接超时时间(ms)
      
      # 连接池配置
      lettuce:
        pool:
          max-active: 100     # 最大连接数
          max-wait: -1        # 最大阻塞等待时间
          max-idle: 10        # 最大空闲连接
          min-idle: 0         # 最小空闲连接

💡 使用指南

1. String类型操作(推荐)

java
@Service
public class CacheService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
    public void setCache() {
        // 设置缓存
        stringRedisTemplate.opsForValue().set("key", "value");
        
        // 设置过期时间
        stringRedisTemplate.opsForValue().set("key", "value", 1, TimeUnit.HOURS);
        
        // 原子递增
        Long newValue = stringRedisTemplate.opsForValue().increment("counter", 1);
    }
}

2. 泛型类型操作

java
@Service
public class UserCacheService {
    @Autowired
    private RedisTemplate<String, User> redisTemplate;
    
    public void cacheUser(User user) {
        redisTemplate.opsForValue().set("user:" + user.getId(), user);
    }
    
    public User getUser(String userId) {
        return redisTemplate.opsForValue().get("user:" + userId);
    }
}

📝 使用示例

1. 计数器实现

java
@Service
public class CounterService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
    public Long increment(String key) {
        return stringRedisTemplate.opsForValue().increment(key, 1);
    }
    
    public Long getCount(String key) {
        String value = stringRedisTemplate.opsForValue().get(key);
        return value != null ? Long.parseLong(value) : 0L;
    }
}

2. 缓存测试

java
@SpringBootTest
public class RedisTests {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
    @Test
    public void testIncrement() throws InterruptedException {
        String key = "test:counter";
        
        // 测试1000次递增
        for(int i = 0; i < 1000; i++) {
            Long value = stringRedisTemplate.opsForValue().increment(key, 1L);
            System.out.println("当前计数: " + value);
            Thread.sleep(10);
        }
    }
}

⚠️ 注意事项

  1. 连接池配置

    • 合理设置最大连接数
    • 避免连接池溢出
    • 及时释放连接
  2. 数据操作

    • 设置合理的过期时间
    • 避免大key
    • 注意数据序列化

🔍 常见问题

  1. 连接超时

    • 检查网络连接
    • 验证密码正确
    • 确认端口开放
  2. 内存溢出

    • 检查连接池配置
    • 监控内存使用
    • 及时清理过期数据

📚 最佳实践

  1. 键值设计

    java
    // 推荐的键命名方式
    String userKey = "user:profile:" + userId;
    String counterKey = "counter:daily:" + date;
  2. 批量操作

    java
    // 使用管道提高性能
    stringRedisTemplate.executePipelined(new SessionCallback<Object>() {
        @Override
        public Object execute(RedisOperations operations) throws DataAccessException {
            for(int i = 0; i < 1000; i++) {
                operations.opsForValue().set("key" + i, "value" + i);
            }
            return null;
        }
    });

📚 相关资源