我使用 Lettuce 作为我的 Java Spring 项目的 Redis 客户端。我正在使用 RedisTemplate 进行多项操作。我无法使用 RedisTemplate 删除 Redis 中的所有数据。
我试过了
redisTemplate.delete("*")
但是,这并没有带来任何改变。
尝试
redisTemplate.getConnectionFactory().getConnection().flushAll();
如果您在可挂起函数中使用 kotlin 和反应式连接,代码为:
suspend fun flushCache(): String {
return redisTemplate.connectionFactory.reactiveConnection.serverCommands().flushDb().awaitSingle()
}
redisTemplate.getConnectionFactory().getConnection().flushAll();
这个东西也被弃用了 你可以试试这个
RedisConnection redisConnection = this.redisTemplate.getConnectionFactory().getConnection();
RedisSerializer<String> redisSerializer = this.redisTemplate.getKeySerializer();
DefaultStringRedisConnection defaultStringRedisConnection = new DefaultStringRedisConnection(redisConnection, redisSerializer);
defaultStringRedisConnection.flushAll();