您好,登錄后才能下訂單哦!
這篇文章主要講解了“redis的scan語法及作用對象”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“redis的scan語法及作用對象”吧!
在redis的db存在大量key或者db里頭的某個set、zset、hash里頭的元素非常多的話,用普通的get all操作很可能導致redis因為這個操作阻塞了,導致不能響應其他操作,特別是在高并發、海量數據的背景下,這個問題顯得尤其嚴重。那么能不能像數據庫那樣有個分頁的功能呢,答案就是scan操作。本文主要展示怎么在redis-cli以及SpringDataRedis中的使用。【推薦:redis視頻教程】
scan之后返回兩部分,第一部分是下次scan的參數,第二部分就是scan出來的項
db(key
)
127.0.0.1:6379> scan 0 1) "120" 2) 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:55" 127.0.0.1:6379> scan 120 1) "28" 2) 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 0 1) "3" 2) 1) "m" 2) "j" 3) "c" 4) "h" 5) "f" 6) "i" 7) "a" 8) "g" 9) "n" 10) "e" 11) "b" 127.0.0.1:6379> sscan myset 3 1) "0" 2) 1) "l" 2) "k" 3) "d"
zset(value & score
)
127.0.0.1:6379> zscan sortset 0 1) "0" 2) 1) "tom" 2) "89" 3) "jim" 4) "90" 5) "david" 6) "100"
hash(key & value
)
127.0.0.1:6379> hscan mymap 0 1) "0" 2) 1) "name" 2) "codecraft" 3) "email" 4) "pt@g.cn" 5) "age" 6) "20" 7) "desc" 8) "hello" 9) "sex" 10) "male"
count(指定每次取多少條
)
127.0.0.1:6379> scan 0 count 5 1) "240" 2) 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) "120" 2) 1) "articleMap:63" 2) "articleMap:37" 3) "articleMap:60" 4) "articleMap:80" 5) "articleMap:46" 6) "articleMap:55"
@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; } }); }
/** * 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()); } }
/** * zadd sortset 89 tom 90 jim 100 david */ @Test public void scanZSet(){ Cursor<ZSetOperations.TypedTuple<String>> cursor = template.opsForZSet().scan("sortset",ScanOptions.NONE); while (cursor.hasNext()){ ZSetOperations.TypedTuple<String> item = cursor.next(); System.out.println(item.getValue() + ":" + item.getScore()); } }
/** * 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語法及作用對象”的內容了,經過本文的學習后,相信大家對redis的scan語法及作用對象這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。