Search in sources :

Example 6 with CompositeCodec

use of org.redisson.codec.CompositeCodec in project redisson by redisson.

the class RedissonMapCacheTest method testUpdatedListener.

@Test
public void testUpdatedListener() {
    RMapCache<Integer, Integer> map = redisson.getMapCache("simple");
    map.put(1, 1);
    checkUpdatedListener(map, 1, 3, 1, () -> map.put(1, 3));
    map.put(10, 1);
    checkUpdatedListener(map, 10, 2, 1, () -> map.put(10, 2, 2, TimeUnit.SECONDS));
    map.put(2, 1);
    checkUpdatedListener(map, 2, 5, 1, () -> map.fastPut(2, 5));
    map.put(13, 1);
    checkUpdatedListener(map, 13, 2, 1, () -> map.fastPut(13, 2, 2, TimeUnit.SECONDS));
    map.put(14, 1);
    checkUpdatedListener(map, 14, 2, 1, () -> map.replace(14, 2));
    checkUpdatedListener(map, 14, 3, 2, () -> map.replace(14, 2, 3));
    map.destroy();
    RMapCache<Integer, Integer> map2 = redisson.getMapCache("simple2", new CompositeCodec(redisson.getConfig().getCodec(), IntegerCodec.INSTANCE));
    map2.put(5, 1);
    checkUpdatedListener(map2, 5, 4, 1, () -> map2.addAndGet(5, 3));
    map2.destroy();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompositeCodec(org.redisson.codec.CompositeCodec) Test(org.junit.jupiter.api.Test)

Example 7 with CompositeCodec

use of org.redisson.codec.CompositeCodec in project redisson by redisson.

the class RedissonSessionRepository method loadSession.

private MapSession loadSession(String sessionId) {
    RMap<String, Object> map = redisson.getMap(keyPrefix + sessionId, new CompositeCodec(StringCodec.INSTANCE, redisson.getConfig().getCodec()));
    Set<Entry<String, Object>> entrySet = map.readAllEntrySet();
    if (entrySet.isEmpty()) {
        return null;
    }
    MapSession delegate = new MapSession(sessionId);
    for (Entry<String, Object> entry : entrySet) {
        if ("session:creationTime".equals(entry.getKey())) {
            delegate.setCreationTime(Instant.ofEpochMilli((Long) entry.getValue()));
        } else if ("session:lastAccessedTime".equals(entry.getKey())) {
            delegate.setLastAccessedTime(Instant.ofEpochMilli((Long) entry.getValue()));
        } else if ("session:maxInactiveInterval".equals(entry.getKey())) {
            delegate.setMaxInactiveInterval(Duration.ofSeconds((Long) entry.getValue()));
        } else if (entry.getKey().startsWith(SESSION_ATTR_PREFIX)) {
            delegate.setAttribute(entry.getKey().substring(SESSION_ATTR_PREFIX.length()), entry.getValue());
        }
    }
    return delegate;
}
Also used : CompositeCodec(org.redisson.codec.CompositeCodec) Entry(java.util.Map.Entry) MapSession(org.springframework.session.MapSession)

Example 8 with CompositeCodec

use of org.redisson.codec.CompositeCodec in project redisson by redisson.

the class AsyncRemoteProxy method cancelExecution.

private void cancelExecution(RemoteInvocationOptions optionsCopy, boolean mayInterruptIfRunning, RemotePromise<Object> remotePromise, String cancelRequestMapName) {
    RMap<String, RemoteServiceCancelRequest> canceledRequests = new RedissonMap<>(new CompositeCodec(StringCodec.INSTANCE, codec, codec), commandExecutor, cancelRequestMapName, null, null, null);
    canceledRequests.fastPutAsync(remotePromise.getRequestId().toString(), new RemoteServiceCancelRequest(mayInterruptIfRunning, false));
    canceledRequests.expireAsync(60, TimeUnit.SECONDS);
    // subscribe for async result if it's not expected before
    if (!optionsCopy.isResultExpected()) {
        RemoteInvocationOptions options = new RemoteInvocationOptions(optionsCopy);
        options.expectResultWithin(60, TimeUnit.SECONDS);
        CompletionStage<RRemoteServiceResponse> responseFuture = pollResponse(options.getExecutionTimeoutInMillis(), remotePromise.getRequestId(), false);
        awaitResultAsync(options, remotePromise, responseFuture);
    }
}
Also used : CompositeCodec(org.redisson.codec.CompositeCodec) RedissonMap(org.redisson.RedissonMap) RemoteInvocationOptions(org.redisson.api.RemoteInvocationOptions)

Example 9 with CompositeCodec

use of org.redisson.codec.CompositeCodec in project redisson by redisson.

the class RedissonSessionStore method getMap.

public RMap<CharSequence, Object> getMap(String sessionId) {
    String keyPrefix = sessionConfiguration.getKeyPrefix();
    String separator = keyPrefix == null || keyPrefix.isEmpty() ? "" : ":";
    String name = keyPrefix + separator + SESSION_PREFIX + sessionId;
    return redisson.getMap(name, new CompositeCodec(StringCodec.INSTANCE, getCodec(), getCodec()));
}
Also used : CompositeCodec(org.redisson.codec.CompositeCodec)

Example 10 with CompositeCodec

use of org.redisson.codec.CompositeCodec in project redisson by redisson.

the class RedissonLocalCachedMapTest method testAddAndGet.

@Test
public void testAddAndGet() throws InterruptedException {
    RLocalCachedMap<Integer, Integer> map = redisson.getLocalCachedMap("getAll", new CompositeCodec(redisson.getConfig().getCodec(), IntegerCodec.INSTANCE), LocalCachedMapOptions.defaults());
    Map<Integer, Integer> cache = map.getCachedMap();
    map.put(1, 100);
    Integer res = map.addAndGet(1, 12);
    assertThat(cache.size()).isEqualTo(1);
    assertThat(res).isEqualTo(112);
    res = map.get(1);
    assertThat(res).isEqualTo(112);
    RMap<Integer, Double> map2 = redisson.getLocalCachedMap("getAll2", new CompositeCodec(redisson.getConfig().getCodec(), DoubleCodec.INSTANCE), LocalCachedMapOptions.defaults());
    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 = redisson.getLocalCachedMap("mapStr", new CompositeCodec(redisson.getConfig().getCodec(), IntegerCodec.INSTANCE), LocalCachedMapOptions.defaults());
    assertThat(mapStr.put("1", 100)).isNull();
    assertThat(mapStr.addAndGet("1", 12)).isEqualTo(112);
    assertThat(mapStr.get("1")).isEqualTo(112);
    assertThat(cache.size()).isEqualTo(1);
}
Also used : 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