redis.clients.jedis.Jedis类的使用及代码示例

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

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

Jedis介绍

暂无

代码示例

代码示例来源:origin: shuzheng/zheng

/**
 * 获取byte[]值
 * @param key
 * @return value
 */
public synchronized static byte[] get(byte[] key) {
  Jedis jedis = getJedis();
  if (null == jedis) {
    return null;
  }
  byte[] value = jedis.get(key);
  jedis.close();
  return value;
}

代码示例来源:origin: shuzheng/zheng

/**
 * 设置 byte[] 过期时间
 * @param key
 * @param value
 * @param seconds 以秒为单位
 */
public synchronized static void set(byte[] key, byte[] value, int seconds) {
  try {
    Jedis jedis = getJedis();
    jedis.set(key, value);
    jedis.expire(key, seconds);
    jedis.close();
  } catch (Exception e) {
    LOGGER.error("Set key error : " + e);
  }
}

代码示例来源:origin: code4craft/webmagic

@Override
public void resetDuplicateCheck(Task task) {
  Jedis jedis = pool.getResource();
  try {
    jedis.del(getSetKey(task));
  } finally {
    pool.returnResource(jedis);
  }
}

代码示例来源:origin: shuzheng/zheng

/**
 * decr
 * @param key
 * @return value
 */
public synchronized static Long decr(String key) {
  Jedis jedis = getJedis();
  if (null == jedis) {
    return null;
  }
  long value = jedis.decr(key);
  jedis.close();
  return value;
}

代码示例来源:origin: spring-projects/spring-data-redis

@Override
public Boolean set(byte[] key, byte[] value) {
  Assert.notNull(key, "Key must not be null!");
  Assert.notNull(value, "Value must not be null!");
  try {
    if (isPipelined()) {
      pipeline(connection.newJedisResult(connection.getRequiredPipeline().set(key, value),
          Converters.stringToBooleanConverter()));
      return null;
    }
    if (isQueueing()) {
      transaction(connection.newJedisResult(connection.getRequiredTransaction().set(key, value),
          Converters.stringToBooleanConverter()));
      return null;
    }
    return Converters.stringToBoolean(connection.getJedis().set(key, value));
  } catch (Exception ex) {
    throw convertJedisAccessException(ex);
  }
}

代码示例来源:origin: spring-projects/spring-data-redis

@Override
public Boolean exists(byte[] key) {
  Assert.notNull(key, "Key must not be null!");
  try {
    if (isPipelined()) {
      pipeline(connection.newJedisResult(connection.getRequiredPipeline().exists(key)));
      return null;
    }
    if (isQueueing()) {
      transaction(connection.newJedisResult(connection.getRequiredTransaction().exists(key)));
      return null;
    }
    return connection.getJedis().exists(key);
  } catch (Exception ex) {
    throw connection.convertJedisAccessException(ex);
  }
}

代码示例来源:origin: spring-projects/spring-data-redis

@Override
public Boolean expire(byte[] key, long seconds) {
  Assert.notNull(key, "Key must not be null!");
  if (seconds > Integer.MAX_VALUE) {
    return pExpire(key, TimeUnit.SECONDS.toMillis(seconds));
  }
  try {
    if (isPipelined()) {
      pipeline(connection.newJedisResult(connection.getRequiredPipeline().expire(key, (int) seconds),
          JedisConverters.longToBoolean()));
      return null;
    }
    if (isQueueing()) {
      transaction(connection.newJedisResult(connection.getRequiredTransaction().expire(key, (int) seconds),
          JedisConverters.longToBoolean()));
      return null;
    }
    return JedisConverters.toBoolean(connection.getJedis().expire(key, (int) seconds));
  } catch (Exception ex) {
    throw connection.convertJedisAccessException(ex);
  }
}

代码示例来源:origin: spring-projects/spring-data-redis

@Override
public Boolean hSet(byte[] key, byte[] field, byte[] value) {
  Assert.notNull(key, "Key must not be null!");
  Assert.notNull(field, "Field must not be null!");
  Assert.notNull(value, "Value must not be null!");
  try {
    if (isPipelined()) {
      pipeline(connection.newJedisResult(connection.getRequiredPipeline().hset(key, field, value),
          JedisConverters.longToBoolean()));
      return null;
    }
    if (isQueueing()) {
      transaction(connection.newJedisResult(connection.getRequiredTransaction().hset(key, field, value),
          JedisConverters.longToBoolean()));
      return null;
    }
    return JedisConverters.toBoolean(connection.getJedis().hset(key, field, value));
  } catch (Exception ex) {
    throw convertJedisAccessException(ex);
  }
}

代码示例来源:origin: spring-projects/spring-data-redis

@Override
public Set<byte[]> keys(byte[] pattern) {
  Assert.notNull(pattern, "Pattern must not be null!");
  Collection<Set<byte[]>> keysPerNode = connection.getClusterCommandExecutor()
      .executeCommandOnAllNodes((JedisClusterCommandCallback<Set<byte[]>>) client -> client.keys(pattern))
      .resultsAsList();
  Set<byte[]> keys = new HashSet<>();
  for (Set<byte[]> keySet : keysPerNode) {
    keys.addAll(keySet);
  }
  return keys;
}

代码示例来源:origin: spring-projects/spring-data-redis

@Override
public byte[] get(byte[] key) {
  Assert.notNull(key, "Key must not be null!");
  try {
    if (isPipelined()) {
      pipeline(connection.newJedisResult(connection.getRequiredPipeline().get(key)));
      return null;
    }
    if (isQueueing()) {
      transaction(connection.newJedisResult(connection.getRequiredTransaction().get(key)));
      return null;
    }
    return connection.getJedis().get(key);
  } catch (Exception ex) {
    throw convertJedisAccessException(ex);
  }
}

代码示例来源:origin: spring-projects/spring-data-redis

@Override
public Set<byte[]> keys(byte[] pattern) {
  Assert.notNull(pattern, "Pattern must not be null!");
  try {
    if (isPipelined()) {
      pipeline(connection.newJedisResult(connection.getRequiredPipeline().keys(pattern)));
      return null;
    }
    if (isQueueing()) {
      transaction(connection.newJedisResult(connection.getRequiredTransaction().keys(pattern)));
      return null;
    }
    return connection.getJedis().keys(pattern);
  } catch (Exception ex) {
    throw connection.convertJedisAccessException(ex);
  }
}

代码示例来源:origin: spring-projects/spring-data-redis

@Override
public byte[] hGet(byte[] key, byte[] field) {
  Assert.notNull(key, "Key must not be null!");
  Assert.notNull(field, "Field must not be null!");
  try {
    if (isPipelined()) {
      pipeline(connection.newJedisResult(connection.getRequiredPipeline().hget(key, field)));
      return null;
    }
    if (isQueueing()) {
      transaction(connection.newJedisResult(connection.getRequiredTransaction().hget(key, field)));
      return null;
    }
    return connection.getJedis().hget(key, field);
  } catch (Exception ex) {
    throw convertJedisAccessException(ex);
  }
}

代码示例来源:origin: apache/ignite

private void testExpire(Expiration exp) throws Exception {
  try (Jedis jedis = pool.getResource()) {
    jedis.set("k1", "v1");
    Assert.assertTrue(jedis.exists("k1"));
    Assert.assertEquals(1L, exp.expire(jedis, "k1"));
    Assert.assertEquals("v1", jedis.get("k1"));
    Thread.sleep(2100);
    Assert.assertFalse(jedis.exists("k1"));
    Assert.assertEquals(0L, (long)jedis.expire("k1", 2));
  }
}

代码示例来源:origin: spring-projects/spring-data-redis

private Jedis getActiveSentinel() {
  Assert.isTrue(RedisConfiguration.isSentinelConfiguration(configuration), "SentinelConfig must not be null!");
  for (RedisNode node : ((SentinelConfiguration) configuration).getSentinels()) {
    Jedis jedis = new Jedis(node.getHost(), node.getPort(), getConnectTimeout(), getReadTimeout());
    if (jedis.ping().equalsIgnoreCase("pong")) {
      potentiallySetClientName(jedis);
      return jedis;
    }
  }
  throw new InvalidDataAccessResourceUsageException("No Sentinel found");
}

代码示例来源:origin: apache/storm

jedis = jedisPool.getResource();
  case STRING:
    String[] keyValue = buildKeyValuesList(keyValues);
    jedis.mset(keyValue);
    if (this.options.expireIntervalSec > 0) {
      Pipeline pipe = jedis.pipelined();
      for (int i = 0; i < keyValue.length; i += 2) {
        pipe.expire(keyValue[i], this.options.expireIntervalSec);
      pipe.sync();
    jedis.hmset(description.getAdditionalKey(), keyValues);
    if (this.options.expireIntervalSec > 0) {
      jedis.expire(description.getAdditionalKey(), this.options.expireIntervalSec);
  jedis.close();

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

private void deferExpired() {
  for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) {
    JedisPool jedisPool = entry.getValue();
    try {
      try (Jedis jedis = jedisPool.getResource()) {
        for (URL url : new HashSet<>(getRegistered())) {
          if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
            String key = toCategoryPath(url);
            if (jedis.hset(key, url.toFullString(), String.valueOf(System.currentTimeMillis() + expirePeriod)) == 1) {
              jedis.publish(key, Constants.REGISTER);
            }
          }
        }
        if (admin) {
          clean(jedis);
        }
        if (!replicate) {
          break;//  If the server side has synchronized data, just write a single machine
        }
      }
    } catch (Throwable t) {
      logger.warn("Failed to write provider heartbeat to redis registry. registry: " + entry.getKey() + ", cause: " + t.getMessage(), t);
    }
  }
}

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

private void storeMetadata(MetadataIdentifier metadataIdentifier, String v) {
  try (Jedis jedis = pool.getResource()) {
    jedis.set(metadataIdentifier.getIdentifierKey() + META_DATA_SOTRE_TAG, v);
  } catch (Throwable e) {
    logger.error("Failed to put " + metadataIdentifier + " to redis " + v + ", cause: " + e.getMessage(), e);
    throw new RpcException("Failed to put " + metadataIdentifier + " to redis " + v + ", cause: " + e.getMessage(), e);
  }
}

代码示例来源:origin: code4craft/webmagic

@Override
protected void pushWhenNoDuplicate(Request request, Task task) {
  Jedis jedis = pool.getResource();
  try {
    jedis.rpush(getQueueKey(task), request.getUrl());
    if (checkForAdditionalInfo(request)) {
      String field = DigestUtils.shaHex(request.getUrl());
      String value = JSON.toJSONString(request);
      jedis.hset((ITEM_PREFIX + task.getUUID()), field, value);
    }
  } finally {
    jedis.close();
  }
}

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

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

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

JedisPool jedisPool = entry.getValue();
try {
  jedis = jedisPool.getResource();
  try {
    if (service.endsWith(Constants.ANY_VALUE)) {
      if (!first) {
        first = false;
        Set<String> keys = jedis.keys(service);
        if (CollectionUtils.isNotEmpty(keys)) {
          for (String s : keys) {
      jedis.psubscribe(new NotifySub(jedisPool), service); // blocking
    } else {
      if (!first) {
        resetSkip();
      jedis.psubscribe(new NotifySub(jedisPool), service + Constants.PATH_SEPARATOR + Constants.ANY_VALUE); // blocking
    jedis.close();

相关文章

微信公众号

最新文章

更多

Jedis类方法