spring 如何使用 redis 模板从 Redis 获取所有密钥
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19098079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get all Keys from Redis using redis template
提问by user1744099
I have been stuck with this problem with quite some time.I want to get keys from redis using redis template. I tried this.redistemplate.keys("*"); but this doesn't fetch anything. Even with the pattern it doesn't work.
我已经被这个问题困扰了很长时间。我想使用 redis 模板从 redis 获取密钥。我试过 this.redistemplate.keys("*"); 但这并没有得到任何东西。即使有模式它也不起作用。
Can you please advise on what is the best solution to this.
您能否就什么是最好的解决方案提出建议。
回答by prashanth-g
I just consolidated the answers, we have seen here.
我刚刚整合了答案,我们在这里看到了。
Here are the two ways of getting keys from Redis, when we use RedisTemplate.
当我们使用 RedisTemplate 时,这里有两种从 Redis 获取密钥的方法。
1. Directly from RedisTemplate
1.直接来自RedisTemplate
Set<String> redisKeys = template.keys("samplekey*"));
// Store the keys in a List
List<String> keysList = new ArrayList<>();
Iterator<String> it = redisKeys.iterator();
while (it.hasNext()) {
String data = it.next();
keysList.add(data);
}
Note: You should have configured redisTemplate with StringRedisSerializerin your bean
注意:您应该在 bean 中使用StringRedisSerializer配置 redisTemplate
If you use java based bean configuration
如果您使用基于 java 的 bean 配置
redisTemplate.setDefaultSerializer(new StringRedisSerializer());
If you use spring.xml based bean configuration
如果您使用基于 spring.xml 的 bean 配置
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!-- redis template definition -->
<bean
id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"
p:keySerializer-ref="stringRedisSerializer"
/>
2. From JedisConnectionFactory
2. 来自 JedisConnectionFactory
RedisConnection redisConnection = template.getConnectionFactory().getConnection();
Set<byte[]> redisKeys = redisConnection.keys("samplekey*".getBytes());
List<String> keysList = new ArrayList<>();
Iterator<byte[]> it = redisKeys.iterator();
while (it.hasNext()) {
byte[] data = (byte[]) it.next();
keysList.add(new String(data, 0, data.length));
}
redisConnection.close();
If you don't close this connection explicitly, you will run into an exhaustion of the underlying jedis connection pool as said in https://stackoverflow.com/a/36641934/3884173.
如果您不明确关闭此连接,您将遇到底层 jedis 连接池耗尽的情况,如https://stackoverflow.com/a/36641934/3884173 中所述。
回答by user3270238
try:
尝试:
Set<byte[]> keys = RedisTemplate.getConnectionFactory().getConnection().keys("*".getBytes());
Iterator<byte[]> it = keys.iterator();
while(it.hasNext()){
byte[] data = (byte[])it.next();
System.out.println(new String(data, 0, data.length));
}
回答by dturanski
Try redisTemplate.setKeySerializer(new StringRedisSerializer());
尝试 redisTemplate.setKeySerializer(new StringRedisSerializer());
回答by ashK
It did work, but seems not recommended? Because we can't use Keys command in production. I assume RedisTemplate.getConnectionFactory().getConnection().keysis calling redis Keys command. What are the alternatives?
它确实有效,但似乎不推荐?因为我们不能在生产中使用 Keys 命令。我假设RedisTemplate.getConnectionFactory().getConnection().keys正在调用 redis Keys 命令。有哪些替代方案?
回答by Jason Null
I was using redisTemplate.keys(), but it was not working. So I used jedis, it worked. The following is the code that I used.
我正在使用redisTemplate.keys(),但它不起作用。所以我使用了绝地武士,它奏效了。以下是我使用的代码。
Jedis jedis = new Jedis("localhost", 6379);
Set<String> keys = jedis.keys("*".getBytes());
for (String key : keys) {
// do something
} // for
回答by Nikita Koksharov
Avoid to use keyscommand. It may ruin performance when it is executed against large databases.
避免使用keys命令。当它针对大型数据库执行时,它可能会破坏性能。
You should use scancommand instead. Here is how you can do it:
您应该改用scan命令。您可以这样做:
RedisConnection redisConnection = null;
try {
redisConnection = redisTemplate.getConnectionFactory().getConnection();
ScanOptions options = ScanOptions.scanOptions().match("myKey*").count(100).build();
Cursor c = redisConnection.scan(options);
while (c.hasNext()) {
logger.info(new String((byte[]) c.next()));
}
} finally {
redisConnection.close(); //Ensure closing this connection.
}
or do it much simplier with RedissonRedis Java client:
或者使用RedissonRedis Java 客户端更简单:
Iterable<String> keysIterator = redisson.getKeys().getKeysByPattern("test*", 100);
for (String key : keysIterator) {
logger.info(key);
}
回答by VanThaoNguyen
Solution can be like this
解决方案可以是这样的
String pattern = "abc"+"*";
Set<String> keys = jedis.keys(pattern);
for (String key : keys) {
jedis.keys(key);
}
Or you can use jedis.hscan()and ScanParamsinstead.
或者您可以使用jedis.hscan()andScanParams代替。

