【发布时间】:2013-10-06 13:03:31
【问题描述】:
我已经被这个问题困扰了很长一段时间。我想使用 redis 模板从 redis 获取密钥。 我试过 this.redistemplate.keys("*"); 但这并没有得到任何东西。即使使用该模式,它也不起作用。
您能否建议最好的解决方案。
【问题讨论】:
我已经被这个问题困扰了很长一段时间。我想使用 redis 模板从 redis 获取密钥。 我试过 this.redistemplate.keys("*"); 但这并没有得到任何东西。即使使用该模式,它也不起作用。
您能否建议最好的解决方案。
【问题讨论】:
我只是整合了答案,我们在这里看到了。
当我们使用 RedisTemplate 时,这里有两种从 Redis 获取密钥的方法。
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);
}
注意:你应该已经在你的 bean 中配置了带有 StringRedisSerializer 的 redisTemplate
如果你使用基于 java 的 bean 配置
redisTemplate.setDefaultSerializer(new StringRedisSerializer());
如果你使用基于 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。来自 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();
如果你不明确关闭这个连接,你会遇到https://stackoverflow.com/a/36641934/3884173中所说的底层jedis连接池的耗尽。
【讨论】:
尝试:
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));
}
【讨论】:
试试redisTemplate.setKeySerializer(new StringRedisSerializer());
【讨论】:
避免使用keys 命令。当它针对大型数据库执行时,它可能会破坏性能。
您应该改用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.
}
或者使用Redisson Redis Java 客户端更简单:
Iterable<String> keysIterator = redisson.getKeys().getKeysByPattern("test*", 100);
for (String key : keysIterator) {
logger.info(key);
}
【讨论】:
它确实有效,但似乎不推荐?因为我们不能在生产中使用 Keys 命令。我假设 RedisTemplate.getConnectionFactory().getConnection().keys 正在调用 redis Keys 命令。有哪些替代方案?
【讨论】:
我使用的是redisTemplate.keys(),但它不起作用。所以我使用了绝地武士,它奏效了。以下是我使用的代码。
Jedis jedis = new Jedis("localhost", 6379);
Set<String> keys = jedis.keys("*".getBytes());
for (String key : keys) {
// do something
} // for
【讨论】:
解决办法可以是这样的
String pattern = "abc"+"*";
Set<String> keys = jedis.keys(pattern);
for (String key : keys) {
jedis.keys(key);
}
或者您可以使用jedis.hscan() 和ScanParams 代替。
【讨论】:
keys,危险
试试
import org.springframework.data.redis.core.RedisTemplate;
import org.apache.commons.collections.CollectionUtils;
String key = "example*";
Set keys = redisTemplate.keys(key);
if (CollectionUtils.isEmpty(keys)) return null;
List list = redisTemplate.opsForValue().multiGet(keys);
【讨论】: