Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Scan Grammar and function object of redis

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "scan grammar and action object of redis". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "scan grammar and action object of redis".

Scan operation order of redis

If there are a large number of key in the db of redis or a large number of elements in a set, zset or hash in the db, using ordinary get all operations may cause the redis to be blocked and unable to respond to other operations, especially in the context of high concurrency and massive data. So can there be a paging function like the database? the answer is scan operation. This article mainly shows how to use it in redis-cli and SpringDataRedis. [recommended: redis video tutorial]

Scan syntax

After scan, two parts are returned. The first part is the parameters of the next scan, and the second part is the item from scan.

Object of action (db, set, zset, hash)

Db (key)

127.0.1 scan 6379 > articleMap:46 01) "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 articleMap:18 6379 > scan 1201) "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 01) "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) sscan myset 31) "0 "2) 1)" l "2)" k "3)" d "

Zset (value & score)

127.0.1 jim 6379 > zscan sortset 01) "0" 2) 1) "tom" 2) "89" 3) "jim" 4) "90" 5) "david" 6) "100"

Hash (key & value)

127.0.1 codecraft 6379 > hscan mymap 01) "0" 2) 1) "name" 2) "codecraft" 3) "email" 4) "pt@g.cn" 5) "age" 6) "20" 7) "desc" 8) "hello" 9) "sex" 10) additional parameters of "male" SCAN

Count (specify how many entries to take at a time)

127.0.1 articleMap:63 6379 > scan 0 count 51) "2402) 1)" articleMap:63 "2)" articleMap:37 "3)" counter:__rand_int__ "4)" articleMap:60 "5)" tagSet:tag5 "

Match (match key)

127.0.1 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" RedisTemplate operation traverses database key@Test public void scanDbKeys () {template.execute (new RedisCallback () {@ Override) Public Iterable doInRedis (RedisConnection connection) throws DataAccessException {List binaryKeys = new ArrayList () Cursor 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;}}) } traverse set/** * sadd myset a b c d e f g hi j k l m n * / @ Test public void scanSet () {Cursor cursor = template.opsForSet () .scan ("myset", ScanOptions.NONE); while (cursor.hasNext ()) {System.out.println (cursor.next ()) }} traverse zset/** * zadd sortset 89 tom 90 jim 100david * / @ Test public void scanZSet () {Cursor cursor = template.opsForZSet () .scan ("sortset", ScanOptions.NONE); while (cursor.hasNext ()) {ZSetOperations.TypedTuple item = cursor.next (); System.out.println (item.getValue () + ":" + item.getScore ()) }} traverse 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 curosr = template.opsForHash () .scan ("mymap", ScanOptions.NONE) While (curosr.hasNext ()) {Map.Entry entry = curosr.next (); System.out.println (entry.getKey () + ":" + entry.getValue ()) }} Thank you for your reading, the above is the content of "scan Grammar and function object of redis". After the study of this article, I believe you have a deeper understanding of the problem of scan grammar and function object of redis, and the specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report