liusuyi
2026-05-12 9f327c33730ba10cb2d89aff99b727502232e968
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.ard.zlm.utils;
 
import com.google.common.collect.Lists;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
 
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
/**
 * Redis工具类
 *
 */
@SuppressWarnings(value = {"rawtypes", "unchecked"})
public class RedisUtil {
 
    /**
     * 模糊查询
     *
     * @param query 查询参数
     * @return
     */
    public static List<Object> scan(RedisTemplate redisTemplate, String query) {
 
        Set<String> resultKeys = (Set<String>) redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
            ScanOptions scanOptions = ScanOptions.scanOptions().match("*" + query + "*").count(1000).build();
            Set<String> keys = new HashSet<>();
            try (Cursor<byte[]> scan = connection.scan(scanOptions)) {
                while (scan.hasNext()) {
                    byte[] next = scan.next();
                    keys.add(new String(next));
                }
            }
            return keys;
        });
 
        return Lists.newArrayList(resultKeys);
    }
}