public void removeAndClose(AbstractRedisClient redisClient) { LettuceObjects lo = (LettuceObjects) map.remove(redisClient); if (lo == null) { return; } if (lo.commands != null && lo.commands instanceof RedisClusterCommands) { ((RedisClusterCommands) lo.commands).close(); } if (lo.asyncCommands != null && lo.asyncCommands instanceof RedisClusterAsyncCommands) { ((RedisClusterAsyncCommands) lo.asyncCommands).close(); } if (lo.reactiveCommands != null && lo.reactiveCommands instanceof RedisClusterReactiveCommands) { ((RedisClusterReactiveCommands) lo.reactiveCommands).close(); } if (lo.connection != null) { lo.connection.close(); } redisClient.shutdown(); } }
} else if (map.size() == 1) { String uri = (String) map.values().iterator().next(); client = RedisClient.create(uri); ((RedisClient)client).setOptions(ClientOptions.builder(). disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS).build()); } else { List<RedisURI> list = map.values().stream().map((k) -> RedisURI.create(URI.create(k.toString()))) .collect(Collectors.toList()); client = RedisClusterClient.create(list); ((RedisClusterClient)client).setOptions(ClusterClientOptions.builder(). disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS).build());
@Override public void connect() throws IOException { client = new RedisClient(host, port); connection = client.connect(); connection.select(dbIndex); }
public StatefulConnection connection(AbstractRedisClient redisClient) { LettuceObjects lo = getLettuceObjectsFromMap(redisClient); if (lo.connection == null) { if (redisClient instanceof RedisClient) { lo.connection = ((RedisClient) redisClient).connect(new JetCacheCodec()); } else if (redisClient instanceof RedisClusterClient) { lo.connection = ((RedisClusterClient) redisClient).connect(new JetCacheCodec()); } else { throw new CacheConfigException("type " + redisClient.getClass() + " is not supported"); } } return lo.connection; }
private ClientOptions buildClientOptions() { return ClientOptions.builder() .pingBeforeActivateConnection(pingBeforeActivateConnection) .autoReconnect(autoReconnect) .cancelCommandsOnReconnectFailure(cancelCommandsOnReconnectFailure) .suspendReconnectOnProtocolFailure(suspendReconnectOnProtocolFailure) .requestQueueSize(requestQueueSize) .disconnectedBehavior(disconnectedBehavior) .build(); }
RedisFuture<Long> del = null; if (!finished) { zadd = async.zadd(keyPrefix + appId, (double) System.currentTimeMillis(), (keyPrefix + transId).getBytes(StandardCharsets.UTF_8)); rpush = async.rpush(keyPrefix + transId, array); } else { zrem = async.zrem(keyPrefix + appId, (keyPrefix + transId).getBytes(StandardCharsets.UTF_8)); del = async.del(keyPrefix + transId); zadd.get(); rpush.get(); zrem.get(); del.get();
RedisFuture<List<ScoredValue<byte[]>>> zrangebyscore = async.zrangebyscoreWithScores(keyPrefix + appId, Range.from(Boundary.excluding(floorTime), Boundary.including(latestTime)), Limit.create(0, pageSize)); List<ScoredValue<byte[]>> transIds = zrangebyscore.get(); if (transIds == null || transIds.size() == 0) { return Collections.emptyList(); pair -> new Pair<>(async.lrange(pair.getKey(), 0, -1), pair.getValue()))); ObjectDigestUtil.hexStringToByteArray(entry.getKey().substring(keyPrefix.length()))); Pair<RedisFuture<List<byte[]>>, Double> value = entry.getValue(); List<byte[]> byteArrayList = value.getKey().get(); List<Content> contentList = byteArrayList.stream().map(objectSerializer::<Content>deserialize) .collect(Collectors.toList());
@Override protected CacheResult do_PUT_IF_ABSENT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) { if (key == null) { return CacheResult.FAIL_ILLEGAL_ARGUMENT; } try { CacheValueHolder<V> holder = new CacheValueHolder(value, timeUnit.toMillis(expireAfterWrite)); byte[] newKey = buildKey(key); RedisFuture<String> future = stringAsyncCommands.set(newKey, valueEncoder.apply(holder), SetArgs.Builder.nx().px(timeUnit.toMillis(expireAfterWrite))); return new CacheResult(future.handle((rt, ex) -> { if (ex != null) { JetCacheExecutor.defaultExecutor().execute(() -> logError("PUT_IF_ABSENT", key, ex)); return new ResultData(ex); } else { if ("OK".equals(rt)) { return new ResultData(CacheResultCode.SUCCESS, null, null); } else if (rt == null) { return new ResultData(CacheResultCode.EXISTS, null, null); } else { return new ResultData(CacheResultCode.FAIL, rt , null); } } })); } catch (Exception ex) { logError("PUT_IF_ABSENT", key, ex); return new CacheResult(ex); } }
@Override protected CacheResult do_REMOVE_ALL(Set<? extends K> keys) { if (keys == null) { return CacheResult.FAIL_ILLEGAL_ARGUMENT; } try { byte[][] newKeys = keys.stream().map((k) -> buildKey(k)).toArray((len) -> new byte[keys.size()][]); RedisFuture<Long> future = keyAsyncCommands.del(newKeys); return new CacheResult(future.handle((v, ex) -> { if (ex != null) { JetCacheExecutor.defaultExecutor().execute(() -> logError("REMOVE_ALL", "keys(" + keys.size() + ")", ex)); return new ResultData(ex); } else { return new ResultData(CacheResultCode.SUCCESS, null, null); } })); } catch (Exception ex) { logError("REMOVE_ALL", "keys(" + keys.size() + ")", ex); return new CacheResult(ex); } }
/** * Open a new synchronous connection to the redis server. Use the supplied * {@link RedisCodec codec} to encode/decode keys and values. * * @param codec Use this codec to encode/decode keys and values. * * @return A new connection. */ public <K, V> RedisConnection<K, V> connect(RedisCodec<K, V> codec) { return new RedisConnection<K, V>(connectAsync(codec)); }
@Override protected CacheResult do_PUT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) { if (key == null) { return CacheResult.FAIL_ILLEGAL_ARGUMENT; } try { CacheValueHolder<V> holder = new CacheValueHolder(value, timeUnit.toMillis(expireAfterWrite)); byte[] newKey = buildKey(key); RedisFuture<String> future = stringAsyncCommands.psetex(newKey, timeUnit.toMillis(expireAfterWrite), valueEncoder.apply(holder)); return new CacheResult(future.handle((rt, ex) -> { if (ex != null) { JetCacheExecutor.defaultExecutor().execute(() -> logError("PUT", key, ex)); return new ResultData(ex); } else { if ("OK".equals(rt)) { return new ResultData(CacheResultCode.SUCCESS, null, null); } else { return new ResultData(CacheResultCode.FAIL, rt, null); } } })); } catch (Exception ex) { logError("PUT", key, ex); return new CacheResult(ex); } }
return new MultiGetResult<K, V>(CacheResultCode.SUCCESS, null, resultMap); RedisFuture<List<byte[]>> mgetResults = stringAsyncCommands.mget(newKeys); return new MultiGetResult<K, V>(mgetResults.handle((list, ex) -> { if (ex != null) { JetCacheExecutor.defaultExecutor().execute(() -> logError("GET_ALL", "keys(" + keys.size() + ")", ex));
@Override protected CacheGetResult<V> do_GET(K key) { if (key == null) { return new CacheGetResult<V>(CacheResultCode.FAIL, CacheResult.MSG_ILLEGAL_ARGUMENT, null); } try { byte[] newKey = buildKey(key); RedisFuture<byte[]> future = stringAsyncCommands.get(newKey); return new CacheGetResult(future.handle((valueBytes, ex) -> { if (ex != null) { JetCacheExecutor.defaultExecutor().execute(() -> logError("GET", key, ex)); return new ResultData(ex); } else { if (valueBytes != null) { CacheValueHolder<V> holder = (CacheValueHolder<V>) valueDecoder.apply(valueBytes); if (System.currentTimeMillis() >= holder.getExpireTime()) { return new ResultData(CacheResultCode.EXPIRED, null, null); } else { return new ResultData(CacheResultCode.SUCCESS, null, holder); } } else { return new ResultData(CacheResultCode.NOT_EXISTS, null, null); } } })); } catch (Exception ex) { logError("GET", key, ex); return new CacheGetResult(ex); } }
@Override public void connect() throws IOException { client = new RedisClient(host, port); connection = client.connect(); connection.select(dbIndex); }
@Override protected CacheResult do_REMOVE(K key) { if (key == null) { return CacheResult.FAIL_ILLEGAL_ARGUMENT; } try { RedisFuture<Long> future = keyAsyncCommands.del(buildKey(key)); return new CacheResult(future.handle((rt, ex) -> { if (ex != null) { JetCacheExecutor.defaultExecutor().execute(() -> logError("REMOVE", key, ex)); return new ResultData(ex); } else { if (rt == null) { return new ResultData(CacheResultCode.FAIL, null, null); } else if (rt == 1) { return new ResultData(CacheResultCode.SUCCESS, null, null); } else if (rt == 0) { return new ResultData(CacheResultCode.NOT_EXISTS, null, null); } else { return new ResultData(CacheResultCode.FAIL, null, null); } } })); } catch (Exception ex) { logError("REMOVE", key, ex); return new CacheResult(ex); } }
/** * Open a new synchronous connection to the redis server. Use the supplied * {@link RedisCodec codec} to encode/decode keys and values. * * @param codec Use this codec to encode/decode keys and values. * * @return A new connection. */ public <K, V> RedisConnection<K, V> connect(RedisCodec<K, V> codec) { return new RedisConnection<K, V>(connectAsync(codec)); }