如何在java中使用RedisTemplate删除Redis中的所有数据

问题描述 投票:0回答:3

我使用 Lettuce 作为我的 Java Spring 项目的 Redis 客户端。我正在使用 RedisTemplate 进行多项操作。我无法使用 RedisTemplate 删除 Redis 中的所有数据。

我试过了

redisTemplate.delete("*")

但是,这并没有带来任何改变。

java redis spring-data-redis
3个回答
7
投票

尝试

redisTemplate.getConnectionFactory().getConnection().flushAll();

0
投票

如果您在可挂起函数中使用 kotlin 和反应式连接,代码为:

suspend fun flushCache(): String {
   return redisTemplate.connectionFactory.reactiveConnection.serverCommands().flushDb().awaitSingle()
}

0
投票

redisTemplate.getConnectionFactory().getConnection().flushAll();

这个东西也被弃用了 你可以试试这个

    RedisConnection redisConnection = this.redisTemplate.getConnectionFactory().getConnection();

    RedisSerializer<String> redisSerializer = this.redisTemplate.getKeySerializer();

    DefaultStringRedisConnection defaultStringRedisConnection = new DefaultStringRedisConnection(redisConnection, redisSerializer);

    defaultStringRedisConnection.flushAll();
© www.soinside.com 2019 - 2024. All rights reserved.