从redis哈希键中获取以一些常见文本开头的字段

问题描述 投票:0回答:1

我想从 Redis 哈希键中获取以某个字符串开头的所有字段

例如 哈希键包含字段 学生A001 学生B253 学生A24165

所以我想要所有以studentA开头的字段

我尝试所有键并检查它是否包含或以 docID 开头

try (Jedis jedis = this.getJedisPool().getResource()) {
            Set<String> keys = jedis.hkeys(mapKey);
            for (String key : keys) {
                if (StringUtils.contains(key, docId)) {
                    long clearedCount = jedis.hdel(mapKey, key);
                    if (clearedCount > 0) {
                        cacheCleared = true;
                        log.info("Cleared from cache for Doc ID: [" + docId + "");
                    }
                }
            }
        } catch (Exception e) {
            throw e;
        }
redis hash
1个回答
0
投票

您可以将 HSCAN 命令与可选的

[MATCH pattern]
参数一起使用。

这是一个例子:

redis> HSET h student1 dave student2 joe teacher1 jhon
(integer) 3
redis> HSCAN h 0 MATCH student*
1) "0"
2) 1) "student1"
   2) "dave"
   3) "student2"
   4) "joe"
© www.soinside.com 2019 - 2024. All rights reserved.