RedisTemplate 怎么获取到链接信息?怎么获取到所有key?怎么获取指定key?

2024-01-07 19:21:37

获取Redis的链接信息:

(RedisTemplate<String,??>?redisTemplate)?{
????RedisConnectionFactory?connectionFactory?=?redisTemplate.getConnectionFactory();

????(!(connectionFactory?LettuceConnectionFactory))?{
????????System..println();
????????;
????}

????LettuceConnectionFactory?lettuceConnectionFactory?=?(LettuceConnectionFactory)?connectionFactory;

????RedisConnection?connection?=?connectionFactory.getConnection();

????(connection?==?||?connection.isClosed())?{
????????System..println();
????}?{
????????System..println();

????????String?host?=?lettuceConnectionFactory.getHostName();
????????port?=?lettuceConnectionFactory.getPort();

????????System..println(+?host);
????????System..println(+?port);

????????System..println(+?connection.info());
????}
}

获取指定的key:

Set<String>?(RedisTemplate<String,??>?redisTemplate)?{
????ScanOptions?scanOptions?=?ScanOptions.().match().count().build();

????Set<String>?keys?=?redisTemplate.execute(connection?->?{
????????Set<[]>?keysBytes?=?connection.keys(.getBytes());
????????keysBytes.stream()
????????????????.map(String::)
????????????????.collect(Collectors.());
????},?);

????keys;
}

获取所有key:

Set<String>?(RedisTemplate<String,??>?redisTemplate,String?hashKey)?{
????HashOperations<String,?String,?Object>?hashOperations?=?redisTemplate.opsForHash();
????hashOperations.keys(hashKey);
}

文章来源:https://blog.csdn.net/kaizi_1992/article/details/135441493
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。