redis.clients.jedis.Jedis.keys()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(6.5k)|赞(0)|评价(0)|浏览(222)

本文整理了Java中redis.clients.jedis.Jedis.keys()方法的一些代码示例,展示了Jedis.keys()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Jedis.keys()方法的具体详情如下:
包路径:redis.clients.jedis.Jedis
类名称:Jedis
方法名:keys

Jedis.keys介绍

[英]Returns all the keys matching the glob-style pattern as space separated strings. For example if you have in the database the keys "foo" and "foobar" the command "KEYS foo*" will return "foo foobar".

Note that while the time complexity for this operation is O(n) the constant times are pretty low. For example Redis running on an entry level laptop can scan a 1 million keys database in 40 milliseconds. Still it's better to consider this one of the slow commands that may ruin the DB performance if not used with care.

In other words this command is intended only for debugging and special operations like creating a script to change the DB schema. Don't use it in your normal code. Use Redis Sets in order to group together a subset of objects.

Glob style patterns examples:

  • h?llo will match hello hallo hhllo
  • h*llo will match hllo heeeello
  • h[ae]llo will match hello and hallo, but not hillo

Use \ to escape special chars if you want to match them verbatim.

Time complexity: O(n) (with n being the number of keys in the DB, and assuming keys and pattern of limited length)
[中]以空格分隔的字符串形式返回与全局样式模式匹配的所有键。例如,如果数据库中有键“foo”和“foobar”,则命令“keys foo*”将返回“foo foobar”。
请注意,虽然此操作的时间复杂度为O(n),但常数时间非常低。例如,在入门级笔记本电脑上运行的Redis可以在40毫秒内扫描一百万个密钥数据库。最好还是考虑一下这个慢命令,如果不小心使用,可能会破坏DB性能。
换句话说,此命令仅用于调试和特殊操作,如创建脚本以更改DB模式。不要在普通代码中使用它。使用Redis集合将对象的子集组合在一起。
全局样式模式示例:
*h?你好,你好,哈罗,哈罗
hllo将与hllo HEEELLO比赛
*h[ae]llo将匹配hello和hallo,但不会匹配hillo
如果要逐字匹配特殊字符,请使用\将其转义。
时间复杂度:O(n)(n是数据库中的密钥数,假设密钥和模式长度有限)

代码示例

代码示例来源:origin: caoxinyu/RedisClient

protected Set<String> getResult() {
  Set<String> nodekeys = null;
  assert(container != null);
  nodekeys = jedis.keys(container + keyPattern);
  return nodekeys;
}

代码示例来源:origin: yu199195/hmily

@Override
public Set<String> keys(final String key) {
  try (Jedis jedis = jedisPool.getResource()) {
    return jedis.keys(key);
  }
}

代码示例来源:origin: yu199195/hmily

@Override
public Set<byte[]> keys(final byte[] pattern) {
  try (Jedis jedis = jedisPool.getResource()) {
    return jedis.keys(pattern);
  }
}

代码示例来源:origin: yu199195/myth

@Override
public Set<String> keys(final String key) {
  try (Jedis jedis = jedisPool.getResource()) {
    return jedis.keys(key);
  }
}

代码示例来源:origin: yu199195/Raincat

@Override
public Set<byte[]> keys(final byte[] pattern) {
  try (Jedis jedis = jedisPool.getResource()) {
    return jedis.keys(pattern);
  }
}

代码示例来源:origin: yu199195/Raincat

@Override
public Set<String> keys(final String key) {
  try (Jedis jedis = jedisPool.getResource()) {
    return jedis.keys(key);
  }
}

代码示例来源:origin: yu199195/myth

@Override
public Set<byte[]> keys(final byte[] pattern) {
  try (Jedis jedis = jedisPool.getResource()) {
    return jedis.keys(pattern);
  }
}

代码示例来源:origin: yu199195/hmily

@Override
public Set<byte[]> keys(final byte[] pattern) {
  try (Jedis jedis = jedisSentinelPool.getResource()) {
    return jedis.keys(pattern);
  }
}

代码示例来源:origin: yu199195/hmily

@Override
public Set<String> keys(final String key) {
  try (Jedis jedis = jedisSentinelPool.getResource()) {
    return jedis.keys(key);
  }
}

代码示例来源:origin: yu199195/Raincat

@Override
public Set<byte[]> keys(final byte[] pattern) {
  try (Jedis jedis = jedisSentinelPool.getResource()) {
    return jedis.keys(pattern);
  }
}

代码示例来源:origin: yu199195/Raincat

@Override
public Set<String> keys(final String key) {
  try (Jedis jedis = jedisSentinelPool.getResource()) {
    return jedis.keys(key);
  }
}

代码示例来源:origin: yu199195/myth

@Override
public Set<byte[]> keys(final byte[] pattern) {
  try (Jedis jedis = jedisSentinelPool.getResource()) {
    return jedis.keys(pattern);
  }
}

代码示例来源:origin: yu199195/myth

@Override
public Set<String> keys(final String key) {
  try (Jedis jedis = jedisSentinelPool.getResource()) {
    return jedis.keys(key);
  }
}

代码示例来源:origin: caoxinyu/RedisClient

@Override
public void command() {
  jedis.select(db);
  Set<String> nodekeys = jedis.keys("*");
  Iterator<String> it = nodekeys.iterator();
  while (it.hasNext()) {
    String key = (String)it.next();
    NodeType nodeType = getValueType(key);
    
    Node node = new Node(id, db, key, nodeType);
    nodes.add(node);
  }
}

代码示例来源:origin: apache/incubator-dubbo

if (service.endsWith(Constants.ANY_VALUE)) {
  admin = true;
  Set<String> keys = jedis.keys(service);
  if (CollectionUtils.isNotEmpty(keys)) {
    Map<String, Set<String>> serviceKeys = new HashMap<>();
  doNotify(jedis, jedis.keys(service + Constants.PATH_SEPARATOR + Constants.ANY_VALUE), url, Collections.singletonList(listener));

代码示例来源:origin: apache/incubator-dubbo

if (service.endsWith(Constants.ANY_VALUE)) {
  admin = true;
  Set<String> keys = jedis.keys(service);
  if (CollectionUtils.isNotEmpty(keys)) {
    Map<String, Set<String>> serviceKeys = new HashMap<>();
  doNotify(jedis, jedis.keys(service + Constants.PATH_SEPARATOR + Constants.ANY_VALUE), url, Collections.singletonList(listener));

代码示例来源:origin: apache/incubator-dubbo

private void clean(Jedis jedis) {
  Set<String> keys = jedis.keys(root + Constants.ANY_VALUE);
  if (CollectionUtils.isNotEmpty(keys)) {
    for (String key : keys) {
      Map<String, String> values = jedis.hgetAll(key);
      if (CollectionUtils.isNotEmptyMap(values)) {
        boolean delete = false;
        long now = System.currentTimeMillis();
        for (Map.Entry<String, String> entry : values.entrySet()) {
          URL url = URL.valueOf(entry.getKey());
          if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
            long expire = Long.parseLong(entry.getValue());
            if (expire < now) {
              jedis.hdel(key, entry.getKey());
              delete = true;
              if (logger.isWarnEnabled()) {
                logger.warn("Delete expired key: " + key + " -> value: " + entry.getKey() + ", expire: " + new Date(expire) + ", now: " + new Date(now));
              }
            }
          }
        }
        if (delete) {
          jedis.publish(key, Constants.UNREGISTER);
        }
      }
    }
  }
}

代码示例来源:origin: apache/incubator-dubbo

private void clean(Jedis jedis) {
  Set<String> keys = jedis.keys(root + Constants.ANY_VALUE);
  if (CollectionUtils.isNotEmpty(keys)) {
    for (String key : keys) {
      Map<String, String> values = jedis.hgetAll(key);
      if (CollectionUtils.isNotEmptyMap(values)) {
        boolean delete = false;
        long now = System.currentTimeMillis();
        for (Map.Entry<String, String> entry : values.entrySet()) {
          URL url = URL.valueOf(entry.getKey());
          if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
            long expire = Long.parseLong(entry.getValue());
            if (expire < now) {
              jedis.hdel(key, entry.getKey());
              delete = true;
              if (logger.isWarnEnabled()) {
                logger.warn("Delete expired key: " + key + " -> value: " + entry.getKey() + ", expire: " + new Date(expire) + ", now: " + new Date(now));
              }
            }
          }
        }
        if (delete) {
          jedis.publish(key, Constants.UNREGISTER);
        }
      }
    }
  }
}

代码示例来源:origin: apache/incubator-dubbo

if (!first) {
  first = false;
  Set<String> keys = jedis.keys(service);
  if (CollectionUtils.isNotEmpty(keys)) {
    for (String s : keys) {

代码示例来源:origin: apache/incubator-dubbo

if (!first) {
  first = false;
  Set<String> keys = jedis.keys(service);
  if (CollectionUtils.isNotEmpty(keys)) {
    for (String s : keys) {

相关文章

微信公众号

最新文章

更多

Jedis类方法