org.redisson.api.RMapCache Java Examples

The following examples show how to use org.redisson.api.RMapCache. 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 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 #2
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testKeySet() throws InterruptedException {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple03");
    map.put(new SimpleKey("33"), new SimpleValue("44"), 1, TimeUnit.SECONDS);
    map.put(new SimpleKey("1"), new SimpleValue("2"));

    Assert.assertTrue(map.keySet().contains(new SimpleKey("33")));
    Assert.assertFalse(map.keySet().contains(new SimpleKey("44")));
    Assert.assertTrue(map.keySet().contains(new SimpleKey("1")));

    Thread.sleep(1000);

    Assert.assertFalse(map.keySet().contains(new SimpleKey("33")));
    Assert.assertFalse(map.keySet().contains(new SimpleKey("44")));
    Assert.assertTrue(map.keySet().contains(new SimpleKey("1")));
    map.destroy();
}
 
Example #3
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddAndGetTTL() {
    RMapCache<String, Object> mapCache = redisson.getMapCache("test_put_if_absent", LongCodec.INSTANCE);
    assertThat(mapCache.putIfAbsent("4", 0L, 10000L, TimeUnit.SECONDS)).isNull();
    assertThat(mapCache.addAndGet("4", 1L)).isEqualTo(1L);
    assertThat(mapCache.putIfAbsent("4", 0L)).isEqualTo(1L);
    Assert.assertEquals(1L, mapCache.get("4"));
    mapCache.destroy();
    mapCache = redisson.getMapCache("test_put_if_absent_1", LongCodec.INSTANCE);
    mapCache.putIfAbsent("4", 0L);
    mapCache.addAndGet("4", 1L);
    mapCache.putIfAbsent("4", 0L);
    Assert.assertEquals(1L, mapCache.get("4"));
    RMap map = redisson.getMap("test_put_if_absent_2", LongCodec.INSTANCE);
    map.putIfAbsent("4", 0L);
    map.addAndGet("4", 1L);
    map.putIfAbsent("4", 0L);
    Assert.assertEquals(1L, map.get("4"));
    RMapCache<String, Object> mapCache1 = redisson.getMapCache("test_put_if_absent_3", DoubleCodec.INSTANCE);
    mapCache1.putIfAbsent("4", 1.23, 10000L, TimeUnit.SECONDS);
    mapCache1.addAndGet("4", 1D);
    Assert.assertEquals(2.23, mapCache1.get("4"));
    mapCache.destroy();
    mapCache1.destroy();
}
 
Example #4
Source File: RedissonStorage.java    From redisson with Apache License 2.0 6 votes vote down vote up
public RedissonStorage(RMapCache<Object, Object> mapCache, ConnectionManager connectionManager, Map<String, Object> properties, String defaultKey) {
    super();
    this.mapCache = mapCache;
    this.connectionManager = connectionManager;
    
    String maxEntries = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.MAX_ENTRIES_SUFFIX);
    if (maxEntries != null) {
        mapCache.setMaxSize(Integer.valueOf(maxEntries));
    }
    String timeToLive = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.TTL_SUFFIX);
    if (timeToLive != null) {
        ttl = Integer.valueOf(timeToLive);
    }
    String maxIdleTime = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.MAX_IDLE_SUFFIX);
    if (maxIdleTime != null) {
        maxIdle = Integer.valueOf(maxIdleTime);
    }

    String fallbackValue = (String) properties.getOrDefault(RedissonRegionFactory.FALLBACK, "false");
    fallback = Boolean.valueOf(fallbackValue);
}
 
Example #5
Source File: RedissonRegionFactory.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
protected DomainDataStorageAccess createDomainDataStorageAccess(DomainDataRegionConfig regionConfig,
        DomainDataRegionBuildingContext buildingContext) {
    String defaultKey = null;
    if (!regionConfig.getCollectionCaching().isEmpty()) {
        defaultKey = COLLECTION_DEF;
    } else if (!regionConfig.getEntityCaching().isEmpty()) {
        defaultKey = ENTITY_DEF;
    } else if (!regionConfig.getNaturalIdCaching().isEmpty()) {
        defaultKey = NATURAL_ID_DEF;
    } else {
        throw new IllegalArgumentException("Unable to determine entity cache type!");
    }
    
    RMapCache<Object, Object> mapCache = getCache(regionConfig.getRegionName(), buildingContext.getSessionFactory().getProperties(), defaultKey);
    return new RedissonStorage(mapCache, ((Redisson)redisson).getConnectionManager(), buildingContext.getSessionFactory().getProperties(), defaultKey);
}
 
Example #6
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testExpireOverwrite() throws InterruptedException, ExecutionException {
    RMapCache<String, Integer> map = redisson.getMapCache("simple");
    map.put("123", 3, 1, TimeUnit.SECONDS);

    Thread.sleep(800);

    map.put("123", 3, 1, TimeUnit.SECONDS);

    Thread.sleep(800);
    Assert.assertEquals(3, (int)map.get("123"));

    Thread.sleep(200);

    Assert.assertFalse(map.containsKey("123"));
    map.destroy();
}
 
Example #7
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
private void checkCreatedListener(RMapCache<Integer, Integer> map, Integer key, Integer value, Runnable runnable) {
    AtomicBoolean ref = new AtomicBoolean();
    int createListener1 = map.addListener(new EntryCreatedListener<Integer, Integer>() {

        @Override
        public void onCreated(EntryEvent<Integer, Integer> event) {
            try {
                assertThat(event.getKey()).isEqualTo(key);
                assertThat(event.getValue()).isEqualTo(value);
                
                if (!ref.compareAndSet(false, true)) {
                    Assert.fail();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    });
    runnable.run();

    await().atMost(Duration.ONE_SECOND).untilTrue(ref);
    map.removeListener(createListener1);
    map.destroy();
}
 
Example #8
Source File: BaseRegion.java    From redisson with Apache License 2.0 6 votes vote down vote up
public BaseRegion(RMapCache<Object, Object> mapCache, ConnectionManager connectionManager, RegionFactory regionFactory, CacheDataDescription metadata, Properties properties, String defaultKey) {
    super();
    this.mapCache = mapCache;
    this.regionFactory = regionFactory;
    this.metadata = metadata;
    this.connectionManager = connectionManager;
    
    String maxEntries = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.MAX_ENTRIES_SUFFIX);
    if (maxEntries != null) {
        mapCache.setMaxSize(Integer.valueOf(maxEntries));
    }
    String timeToLive = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.TTL_SUFFIX);
    if (timeToLive != null) {
        ttl = Integer.valueOf(timeToLive);
    }
    String maxIdleTime = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.MAX_IDLE_SUFFIX);
    if (maxIdleTime != null) {
        maxIdle = Integer.valueOf(maxIdleTime);
    }

    String fallbackValue = (String) properties.getOrDefault(RedissonRegionFactory.FALLBACK, "false");
    fallback = Boolean.valueOf(fallbackValue);
}
 
Example #9
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #10
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testFastPutIfAbsentWithTTL() throws Exception {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simpleTTL");
    SimpleKey key = new SimpleKey("1");
    SimpleValue value = new SimpleValue("2");
    map.fastPutIfAbsent(key, value, 1, TimeUnit.SECONDS);
    assertThat(map.fastPutIfAbsent(key, new SimpleValue("3"), 1, TimeUnit.SECONDS)).isFalse();
    assertThat(map.get(key)).isEqualTo(value);
    
    Thread.sleep(1100);
    
    assertThat(map.fastPutIfAbsent(key, new SimpleValue("3"), 1, TimeUnit.SECONDS)).isTrue();
    assertThat(map.get(key)).isEqualTo(new SimpleValue("3"));
    
    assertThat(map.fastPutIfAbsent(key, new SimpleValue("4"), 1, TimeUnit.SECONDS)).isFalse();
    assertThat(map.get(key)).isEqualTo(new SimpleValue("3"));
    
    Thread.sleep(1100);
    assertThat(map.fastPutIfAbsent(key, new SimpleValue("4"), 1, TimeUnit.SECONDS, 500, TimeUnit.MILLISECONDS)).isTrue();
    
    Thread.sleep(550);
    assertThat(map.fastPutIfAbsent(key, new SimpleValue("5"), 1, TimeUnit.SECONDS, 500, TimeUnit.MILLISECONDS)).isTrue();
    map.destroy();

}
 
Example #11
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriterFastPutTTL() {
    Map<String, String> store = new HashMap<>();
    RMapCache<String, String> map = (RMapCache<String, String>) getWriterTestMap("test", store);

    map.fastPut("1", "11", 10, TimeUnit.SECONDS);
    map.fastPut("2", "22", 10, TimeUnit.SECONDS);
    map.fastPut("3", "33", 10, TimeUnit.SECONDS);
    
    Map<String, String> expected = new HashMap<>();
    expected.put("1", "11");
    expected.put("2", "22");
    expected.put("3", "33");
    assertThat(store).isEqualTo(expected);
    map.destroy();
}
 
Example #12
Source File: BaseRegion.java    From redisson with Apache License 2.0 6 votes vote down vote up
public BaseRegion(RMapCache<Object, Object> mapCache, ConnectionManager connectionManager, RegionFactory regionFactory, CacheDataDescription metadata, Properties properties, String defaultKey) {
    super();
    this.mapCache = mapCache;
    this.regionFactory = regionFactory;
    this.metadata = metadata;
    this.connectionManager = connectionManager;
    
    String maxEntries = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.MAX_ENTRIES_SUFFIX);
    if (maxEntries != null) {
        mapCache.setMaxSize(Integer.valueOf(maxEntries));
    }
    String timeToLive = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.TTL_SUFFIX);
    if (timeToLive != null) {
        ttl = Integer.valueOf(timeToLive);
    }
    String maxIdleTime = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.MAX_IDLE_SUFFIX);
    if (maxIdleTime != null) {
        maxIdle = Integer.valueOf(maxIdleTime);
    }

    String fallbackValue = (String) properties.getOrDefault(RedissonRegionFactory.FALLBACK, "false");
    fallback = Boolean.valueOf(fallbackValue);
}
 
Example #13
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testContainsKeyTTL() throws InterruptedException {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple30");
    map.put(new SimpleKey("33"), new SimpleValue("44"), 1, TimeUnit.SECONDS);

    Assert.assertTrue(map.containsKey(new SimpleKey("33")));
    Assert.assertFalse(map.containsKey(new SimpleKey("34")));

    Thread.sleep(1000);

    Assert.assertFalse(map.containsKey(new SimpleKey("33")));
    map.destroy();
}
 
Example #14
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 #15
Source File: RedissonRegionFactory.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties properties) throws CacheException {
    log.debug("Building query cache region: " + regionName);
    
    RMapCache<Object, Object> mapCache = getCache(regionName, properties, QUERY_DEF);
    return new RedissonQueryRegion(mapCache, ((Redisson)redisson).getConnectionManager(),this, properties, QUERY_DEF);
}
 
Example #16
Source File: RedissonRegionFactory.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public TimestampsRegion buildTimestampsRegion(String regionName, Properties properties) throws CacheException {
    log.debug("Building timestamps cache region: " + regionName);
    
    RMapCache<Object, Object> mapCache = getCache(regionName, properties, TIMESTAMPS_DEF);
    return new RedissonTimestampsRegion(mapCache, ((Redisson)redisson).getConnectionManager(),this, properties, TIMESTAMPS_DEF);
}
 
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: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testFastPutTTL() throws InterruptedException {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("getAll");
    map.trySetMaxSize(1);
    map.fastPut(new SimpleKey("1"), new SimpleValue("3"), 5, TimeUnit.SECONDS, 0, TimeUnit.SECONDS);
    Thread.sleep(5000);
    assertThat(map.get(new SimpleKey("1"))).isNull();

    map.fastPut(new SimpleKey("1"), new SimpleValue("4"), 5, TimeUnit.SECONDS, 0, TimeUnit.SECONDS);
    Thread.sleep(10000);
    assertThat(map.get(new SimpleKey("1"))).isNull();
}
 
Example #19
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriterPutIfAbsentTTL() {
    Map<String, String> store = new HashMap<>();
    RMapCache<String, String> map = (RMapCache<String, String>) getWriterTestMap("test", store);

    map.putIfAbsent("1", "11", 10, TimeUnit.SECONDS);
    map.putIfAbsent("1", "00", 10, TimeUnit.SECONDS);
    map.putIfAbsent("2", "22", 10, TimeUnit.SECONDS);
    
    Map<String, String> expected = new HashMap<>();
    expected.put("1", "11");
    expected.put("2", "22");
    assertThat(store).isEqualTo(expected);
    map.destroy();
}
 
Example #20
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testEntrySet() throws InterruptedException {
    RMapCache<Integer, String> map = redisson.getMapCache("simple12");
    map.put(1, "12");
    map.put(2, "33", 1, TimeUnit.SECONDS);
    map.put(3, "43");

    Map<Integer, String> expected = new HashMap<>();
    map.put(1, "12");
    map.put(3, "43");
    
    assertThat(map.entrySet()).containsAll(expected.entrySet());
    assertThat(map).hasSize(3);
    map.destroy();
}
 
Example #21
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 #22
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpiredListener() {
    RMapCache<Integer, Integer> map = redisson.getMapCache("simple");

    checkExpiredListener(map, 10, 2, () -> map.put(10, 2, 2, TimeUnit.SECONDS));
    checkExpiredListener(map, 13, 2, () -> map.fastPut(13, 2, 2, TimeUnit.SECONDS));
    checkExpiredListener(map, 14, 2, () -> map.putIfAbsent(14, 2, 2, TimeUnit.SECONDS));
    checkExpiredListener(map, 15, 2, () -> map.fastPutIfAbsent(15, 2, 2, TimeUnit.SECONDS));
    map.destroy();
}
 
Example #23
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpire() throws InterruptedException {
    RMapCache<String, String> cache = redisson.getMapCache("simple");
    cache.put("0", "8", 1, TimeUnit.SECONDS);

    cache.expire(100, TimeUnit.MILLISECONDS);

    Thread.sleep(500);

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

    Thread.sleep(1000);

    assertThat(cache.keySet()).containsOnly("0", "2", "3");
    cache.destroy();
}
 
Example #25
Source File: RedissonMapReduceTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
private RMap<String, String> getMap() {
    RMap<String, String> map = null;
    if (RMapCache.class.isAssignableFrom(mapClass)) {
        map = redisson.getMapCache("map");
    } else {
        map = redisson.getMap("map");
    }
    return map;
}
 
Example #26
Source File: EntryEvent.java    From redisson with Apache License 2.0 5 votes vote down vote up
public EntryEvent(RMapCache<K, V> source, Type type, K key, V value, V oldValue) {
    super();
    this.source = source;
    this.type = type;
    this.key = key;
    this.value = value;
    this.oldValue = oldValue;
}
 
Example #27
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testCacheValues() {
    final RMapCache<String, String> map = redisson.getMapCache("testRMapCacheValues");
    map.put("1234", "5678", 0, TimeUnit.MINUTES, 60, TimeUnit.MINUTES);
    assertThat(map.values()).containsOnly("5678");
    map.destroy();
}
 
Example #28
Source File: BaseMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testValueSize() {
    Assume.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("3.2.0") > 0);
    RMap<String, String> map = getMap("getAll");
    Assume.assumeTrue(!(map instanceof RMapCache));
    map.put("1", "1234");
    assertThat(map.valueSize("4")).isZero();
    assertThat(map.valueSize("1")).isEqualTo(7);
    destroy(map);
}
 
Example #29
Source File: RedissonRegionFactory.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public TimestampsRegion buildTimestampsRegion(String regionName, Properties properties) throws CacheException {
    log.debug("Building timestamps cache region: " + regionName);
    
    RMapCache<Object, Object> mapCache = getCache(regionName, properties, TIMESTAMPS_DEF);
    return new RedissonTimestampsRegion(mapCache, ((Redisson)redisson).getConnectionManager(),this, properties, TIMESTAMPS_DEF);
}
 
Example #30
Source File: RedissonRegionFactory.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public NaturalIdRegion buildNaturalIdRegion(String regionName, Properties properties, CacheDataDescription metadata)
        throws CacheException {
    log.debug("Building naturalId cache region: " + regionName);
    
    RMapCache<Object, Object> mapCache = getCache(regionName, properties, NATURAL_ID_DEF);
    return new RedissonNaturalIdRegion(mapCache, ((Redisson)redisson).getConnectionManager(),this, metadata, settings, properties, NATURAL_ID_DEF, cacheKeysFactory);
}