Jedis uses scan to delete regular matching keys


import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

import java.util.List;
import java.util.Set;

public class RedisApplication {

    private static String prefix = "ws:";
    static Jedis jedis;

    static {
        jedis = new Jedis("localhost");
        System.out.println("连接成功");
    }

    public static void main(String[] args) {
//       init();
        list();
//        scan();
        del();
    }

    private static void del() {
        Long count = jedis.del("abc");
        System.out.println(count);
    }
    private static void scan() {
        ScanParams params = new ScanParams();
        params.match("ws:*");
//        params.count(3);
        String cursor = "0";
        while (true) {
            ScanResult<String> scanResult = jedis.scan(cursor, params);
            List<String> elements = scanResult.getResult();
            if (elements != null && elements.size() > 0) {
                System.out.println(elements);
            }
            cursor = scanResult.getStringCursor();
            if ("0".equals(cursor)) {
                break;
            }
        }
    }

    private static void list() {
        Set<String> keys = jedis.keys("ws:*");
        for (String key : keys) {
            System.out.println(key);
        }
        System.out.println("====");
    }

    private static void init() {
        for (int i = 0; i < 5; i++) {
            jedis.set(prefix + i, i + "");
        }
    }
}

Why can't keys be used in production?

The keys command of redis is generally used to delete related keys, but this command has a drawback. When redis has millions or more keys, it will execute slowly. What's more fatal is that this command The command will block the io main thread of redis multiplexing. If this thread is blocked, other commands sent to the redis server will be blocked during this execution , which will cause a series of cascade reactions, resulting in an instant response freeze, thus It will cause problems such as timeout, so the use of keys and similar commands smembers should be prohibited in the production environment. This time complexity is O(N) and commands that block the main thread are very dangerous.

Advantages of scan

Then in the production environment, we should use the scan command instead of the keys command, which is also a scan command with O(N) complexity, supports wildcard search, scan command or other scan commands such as SSCAN, HSCAN, ZSCAN, without blocking the main thread , and supports the cursor to iterate and return data in batches, so it is an ideal choice. The advantage of keys compared to the scan command is that keys are returned once, while scan needs to be iterated and returned multiple times. But the scan command also has shortcomings, the returned data may be repeated

 

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324031495&siteId=291194637