Redis 常用操作
基础数据类型
Section titled “基础数据类型”String 字符串
Section titled “String 字符串”SET key valueSET key value EX 3600SET key value PX 60000SETNX key valueGET keyMGET key1 key2 key3INCR counterINCRBY counter 10DECR counterHash 哈希
Section titled “Hash 哈希”HSET user:1 name "张三" age 25HGET user:1 nameHGETALL user:1HMGET user:1 name ageHDEL user:1 ageHEXISTS user:1 nameList 列表
Section titled “List 列表”LPUSH list valueRPUSH list valueLRANGE list 0 -1LINDEX list 0LPOP listRPOP listBLPOP list 5Set 集合
Section titled “Set 集合”SADD set value1 value2SMEMBERS setSISMEMBER set valueSINTER set1 set2SUNION set1 set2SDIFF set1 set2ZSet 有序集合
Section titled “ZSet 有序集合”ZADD rank 100 "user1" 90 "user2"ZRANK rank "user1"ZREVRANK rank "user1"ZRANGE rank 0 9 WITHSCORESZREVRANGE rank 0 9 WITHSCORESZRANGEBYSCORE rank 80 100Spring Boot 集成
Section titled “Spring Boot 集成”spring: data: redis: host: localhost port: 6379 password: database: 0 lettuce: pool: max-active: 8 max-idle: 8 min-idle: 0RedisTemplate 使用
Section titled “RedisTemplate 使用”@Servicepublic class RedisService {
@Autowired private RedisTemplate<String, Object> redisTemplate;
public void set(String key, Object value, long timeout, TimeUnit unit) { redisTemplate.opsForValue().set(key, value, timeout, unit); }
public Object get(String key) { return redisTemplate.opsForValue().get(key); }
public Boolean delete(String key) { return redisTemplate.delete(key); }
public Boolean hasKey(String key) { return redisTemplate.hasKey(key); }
public Boolean expire(String key, long timeout, TimeUnit unit) { return redisTemplate.expire(key, timeout, unit); }}@Servicepublic class UserService {
@Cacheable(value = "user", key = "#id") public User getById(Long id) { return userMapper.selectById(id); }
@CachePut(value = "user", key = "#user.id") public User update(User user) { userMapper.updateById(user); return user; }
@CacheEvict(value = "user", key = "#id") public void delete(Long id) { userMapper.deleteById(id); }}Redisson 实现
Section titled “Redisson 实现”@Configurationpublic class RedissonConfig {
@Bean public RedissonClient redissonClient() { Config config = new Config(); config.useSingleServer() .setAddress("redis://localhost:6379"); return Redisson.create(config); }}
@Servicepublic class OrderService {
@Autowired private RedissonClient redissonClient;
public void createOrder(String orderId) { RLock lock = redissonClient.getLock("order:" + orderId); try { if (lock.tryLock(10, 30, TimeUnit.SECONDS)) { } } finally { if (lock.isHeldByCurrentThread()) { lock.unlock(); } } }}常见应用场景
Section titled “常见应用场景”public User getUserWithCache(Long id) { String key = "user:" + id; User user = (User) redisTemplate.opsForValue().get(key); if (user == null) { user = userMapper.selectById(id); if (user != null) { redisTemplate.opsForValue().set(key, user, 1, TimeUnit.HOURS); } } return user;}public boolean allowRequest(String key, int limit, int period) { String redisKey = "rate:" + key; Long count = redisTemplate.opsForValue().increment(redisKey); if (count == 1) { redisTemplate.expire(redisKey, period, TimeUnit.SECONDS); } return count <= limit;}- Key 命名规范:
业务:模块:ID,如user:profile:1 - 设置过期时间: 避免内存溢出
- 避免大 Key: 单个 Key 的 Value 不要超过 10KB
- 批量操作: 使用 Pipeline 或 Lua 脚本
- 序列化: 统一使用 JSON 序列化