Search in sources :

Example 1 with CompositeCodec

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);
}
Also used : CompositeCodec(org.redisson.codec.CompositeCodec) Codec(org.redisson.client.codec.Codec) StringCodec(org.redisson.client.codec.StringCodec) CompositeCodec(org.redisson.codec.CompositeCodec) RedisCommand(org.redisson.client.protocol.RedisCommand) MapGetAllDecoder(org.redisson.connection.decoder.MapGetAllDecoder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BucketsDecoder(org.redisson.connection.decoder.BucketsDecoder)

Example 2 with CompositeCodec

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));
}
Also used : CompositeCodec(org.redisson.codec.CompositeCodec)

Example 3 with CompositeCodec

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);
        });
    });
}
Also used : CompositeCodec(org.redisson.codec.CompositeCodec) MessageListener(org.redisson.api.listener.MessageListener) StreamMessageId(org.redisson.api.StreamMessageId)

Example 4 with CompositeCodec

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);
}
Also used : CompositeCodec(org.redisson.codec.CompositeCodec) Test(org.junit.jupiter.api.Test)

Example 5 with CompositeCodec

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompositeCodec(org.redisson.codec.CompositeCodec) Test(org.junit.jupiter.api.Test)

Aggregations

CompositeCodec (org.redisson.codec.CompositeCodec)12 Test (org.junit.jupiter.api.Test)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 RedissonMap (org.redisson.RedissonMap)1 RemoteInvocationOptions (org.redisson.api.RemoteInvocationOptions)1 StreamMessageId (org.redisson.api.StreamMessageId)1 MessageListener (org.redisson.api.listener.MessageListener)1 Codec (org.redisson.client.codec.Codec)1 StringCodec (org.redisson.client.codec.StringCodec)1 RedisCommand (org.redisson.client.protocol.RedisCommand)1 BucketsDecoder (org.redisson.connection.decoder.BucketsDecoder)1 MapGetAllDecoder (org.redisson.connection.decoder.MapGetAllDecoder)1 MapSession (org.springframework.session.MapSession)1