Java Code Examples for org.redisson.api.RMapCache#put()

The following examples show how to use org.redisson.api.RMapCache#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testExpirationWithMaxSize() throws InterruptedException {
    Config config = new Config();
    config.useSingleServer().setAddress(RedisRunner.getDefaultRedisServerBindAddressAndPort());
    config.setMaxCleanUpDelay(2);
    config.setMinCleanUpDelay(1);
    RedissonClient redisson = Redisson.create(config);

    RMapCache<String, String> map = redisson.getMapCache("test", StringCodec.INSTANCE);
    assertThat(map.trySetMaxSize(2)).isTrue();

    map.put("1", "1", 3, TimeUnit.SECONDS);
    map.put("2", "2", 0, TimeUnit.SECONDS, 3, TimeUnit.SECONDS);
    map.put("3", "3", 3, TimeUnit.SECONDS);
    map.put("4", "4", 0, TimeUnit.SECONDS, 3, TimeUnit.SECONDS);

    Thread.sleep(5000);

    assertThat(map.size()).isZero();

    assertThat(redisson.getKeys().count()).isEqualTo(2);
    redisson.shutdown();
}
 
Example 2
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testFastPutIfAbsentTTL() throws Exception {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
    SimpleKey key = new SimpleKey("1");
    SimpleValue value = new SimpleValue("2");
    map.put(key, value);
    assertThat(map.fastPutIfAbsent(key, new SimpleValue("3"))).isFalse();
    assertThat(map.get(key)).isEqualTo(value);

    SimpleKey key1 = new SimpleKey("2");
    SimpleValue value1 = new SimpleValue("4");
    assertThat(map.fastPutIfAbsent(key1, value1)).isTrue();
    assertThat(map.get(key1)).isEqualTo(value1);
    
    SimpleKey key2 = new SimpleKey("3");
    map.put(key2, new SimpleValue("31"), 500, TimeUnit.MILLISECONDS);
    assertThat(map.fastPutIfAbsent(key2, new SimpleValue("32"))).isFalse();
    
    Thread.sleep(500);
    assertThat(map.fastPutIfAbsent(key2, new SimpleValue("32"))).isTrue();
    assertThat(map.get(key2)).isEqualTo(new SimpleValue("32"));
    map.destroy();

}
 
Example 3
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testScheduler() throws InterruptedException {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple3");
    Assert.assertNull(map.get(new SimpleKey("33")));

    map.put(new SimpleKey("33"), new SimpleValue("44"), 5, TimeUnit.SECONDS);
    map.put(new SimpleKey("10"), new SimpleValue("32"), 5, TimeUnit.SECONDS, 2, TimeUnit.SECONDS);
    map.put(new SimpleKey("01"), new SimpleValue("92"), 0, null, 2, TimeUnit.SECONDS);

    Assert.assertEquals(3, map.size());

    Thread.sleep(11000);

    Assert.assertEquals(0, map.size());
    map.destroy();

}
 
Example 4
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceOldValueFail() {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
    map.put(new SimpleKey("1"), new SimpleValue("2"));

    boolean res = map.replace(new SimpleKey("1"), new SimpleValue("43"), new SimpleValue("31"));
    Assert.assertFalse(res);

    SimpleValue val1 = map.get(new SimpleKey("1"));
    Assert.assertEquals("2", val1.getValue());
    map.destroy();
}
 
Example 5
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testEntrySetByPatternTTL() {
    RMapCache<String, String> map = redisson.getMapCache("simple", StringCodec.INSTANCE);
    map.put("10", "100");
    map.put("20", "200", 1, TimeUnit.MINUTES);
    map.put("30", "300");

    assertThat(map.entrySet("?0")).containsExactly(new AbstractMap.SimpleEntry("10", "100"), new AbstractMap.SimpleEntry("20", "200"), new AbstractMap.SimpleEntry("30", "300"));
    assertThat(map.entrySet("1")).isEmpty();
    assertThat(map.entrySet("10")).containsExactly(new AbstractMap.SimpleEntry("10", "100"));
    map.destroy();
}
 
Example 6
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testValuesByPatternTTL() {
    RMapCache<String, String> map = redisson.getMapCache("simple", StringCodec.INSTANCE);
    map.put("10", "100");
    map.put("20", "200", 1, TimeUnit.MINUTES);
    map.put("30", "300");

    assertThat(map.values("?0")).containsExactly("100", "200", "300");
    assertThat(map.values("1")).isEmpty();
    assertThat(map.values("10")).containsExactly("100");
    map.destroy();
}
 
Example 7
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveValueFail() {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
    map.put(new SimpleKey("1"), new SimpleValue("2"));

    boolean res = map.remove(new SimpleKey("2"), new SimpleValue("1"));
    Assert.assertFalse(res);

    boolean res1 = map.remove(new SimpleKey("1"), new SimpleValue("3"));
    Assert.assertFalse(res1);

    SimpleValue val1 = map.get(new SimpleKey("1"));
    Assert.assertEquals("2", val1.getValue());
    map.destroy();
}
 
Example 8
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutIfAbsentTTL() throws Exception {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
    SimpleKey key = new SimpleKey("1");
    SimpleValue value = new SimpleValue("2");
    map.put(key, value);
    Assert.assertEquals(value, map.putIfAbsent(key, new SimpleValue("3"), 1, TimeUnit.SECONDS));
    Assert.assertEquals(value, map.get(key));

    map.putIfAbsent(new SimpleKey("4"), new SimpleValue("4"), 1, TimeUnit.SECONDS);
    Assert.assertEquals(new SimpleValue("4"), map.get(new SimpleKey("4")));
    
    Thread.sleep(1000);

    Assert.assertNull(map.get(new SimpleKey("4")));
    
    // this should be passed
    map.putIfAbsent(new SimpleKey("4"), new SimpleValue("4"), 1, TimeUnit.SECONDS);
    Assert.assertEquals(new SimpleValue("4"), map.get(new SimpleKey("4")));
    

    SimpleKey key1 = new SimpleKey("2");
    SimpleValue value1 = new SimpleValue("4");
    Assert.assertNull(map.putIfAbsent(key1, value1, 2, TimeUnit.SECONDS));
    Assert.assertEquals(value1, map.get(key1));
    map.destroy();
}
 
Example 9
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@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();

}
 
Example 10
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceValueTTL() throws InterruptedException {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
    map.put(new SimpleKey("1"), new SimpleValue("2"), 1, TimeUnit.SECONDS);

    Thread.sleep(1000);
    
    SimpleValue res = map.replace(new SimpleKey("1"), new SimpleValue("3"));
    assertThat(res).isNull();

    SimpleValue val1 = map.get(new SimpleKey("1"));
    assertThat(val1).isNull();
    map.destroy();
}
 
Example 11
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testClearExpire() throws InterruptedException {
    RMapCache<String, String> cache = redisson.getMapCache("simple");
    cache.put("0", "8", 1, TimeUnit.SECONDS);

    cache.expireAt(System.currentTimeMillis() + 100);

    cache.clearExpire();

    Thread.sleep(500);

    Assert.assertEquals(1, cache.size());
    cache.destroy();
}
 
Example 12
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpireAt() throws InterruptedException {
    RMapCache<String, String> cache = redisson.getMapCache("simple");
    cache.put("0", "8", 1, TimeUnit.SECONDS);

    cache.expireAt(System.currentTimeMillis() + 100);

    Thread.sleep(500);

    Assert.assertEquals(0, cache.size());
    cache.destroy();
}
 
Example 13
Source File: LdempotentMethodInterceptor.java    From fast-family-master with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    String ldempotentToken = Optional.ofNullable(WebUtils.getHttpServletRequest().getHeader(commonProperties.getLdempotent().getLdempotentHeaderName()))
            .orElseThrow(() -> new LdempotentException(ResponseCode.LDEMPOTENT_ERROR.getCode(),ResponseCode.LDEMPOTENT_ERROR.getMessage()));
    RMapCache<String,String> rMapCache = redissonClient.getMapCache(commonProperties.getLdempotent().getLdempotentKey());
    if (rMapCache.put(commonProperties.getLdempotent().getLdempotentPrefix() + ldempotentToken,ldempotentToken,
            commonProperties.getLdempotent().getTtl(), TimeUnit.MINUTES) != null){
        throw new LdempotentException(ResponseCode.LDEMPOTENT_ERROR.getCode(),ResponseCode.LDEMPOTENT_ERROR.getMessage());
    }
    return invocation.proceed();
}
 
Example 14
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceOldValueSuccess() {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
    map.put(new SimpleKey("1"), new SimpleValue("2"));

    boolean res = map.replace(new SimpleKey("1"), new SimpleValue("2"), new SimpleValue("3"));
    Assert.assertTrue(res);

    boolean res1 = map.replace(new SimpleKey("1"), new SimpleValue("2"), new SimpleValue("3"));
    Assert.assertFalse(res1);

    SimpleValue val1 = map.get(new SimpleKey("1"));
    Assert.assertEquals("3", val1.getValue());
    map.destroy();
}
 
Example 15
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemovedListener() {
    RMapCache<Integer, Integer> map = redisson.getMapCache("simple");

    map.put(1, 1);
    checkRemovedListener(map, 1, 1, () -> map.remove(1, 1));
    
    map.put(10, 1);
    checkRemovedListener(map, 10, 1, () -> map.remove(10));
    
    map.put(2, 1);
    checkRemovedListener(map, 2, 1, () -> map.fastRemove(2));
    map.destroy();
}
 
Example 16
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemainTimeToLive() {
    RMapCache<String, String> map = redisson.getMapCache("test");
    map.put("1", "2", 2, TimeUnit.SECONDS);
    assertThat(map.remainTimeToLive("1")).isBetween(1900L, 2000L);
    map.put("3", "4");
    assertThat(map.remainTimeToLive("3")).isEqualTo(-1);
    assertThat(map.remainTimeToLive("0")).isEqualTo(-2);

    map.put("5", "6", 20, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);
    assertThat(map.remainTimeToLive("1")).isLessThan(9900);
    map.destroy();
}
 
Example 17
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testSizeInMemory() {
    Assume.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("4.0.0") > 0);
    
    RMapCache<Integer, Integer> map = redisson.getMapCache("test");
    for (int i = 0; i < 10; i++) {
        map.put(i, i, 5, TimeUnit.SECONDS);
    }
    
    assertThat(map.sizeInMemory()).isGreaterThanOrEqualTo(466);
}
 
Example 18
Source File: RedissonTransactionalMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutIfAbsentTTL() throws InterruptedException {
    RMapCache<Object, Object> m = redisson.getMapCache("test");
    m.put("1", "2");
    m.put("3", "4");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMapCache<Object, Object> map = transaction.getMapCache("test");
    assertThat(map.putIfAbsent("3", "2", 1, TimeUnit.SECONDS)).isEqualTo("4");
    assertThat(map.putIfAbsent("5", "6", 3, TimeUnit.SECONDS)).isNull();
    assertThat(map.putIfAbsent("5", "7", 1, TimeUnit.SECONDS)).isEqualTo("6");
    
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.size()).isEqualTo(2);
    
    transaction.commit();
    
    assertThat(m.get("1")).isEqualTo("2");
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.get("5")).isEqualTo("6");
    
    Thread.sleep(1500);
    
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.get("5")).isEqualTo("6");
    
    Thread.sleep(1500);
    
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.get("5")).isNull();
}
 
Example 19
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadAllValuesTTL() {
    final RMapCache<String, String> map = redisson.getMapCache("testRMapCacheAllValues");
    map.put("1234", "5678", 1, TimeUnit.MINUTES, 60, TimeUnit.MINUTES);
    assertThat(map.readAllValues()).containsOnly("5678");
    map.destroy();
}
 
Example 20
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testRemoveValueTTL() throws InterruptedException {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
    map.put(new SimpleKey("1"), new SimpleValue("2"), 1, TimeUnit.SECONDS);

    boolean res = map.remove(new SimpleKey("1"), new SimpleValue("2"));
    Assert.assertTrue(res);

    SimpleValue val1 = map.get(new SimpleKey("1"));
    Assert.assertNull(val1);

    Assert.assertEquals(0, map.size());
    
    map.put(new SimpleKey("3"), new SimpleValue("4"), 1, TimeUnit.SECONDS);

    Thread.sleep(1000);
    
    assertThat(map.remove(new SimpleKey("3"), new SimpleValue("4"))).isFalse();

    assertThat(map.get(new SimpleKey("3"))).isNull();
    map.destroy();
}