use of org.redisson.codec.CompositeCodec in project redisson by redisson.
the class RedissonBuckets method getAsync.
@Override
public <V> RFuture<Map<String, V>> getAsync(String... keys) {
if (keys.length == 0) {
return RedissonPromise.newSucceededFuture(Collections.emptyMap());
}
Codec commandCodec = new CompositeCodec(StringCodec.INSTANCE, codec, codec);
RedisCommand<Map<Object, Object>> command = new RedisCommand<Map<Object, Object>>("MGET", new MapGetAllDecoder(Arrays.<Object>asList(keys), 0));
return commandExecutor.readBatchedAsync(commandCodec, command, new SlotCallback<Map<Object, Object>, Map<String, V>>() {
final Map<String, V> results = new ConcurrentHashMap<>();
@Override
public void onSlotResult(Map<Object, Object> result) {
for (Map.Entry<Object, Object> entry : result.entrySet()) {
if (entry.getKey() != null && entry.getValue() != null) {
results.put((String) entry.getKey(), (V) entry.getValue());
}
}
}
@Override
public Map<String, V> onFinish() {
return results;
}
@Override
public RedisCommand<Map<Object, Object>> createCommand(List<String> keys) {
return new RedisCommand<>("MGET", new BucketsDecoder(keys));
}
}, keys);
}
use of org.redisson.codec.CompositeCodec in project redisson by redisson.
the class RedissonSessionManager method getMap.
public RMap<String, Object> getMap(String sessionId) {
String separator = keyPrefix == null || keyPrefix.isEmpty() ? "" : ":";
String name = keyPrefix + separator + "redisson:tomcat_session:" + sessionId;
return redisson.getMap(name, new CompositeCodec(StringCodec.INSTANCE, codecToUse, codecToUse));
}
use of org.redisson.codec.CompositeCodec in project redisson by redisson.
the class RedissonReliableTopic method poll.
private void poll(String id, StreamMessageId startId) {
readFuture = commandExecutor.readAsync(getRawName(), new CompositeCodec(StringCodec.INSTANCE, codec), RedisCommands.XREAD_BLOCKING_SINGLE, "BLOCK", 0, "STREAMS", getRawName(), startId);
readFuture.whenComplete((res, ex) -> {
if (readFuture.isCancelled()) {
return;
}
if (ex != null) {
if (ex instanceof RedissonShutdownException) {
return;
}
log.error(ex.getMessage(), ex);
commandExecutor.getConnectionManager().newTimeout(task -> {
poll(id, startId);
}, 1, TimeUnit.SECONDS);
return;
}
commandExecutor.getConnectionManager().getExecutor().execute(() -> {
res.values().forEach(entry -> {
Object m = entry.get("m");
listeners.values().forEach(e -> {
if (e.getType().isInstance(m)) {
((MessageListener<Object>) e.getListener()).onMessage(getRawName(), m);
}
});
});
});
if (listeners.isEmpty()) {
return;
}
StreamMessageId lastId = res.keySet().stream().skip(res.size() - 1).findFirst().get();
long time = System.currentTimeMillis();
RFuture<Boolean> updateFuture = commandExecutor.evalWriteAsync(getRawName(), StringCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, "local r = redis.call('zscore', KEYS[2], ARGV[2]); " + "if r ~= false then " + "local value = redis.call('incr', KEYS[4]); " + "redis.call('zadd', KEYS[2], value, ARGV[2]); " + "redis.call('hset', KEYS[3], ARGV[2], ARGV[1]); " + "end; " + "local t = redis.call('zrange', KEYS[5], 0, 0, 'WITHSCORES'); " + "if tonumber(t[2]) < tonumber(ARGV[3]) then " + "redis.call('hdel', KEYS[3], t[1]); " + "redis.call('zrem', KEYS[2], t[1]); " + "redis.call('zrem', KEYS[5], t[1]); " + "end; " + "local v = redis.call('zrange', KEYS[2], 0, 0); " + "local score = redis.call('hget', KEYS[3], v[1]); " + "local range = redis.call('xrange', KEYS[1], score, '+'); " + "if #range == 0 then " + "redis.call('del', KEYS[1]); " + "elseif #range == 1 and range[1][1] == score then " + "redis.call('del', KEYS[1]); " + "else " + "redis.call('xtrim', KEYS[1], 'maxlen', #range); " + "end;" + "return r ~= false; ", Arrays.asList(getRawName(), getSubscribersName(), getMapName(), getCounter(), getTimeout()), lastId, id, time);
updateFuture.whenComplete((re, exc) -> {
if (exc != null) {
if (exc instanceof RedissonShutdownException) {
return;
}
log.error("Unable to update subscriber status", exc);
return;
}
if (!re || listeners.isEmpty()) {
return;
}
poll(id, lastId);
});
});
}
use of org.redisson.codec.CompositeCodec in project redisson by redisson.
the class BaseMapTest method testAddAndGet.
@Test
public void testAddAndGet() throws InterruptedException {
RMap<Integer, Integer> map = getMap("getAll", new CompositeCodec(redisson.getConfig().getCodec(), IntegerCodec.INSTANCE));
map.put(1, 100);
Integer res = map.addAndGet(1, 12);
assertThat(res).isEqualTo(112);
res = map.get(1);
assertThat(res).isEqualTo(112);
RMap<Integer, Double> map2 = getMap("getAll2", new CompositeCodec(redisson.getConfig().getCodec(), DoubleCodec.INSTANCE));
map2.put(1, new Double(100.2));
Double res2 = map2.addAndGet(1, new Double(12.1));
assertThat(res2).isEqualTo(112.3);
res2 = map2.get(1);
assertThat(res2).isEqualTo(112.3);
RMap<String, Integer> mapStr = getMap("mapStr", new CompositeCodec(redisson.getConfig().getCodec(), IntegerCodec.INSTANCE));
assertThat(mapStr.put("1", 100)).isNull();
assertThat(mapStr.addAndGet("1", 12)).isEqualTo(112);
assertThat(mapStr.get("1")).isEqualTo(112);
destroy(map);
}
use of org.redisson.codec.CompositeCodec in project redisson by redisson.
the class RedissonMapCacheTest method testCreatedListener.
@Test
public void testCreatedListener() {
RMapCache<Integer, Integer> map = redisson.getMapCache("simple");
checkCreatedListener(map, 1, 2, () -> map.put(1, 2));
checkCreatedListener(map, 10, 2, () -> map.put(10, 2, 2, TimeUnit.SECONDS));
checkCreatedListener(map, 2, 5, () -> map.fastPut(2, 5));
checkCreatedListener(map, 13, 2, () -> map.fastPut(13, 2, 2, TimeUnit.SECONDS));
checkCreatedListener(map, 3, 2, () -> map.putIfAbsent(3, 2));
checkCreatedListener(map, 14, 2, () -> map.putIfAbsent(14, 2, 2, TimeUnit.SECONDS));
checkCreatedListener(map, 4, 1, () -> map.fastPutIfAbsent(4, 1));
checkCreatedListener(map, 15, 2, () -> map.fastPutIfAbsent(15, 2, 2, TimeUnit.SECONDS));
map.destroy();
RMapCache<Integer, Integer> map2 = redisson.getMapCache("simple3", new CompositeCodec(redisson.getConfig().getCodec(), IntegerCodec.INSTANCE));
checkCreatedListener(map2, 5, 10, () -> map2.addAndGet(5, 10));
map2.destroy();
}
Aggregations