本文整理了Java中redis.clients.util.Pool.getResource()
方法的一些代码示例,展示了Pool.getResource()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Pool.getResource()
方法的具体详情如下:
包路径:redis.clients.util.Pool
类名称:Pool
方法名:getResource
暂无
代码示例来源:origin: Dreampie/Resty
private ShardedJedis getShardedJedis() {
ShardedJedis shardedJedis = null;
if (pool != null && pool instanceof ShardedJedisPool) {
shardedJedis = (ShardedJedis) pool.getResource();
}
return shardedJedis;
}
代码示例来源:origin: sohutv/cachecloud
@Override
public ShardedJedis getResource() {
ShardedJedis jedis = super.getResource();
jedis.setDataSource(this);
return jedis;
}
代码示例来源: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: 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: 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: alibaba/jetcache
private CacheResult REMOVE_impl(Object key, byte[] newKey) {
try (Jedis jedis = pool.getResource()) {
Long rt = jedis.del(newKey);
if (rt == null) {
return CacheResult.FAIL_WITHOUT_MSG;
} else if (rt == 1) {
return CacheResult.SUCCESS_WITHOUT_MSG;
} else if (rt == 0) {
return new CacheResult(CacheResultCode.NOT_EXISTS, null);
} else {
return CacheResult.FAIL_WITHOUT_MSG;
}
} catch (Exception ex) {
logError("REMOVE", key, ex);
return new CacheResult(ex);
}
}
代码示例来源:origin: alibaba/jetcache
return new MultiGetResult<>(CacheResultCode.FAIL, CacheResult.MSG_ILLEGAL_ARGUMENT, null);
try (Jedis jedis = pool.getResource()) {
ArrayList<K> keyList = new ArrayList<K>(keys);
byte[][] newKeys = keyList.stream().map((k) -> buildKey(k)).toArray(byte[][]::new);
代码示例来源: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: alibaba/jetcache
@Override
protected CacheResult do_PUT(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.psetex(newKey, timeUnit.toMillis(expireAfterWrite), valueEncoder.apply(holder));
if ("OK".equals(rt)) {
return CacheResult.SUCCESS_WITHOUT_MSG;
} else {
return new CacheResult(CacheResultCode.FAIL, rt);
}
} catch (Exception ex) {
logError("PUT", key, ex);
return new CacheResult(ex);
}
}
代码示例来源: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: 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: 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: 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
@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: Baqend/Orestes-Bloomfilter
public <T> T safelyReturn(Function<Jedis, T> f) {
try (Jedis jedis = pool.getResource()) {
return f.apply(jedis);
}
}
代码示例来源:origin: eclipse/leshan
@Override
public Deregistration removeRegistration(String registrationId) {
try (Jedis j = pool.getResource()) {
return removeRegistration(j, registrationId, false);
}
}
代码示例来源: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: eclipse/leshan
@Override
public Registration getRegistrationByEndpoint(String endpoint) {
Validate.notNull(endpoint);
try (Jedis j = pool.getResource()) {
byte[] data = j.get(toEndpointKey(endpoint));
if (data == null) {
return null;
}
return deserializeReg(data);
}
}
代码示例来源:origin: eclipse/leshan
private void sendResponse(String ticket, LwM2mResponse response) {
if (response instanceof ObserveResponse) {
Observation observation = ((ObserveResponse) response).getObservation();
observatioIdToTicket.put(new KeyId(observation.getId()), ticket);
}
try (Jedis j = pool.getResource()) {
JsonObject m = Json.object();
m.add("ticket", ticket);
m.add("rep", ResponseSerDes.jSerialize(response));
j.publish(RESPONSE_CHANNEL, m.toString());
}
}
内容来源于网络,如有侵权,请联系作者删除!