SpringRedisTemplate实现scan操作
来源:爱问旅游网
SpringRedisTemplate实现scan操作
⽼⽣常谈,keys不安全,因为
keys的操作会导致数据库暂时被锁住,其他的请求都会被堵塞;业务量⼤的时候会出问题
Spring RedisTemplate实现scan
1. hscan sscan zscan
例⼦中的\"field\"是值redis的key,即从key为\"field\"中的hash中查找
redisTemplate的opsForHash,opsForSet,opsForZSet 可以 分别对应 sscan、hscan、zscan
也可以使⽤ (JedisCommands) connection.getNativeConnection() 的 hscan、sscan、zscan ⽅法实现cursor遍历,参照下⽂2.2章节
try {
Cursor> cursor = redisTemplate.opsForHash().scan(\"field\", ScanOptions.scanOptions().match(\"*\").count(1000).build()); while (cursor.hasNext()) {Map.Entry