Skip to content

如何去除Redis缓存

1. 通过Redis CLI命令清除缓存

清除所有缓存

bash
# 连接到Redis
redis-cli

# 清除所有数据库中的所有key
FLUSHDB

# 清除所有数据库中的所有key
FLUSHALL

清除特定前缀的缓存

bash
# 连接到Redis
redis-cli

# 查看所有匹配前缀的key
KEYS prefix:*

# 删除匹配前缀的key
DEL prefix:*

清除特定key

bash
# 连接到Redis
redis-cli

# 删除指定的key
DEL key1 key2 key3

2. 通过应用程序代码清除缓存

Java Spring Boot项目

使用RedisTemplate清除缓存
java
@Autowired
private RedisTemplate<String, Object> redisTemplate;

// 清除所有缓存
public void clearAllCache() {
    redisTemplate.getConnectionFactory().getConnection().flushDb();
}

// 清除特定前缀的缓存
public void clearCacheByPrefix(String prefix) {
    Set<String> keys = redisTemplate.keys(prefix + "*");
    if (!keys.isEmpty()) {
        redisTemplate.delete(keys);
    }
}

// 清除特定key
public void clearCacheByKey(String key) {
    redisTemplate.delete(key);
}
使用@CacheEvict注解清除缓存
java
@CacheEvict(value = "user", allEntries = true)
public void clearUserCache() {
    // 清除user缓存中的所有数据
}

@CacheEvict(value = "product", key = "#productId")
public void clearProductCache(Long productId) {
    // 清除特定产品的缓存
}

Python项目

使用redis-py库清除缓存
python
import redis

# 连接到Redis
r = redis.Redis(host='localhost', port=6379, db=0)

# 清除所有缓存
r.flushdb()

# 清除特定前缀的缓存
def clear_cache_by_prefix(prefix):
    keys = r.keys(f"{prefix}*")
    if keys:
        r.delete(*keys)

# 清除特定key
def clear_cache_by_key(key):
    r.delete(key)

Node.js项目

使用ioredis库清除缓存
javascript
const Redis = require('ioredis');
const redis = new Redis();

// 清除所有缓存
async function clearAllCache() {
    await redis.flushdb();
}

// 清除特定前缀的缓存
async function clearCacheByPrefix(prefix) {
    const keys = await redis.keys(`${prefix}*`);
    if (keys.length > 0) {
        await redis.del(...keys);
    }
}

// 清除特定key
async function clearCacheByKey(key) {
    await redis.del(key);
}

3. 通过重启Redis服务清除缓存

Linux/Mac系统

bash
# 重启Redis服务
sudo systemctl restart redis

# 或者
sudo service redis restart

# 或者
redis-cli shutdown
redis-server

Windows系统

bash
# 通过服务管理器重启Redis服务
# 或者通过命令行
redis-cli shutdown
redis-server.exe

4. 针对特定场景清除缓存

清除用户相关缓存

bash
# 清除用户信息缓存
redis-cli DEL user:info:*
redis-cli DEL user:permissions:*
redis-cli DEL user:profile:*

清除产品相关缓存

bash
# 清除产品信息缓存
redis-cli DEL product:info:*
redis-cli DEL product:list:*
redis-cli DEL product:detail:*

清除配置相关缓存

bash
# 清除配置缓存
redis-cli DEL config:*
redis-cli DEL dict:*
redis-cli DEL system:*

5. 监控Redis缓存使用情况

查看Redis内存使用情况

bash
# 查看Redis内存使用情况
redis-cli INFO memory

# 查看Redis key数量
redis-cli DBSIZE

# 查看特定key的信息
redis-cli INFO keyspace

查看大key

bash
# 查看占用内存最大的key
redis-cli --bigkeys

# 或者使用命令查看
redis-cli --scan --pattern "*" --count 1000

6. 最佳实践

缓存清除策略

  1. 设置过期时间:为缓存数据设置合理的过期时间,避免手动清除
  2. 使用合适的命名空间:为不同类型的缓存使用统一的前缀,便于管理
  3. 定期清理:设置定时任务定期清理过期或无效的缓存
  4. 监控内存使用:定期检查Redis内存使用情况,及时清理

注意事项

  1. 生产环境谨慎操作:在生产环境清除缓存前,先在测试环境验证
  2. 备份重要数据:清除缓存前,确认不会影响系统正常运行
  3. 分批清除:对于大量缓存,建议分批清除,避免一次性清除导致性能问题
  4. 记录操作日志:记录缓存清除操作,便于问题排查和审计

7. 常见问题解决

缓存未生效

  • 检查Redis连接是否正常
  • 确认key的命名是否正确
  • 检查缓存注解的配置是否正确
  • 查看是否有多个Redis实例

内存占用过高

  • 查看是否有大key占用过多内存
  • 检查是否设置了合理的过期时间
  • 考虑使用Redis的内存优化策略
  • 定期清理无效缓存

连接失败

  • 检查Redis服务是否正常运行
  • 确认连接配置(host、port、password)是否正确
  • 检查防火墙设置
  • 查看Redis日志排查问题