scan语法scan之后返回两部分,第一部分是下次scan的参数,第二部分就是scan出来的项
作用对象(db、set、zset、hash)db(key)127.0.0.1:6379> scan 01) 1202) 1) articlemap:63 2) articlemap:37 3) counter:__rand_int__ 4) articlemap:60 5) tagset:tag5 6) articlemap:80 7) messagecache~keys 8) mymap 9) articlemap:46 10) articlemap:55127.0.0.1:6379> scan 1201) 282) 1) articlemap:17 2) tagset:tag1 3) articlemap:18 4) articlemap:81 5) \xac\xed\x00\x05t\x00\btest-cas 6) articlemap:51 7) articlemap:94 8) articlemap:26 9) articlemap:71 10) user-abcde
set(value)127.0.0.1:6379> sscan myset 01) 32) 1) m 2) j 3) c 4) h 5) f 6) i 7) a 8) g 9) n 10) e 11) b127.0.0.1:6379> sscan myset 31) 02) 1) l 2) k 3) d
zset(value & score)127.0.0.1:6379> zscan sortset 01) 02) 1) tom 2) 89 3) jim 4) 90 5) david 6) 100
hash(key & value)127.0.0.1:6379> hscan mymap 01) 02) 1) name 2) codecraft 3) email 4) pt@g.cn 5) age 6) 20 7) desc 8) hello 9) sex 10) male
scan的额外参数count(指定每次取多少条)127.0.0.1:6379> scan 0 count 51) 2402) 1) articlemap:63 2) articlemap:37 3) counter:__rand_int__ 4) articlemap:60 5) tagset:tag5
match(匹配key)127.0.0.1:6379> scan 0 match article*1) 1202) 1) articlemap:63 2) articlemap:37 3) articlemap:60 4) articlemap:80 5) articlemap:46 6) articlemap:55
redistemplate操作遍历数据库key@test public void scandbkeys(){ template.execute(new rediscallback<iterable<byte[]>>() { @override public iterable<byte[]> doinredis(redisconnection connection) throws dataaccessexception { list<byte[]> binarykeys = new arraylist<byte[]>(); cursor<byte[]> cursor = connection.scan(scanoptions.scanoptions().count(5).build()); while (cursor.hasnext()) { byte[] key = cursor.next(); binarykeys.add(key); system.out.println(new string(key, standardcharsets.utf_8)); } try { cursor.close(); } catch (ioexception e) { // do something meaningful } return binarykeys; } }); }
遍历set/** * sadd myset a b c d e f g h i j k l m n */ @test public void scanset(){ cursor<string> cursor = template.opsforset().scan(myset,scanoptions.none); while (cursor.hasnext()){ system.out.println(cursor.next()); } }
遍历zset/** * zadd sortset 89 tom 90 jim 100 david */ @test public void scanzset(){ cursor cursor = template.opsforzset().scan(sortset,scanoptions.none); while (cursor.hasnext()){ zsetoperations.typedtuple<string> item = cursor.next(); system.out.println(item.getvalue() + : + item.getscore()); } }
遍历hash/** * hset mymap name codecraft * hset mymap email pt@g.cn * hset mymap age 20 * hset mymap desc hello * hset mymap sex male */ @test public void scanhash(){ cursor<map.entry<object, object>> curosr = template.opsforhash().scan(mymap, scanoptions.none); while(curosr.hasnext()){ map.entry<object, object> entry = curosr.next(); system.out.println(entry.getkey()+:+entry.getvalue()); } }
以上就是一起聊聊redis的scan操作的详细内容。
