redis.clients.util.Pool类的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(9.3k)|赞(0)|评价(0)|浏览(84)

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

Pool介绍

暂无

代码示例

代码示例来源:origin: alibaba/jetcache

@Override
protected CacheGetResult<V> do_GET(K key) {
  if (key == null) {
    return new CacheGetResult<V>(CacheResultCode.FAIL, CacheResult.MSG_ILLEGAL_ARGUMENT, null);
  }
  try (Jedis jedis = pool.getResource()) {
    byte[] newKey = buildKey(key);
    byte[] bytes = jedis.get(newKey);
    if (bytes != null) {
      CacheValueHolder<V> holder = (CacheValueHolder<V>) valueDecoder.apply(bytes);
      if (System.currentTimeMillis() >= holder.getExpireTime()) {
        return CacheGetResult.EXPIRED_WITHOUT_MSG;
      }
      return new CacheGetResult(CacheResultCode.SUCCESS, null, holder);
    } else {
      return CacheGetResult.NOT_EXISTS_WITHOUT_MSG;
    }
  } catch (Exception ex) {
    logError("GET", key, ex);
    return new CacheGetResult(ex);
  }
}

代码示例来源:origin: sohutv/cachecloud

@Override
public void close() {
 destroy();
}

代码示例来源:origin: Dreampie/Resty

private void returnResource(ShardedJedis shardedJedis, Jedis jedis) {
 if (pool != null) {
  if (shardedJedis != null) {
   pool.returnResource(shardedJedis);
  }
  if (jedis != null) {
   pool.returnResource(jedis);
  }
 } else {
  if (jedis != null) {
   jedis.disconnect();
  }
 }
}

代码示例来源:origin: eclipse/leshan

@Override
public void updated(RegistrationUpdate update, Registration updatedRegistration,
    Registration previousRegistration) {
  try (Jedis j = pool.getResource()) {
    // create registration entry
    byte[] k = (EP_UID + updatedRegistration.getEndpoint()).getBytes();
    j.set(k, instanceUID.getBytes());
    j.expire(k, updatedRegistration.getLifeTimeInSec().intValue());
  }
}

代码示例来源:origin: foxinmy/weixin4j

@Override
public void caching(String key, T cache) {
  Jedis jedis = null;
  try {
    jedis = jedisPool.getResource();
    byte[] cacheKey = key.getBytes(Consts.UTF_8);
    byte[] value = SerializationUtils.serialize(cache);
    if (cache.getExpires() > 0) {
      jedis.setex(cacheKey, (int) (cache.getExpires() - CUTMS) / 1000, value);
    } else {
      jedis.set(cacheKey, value);
    }
    jedis.sadd(ALLKEY, key);
  } finally {
    if (jedis != null) {
      jedis.close();
    }
  }
}

代码示例来源:origin: penggle/jedis-ms-sentinel

@Test
public void getRourceAndReturnResource(){
  Pool<MasterSlaveJedis> pool = this.getJedisPool();
  MasterSlaveJedis masterSlaveJedis = pool.getResource();
  System.out.println(">>> masterSlaveJedis = " + masterSlaveJedis);
  pool.returnResource(masterSlaveJedis);
  
  System.out.println("--------------------------------------");
  for(int i = 0; i < 10; i++){
    masterSlaveJedis = pool.getResource();
    Jedis slaveJedis = masterSlaveJedis.opsForSlave(String.valueOf(i));
    System.out.println(">>> slaveJedis = " + slaveJedis.getClient().getHost() + ":" + slaveJedis.getClient().getPort());
    pool.returnResource(masterSlaveJedis);
  }
  System.out.println("--------------------------------------");
  
  System.out.println(">>> pool = " + pool);
  pool.destroy();
  System.out.println(">>> pool = " + pool);
  System.out.println(pool.getResource());// 如果pool已经被销毁,调用pool.getResource()会抛出"Can not get a resource from pool"异常
}

代码示例来源:origin: foxinmy/weixin4j

@Override
  public void clear() {
    Jedis jedis = null;
    try {
      jedis = jedisPool.getResource();
      Set<String> cacheKeys = jedis.smembers(ALLKEY);
      if (!cacheKeys.isEmpty()) {
        cacheKeys.add(ALLKEY);
        jedis.del(cacheKeys.toArray(new String[cacheKeys.size()]));
      }
    } finally {
      if (jedis != null) {
        jedis.close();
      }
    }
  }
}

代码示例来源:origin: org.datanucleus/datanucleus-cache

@Override
public void evict(Object oid)
{
  Jedis jedis = null;
  try
  {
    jedis = pool.getResource();
    jedis.del(getCacheKeyForId(oid).getBytes());
    pool.returnResource(jedis);
  }
  catch (Exception e)
  {
    pool.returnBrokenResource(jedis);
    throw new NucleusException(String.format("Failed to evict key %s from Redis cache", oid), e);
  }
}

代码示例来源:origin: foxinmy/weixin4j

@Override
public T evict(String key) {
  T cache = lookup(key);
  Jedis jedis = null;
  try {
    jedis = jedisPool.getResource();
    jedis.del(key);
    jedis.srem(ALLKEY, key);
  } finally {
    if (jedis != null) {
      jedis.close();
    }
  }
  return cache;
}

代码示例来源:origin: foxinmy/weixin4j

@SuppressWarnings("unchecked")
@Override
public T lookup(String key) {
  Jedis jedis = null;
  try {
    jedis = jedisPool.getResource();
    byte[] value = jedis.get(key.getBytes(Consts.UTF_8));
    return value != null ? (T) SerializationUtils.deserialize(value) : null;
  } finally {
    if (jedis != null) {
      jedis.close();
    }
  }
}

代码示例来源:origin: alibaba/jetcache

@Override
protected CacheResult do_REMOVE_ALL(Set<? extends K> keys) {
  if (keys == null) {
    return CacheResult.FAIL_ILLEGAL_ARGUMENT;
  }
  try (Jedis jedis = pool.getResource()) {
    byte[][] newKeys = keys.stream().map((k) -> buildKey(k)).toArray((len) -> new byte[keys.size()][]);
    jedis.del(newKeys);
    return CacheResult.SUCCESS_WITHOUT_MSG;
  } catch (Exception ex) {
    logError("REMOVE_ALL", "keys(" + keys.size() + ")", ex);
    return new CacheResult(ex);
  }
}

代码示例来源:origin: org.datanucleus/datanucleus-cache

public List<Object> get(String queryKey)
{
  String key = KEY_PREFIX + queryKey;
  Jedis jedis = null;
  Object value;
  try
  {
    jedis = pool.getResource();
    value = getObjectFromBytes(jedis.get(key.getBytes()));
    pool.returnResource(jedis);
  }
  catch (Exception e)
  {
    pool.returnBrokenResource(jedis);
    throw new NucleusException("Failed to get from Redis cache", e);
  }
  return (List<Object>) value;
}

代码示例来源:origin: penggle/jedis-ms-sentinel

@Test
public void getResource() throws Exception {
  Jedis master = null;
  try {
    master = this.getJedisPool().getResource();
    System.out.println(String.format(">>> jedis = %s", master));
    System.out.println(master.getClient().getHost() + ":" + master.getClient().getPort());
    //System.out.println(jedis.info()); //看以看出此处的jedis实例指向的是master
    
    Date now = new Date();
    master.set("current_time", String.format("%tF %tT", now, now));
    master.incr("COUNT");
  } finally {
    if(master != null){
      this.getJedisPool().returnResource(master);
    }
  }
}

代码示例来源:origin: eclipse/leshan

@Override
public void registered(Registration registration, Registration previousReg,
    Collection<Observation> previousObsersations) {
  String payload = RegistrationSerDes.sSerialize(registration);
  try (Jedis j = pool.getResource()) {
    j.publish(REGISTER_EVENT, payload);
  }
}

代码示例来源:origin: alibaba/jetcache

@Override
protected CacheResult do_PUT_IF_ABSENT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) {
  if (key == null) {
    return CacheResult.FAIL_ILLEGAL_ARGUMENT;
  }
  try (Jedis jedis = pool.getResource()) {
    CacheValueHolder<V> holder = new CacheValueHolder(value, timeUnit.toMillis(expireAfterWrite));
    byte[] newKey = buildKey(key);
    String rt = jedis.set(newKey, valueEncoder.apply(holder), "NX".getBytes(), "PX".getBytes(), timeUnit.toMillis(expireAfterWrite));
    if ("OK".equals(rt)) {
      return CacheResult.SUCCESS_WITHOUT_MSG;
    } else if (rt == null) {
      return CacheResult.EXISTS_WITHOUT_MSG;
    } else {
      return new CacheResult(CacheResultCode.FAIL, rt);
    }
  } catch (Exception ex) {
    logError("PUT_IF_ABSENT", key, ex);
    return new CacheResult(ex);
  }
}

代码示例来源:origin: Dreampie/Resty

private Jedis getJedis() {
 Jedis jedis = null;
 if (pool != null && pool instanceof JedisPool) {
  jedis = (Jedis) pool.getResource();
 }
 //hp[0] is hostname,hp[1] is port,hp[2] is the password of redis
 if (jedis == null) {
  String[] hp = host.split(":");
  jedis = new Jedis(hp[0], Integer.parseInt(hp[1]), timeout);
  if (hp.length >= 3) {
   jedis.auth(hp[2]);
  }
 }
 return jedis;
}

代码示例来源:origin: mpusher/mpush

/**
 * Returns a Jedis instance to be used as a Redis connection. The instance can be newly created or retrieved from a
 * pool.
 */
protected Jedis fetchJedisConnector() {
  try {
    if (pool != null) {
      return pool.getResource();
    }
    Jedis jedis = new Jedis(getShardInfo());
    // force initialization (see Jedis issue #82)
    jedis.connect();
    return jedis;
  } catch (Exception ex) {
    throw new RuntimeException("Cannot get Jedis connection", ex);
  }
}

代码示例来源:origin: org.datanucleus/datanucleus-cache

public List<Object> put(String queryKey, List<Object> results)
{
  if (queryKey == null || results == null)
  {
    return null;
  }
  Jedis jedis = null;
  String key = KEY_PREFIX + queryKey;
  try
  {
    jedis = pool.getResource();
    jedis.setex(key.getBytes(), expirySeconds, getBytesForObject(results));
    pool.returnResource(jedis);
  }
  catch (Exception e)
  {
    pool.returnBrokenResource(jedis);
    throw new NucleusException(String.format("Failed to set object %s with ID %s into Redis cache", results, queryKey));
  }
  return results;
}

代码示例来源:origin: alibaba/jetcache

@Override
protected CacheResult do_PUT_ALL(Map<? extends K, ? extends V> map, long expireAfterWrite, TimeUnit timeUnit) {
  if (map == null) {
    return CacheResult.FAIL_ILLEGAL_ARGUMENT;
  }
  try (Jedis jedis = pool.getResource()) {
    int failCount = 0;
    List<Response<String>> responses = new ArrayList<>();
    Pipeline p = jedis.pipelined();
    for (Map.Entry<? extends K, ? extends V> en : map.entrySet()) {
      CacheValueHolder<V> holder = new CacheValueHolder(en.getValue(), timeUnit.toMillis(expireAfterWrite));
      Response<String> resp = p.psetex(buildKey(en.getKey()), timeUnit.toMillis(expireAfterWrite), valueEncoder.apply(holder));
      responses.add(resp);
    }
    p.sync();
    for (Response<String> resp : responses) {
      if(!"OK".equals(resp.get())){
        failCount++;
      }
    }
    return failCount == 0 ? CacheResult.SUCCESS_WITHOUT_MSG :
        failCount == map.size() ? CacheResult.FAIL_WITHOUT_MSG : CacheResult.PART_SUCCESS_WITHOUT_MSG;
  } catch (Exception ex) {
    logError("PUT_ALL", "map(" + map.size() + ")", ex);
    return new CacheResult(ex);
  }
}

代码示例来源:origin: info.xiancloud/xian-gelf-common

protected boolean sendMessage0(GelfMessage message) {
  Jedis jedisClient = null;
  try {
    jedisClient = jedisPool.getResource();
    jedisClient.lpush(redisKey, message.toJson(""));
    return true;
  } catch (Exception e) {
    errorReporter.reportError(e.getMessage(), new IOException("Cannot send REDIS data with key URI " + redisKey, e));
    return false;
  } finally {
    if (jedisClient != null) {
      jedisPool.returnResource(jedisClient);
    }
  }
}

相关文章