Java Code Examples for org.redisson.api.RLocalCachedMap#getCachedMap()

The following examples show how to use org.redisson.api.RLocalCachedMap#getCachedMap() . 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: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadAllValues() {
    RLocalCachedMap<SimpleKey, SimpleValue> map = redisson.getLocalCachedMap("simple", LocalCachedMapOptions.defaults());
    Map<SimpleKey, SimpleValue> cache = map.getCachedMap();
    
    map.put(new SimpleKey("1"), new SimpleValue("2"));
    map.put(new SimpleKey("33"), new SimpleValue("44"));
    map.put(new SimpleKey("5"), new SimpleValue("6"));
    assertThat(cache.size()).isEqualTo(3);

    assertThat(map.readAllValues().size()).isEqualTo(3);
    Map<SimpleKey, SimpleValue> testMap = new HashMap<>(map);
    assertThat(map.readAllValues()).containsOnlyElementsOf(testMap.values());
    
    RMap<SimpleKey, SimpleValue> map2 = redisson.getLocalCachedMap("simple", LocalCachedMapOptions.defaults());
    assertThat(map2.readAllValues()).containsOnlyElementsOf(testMap.values());
}
 
Example 2
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testReplaceOldValueSuccess() {
    RLocalCachedMap<SimpleKey, SimpleValue> map = redisson.getLocalCachedMap("simple", LocalCachedMapOptions.defaults());
    Map<SimpleKey, SimpleValue> cache = map.getCachedMap();
    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());
    assertThat(cache.size()).isEqualTo(1);
}
 
Example 3
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testLFU() {
    RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("test", LocalCachedMapOptions.<String, Integer>defaults().evictionPolicy(EvictionPolicy.LFU).cacheSize(5));
    Map<String, Integer> cache = map.getCachedMap();

    map.put("12", 1);
    map.put("14", 2);
    map.put("15", 3);
    map.put("16", 4);
    map.put("17", 5);
    map.put("18", 6);
    
    assertThat(cache.size()).isEqualTo(5);
    assertThat(map.size()).isEqualTo(6);
    assertThat(map.keySet()).containsOnly("12", "14", "15", "16", "17", "18");
    assertThat(map.values()).containsOnly(1, 2, 3, 4, 5, 6);
}
 
Example 4
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testLRU() {
    RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("test", LocalCachedMapOptions.<String, Integer>defaults().evictionPolicy(EvictionPolicy.LRU).cacheSize(5));
    Map<String, Integer> cache = map.getCachedMap();

    map.put("12", 1);
    map.put("14", 2);
    map.put("15", 3);
    map.put("16", 4);
    map.put("17", 5);
    map.put("18", 6);
    
    assertThat(cache.size()).isEqualTo(5);
    assertThat(map.size()).isEqualTo(6);
    assertThat(map.keySet()).containsOnly("12", "14", "15", "16", "17", "18");
    assertThat(map.values()).containsOnly(1, 2, 3, 4, 5, 6);
}
 
Example 5
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutGetCache() {
    RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
    Map<String, Integer> cache = map.getCachedMap();

    map.put("12", 1);
    map.put("14", 2);
    map.put("15", 3);

    assertThat(cache).containsValues(1, 2, 3);
    assertThat(map.get("12")).isEqualTo(1);
    assertThat(map.get("14")).isEqualTo(2);
    assertThat(map.get("15")).isEqualTo(3);
    
    RLocalCachedMap<String, Integer> map1 = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
    
    assertThat(map1.get("12")).isEqualTo(1);
    assertThat(map1.get("14")).isEqualTo(2);
    assertThat(map1.get("15")).isEqualTo(3);
}
 
Example 6
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAllCache() {
    RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("getAll", LocalCachedMapOptions.defaults());
    Map<String, Integer> cache = map.getCachedMap();
    map.put("1", 100);
    map.put("2", 200);
    map.put("3", 300);
    map.put("4", 400);

    assertThat(cache.size()).isEqualTo(4);
    Map<String, Integer> filtered = map.getAll(new HashSet<String>(Arrays.asList("2", "3", "5")));

    Map<String, Integer> expectedMap = new HashMap<String, Integer>();
    expectedMap.put("2", 200);
    expectedMap.put("3", 300);
    assertThat(filtered).isEqualTo(expectedMap);
    
    RMap<String, Integer> map1 = redisson.getLocalCachedMap("getAll", LocalCachedMapOptions.defaults());
    
    Map<String, Integer> filtered1 = map1.getAll(new HashSet<String>(Arrays.asList("2", "3", "5")));

    assertThat(filtered1).isEqualTo(expectedMap);
}
 
Example 7
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveValueFail() {
    RLocalCachedMap<SimpleKey, SimpleValue> map = redisson.getLocalCachedMap("simple12", LocalCachedMapOptions.defaults());
    Map<SimpleKey, SimpleValue> cache = map.getCachedMap();
    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());
    assertThat(cache.size()).isEqualTo(1);
}
 
Example 8
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidationOnUpdateNonBinaryCodec() throws InterruptedException {
    LocalCachedMapOptions<String, String> options = LocalCachedMapOptions.<String, String>defaults().evictionPolicy(EvictionPolicy.LFU).cacheSize(5);
    RLocalCachedMap<String, String> map1 = redisson.getLocalCachedMap("test", new StringCodec(), options);
    Map<String, String> cache1 = map1.getCachedMap();
    
    RLocalCachedMap<String, String> map2 = redisson.getLocalCachedMap("test", new StringCodec(), options);
    Map<String, String> cache2 = map2.getCachedMap();
    
    map1.put("1", "1");
    map1.put("2", "2");
    
    assertThat(map2.get("1")).isEqualTo("1");
    assertThat(map2.get("2")).isEqualTo("2");
    
    assertThat(cache1.size()).isEqualTo(2);
    assertThat(cache2.size()).isEqualTo(2);

    map1.put("1", "3");
    map2.put("2", "4");
    Thread.sleep(50);
    
    assertThat(cache1.size()).isEqualTo(1);
    assertThat(cache2.size()).isEqualTo(1);
}
 
Example 9
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testFastPutIfAbsent() throws Exception {
    RLocalCachedMap<SimpleKey, SimpleValue> map = redisson.getLocalCachedMap("simple", LocalCachedMapOptions.defaults());
    Map<SimpleKey, SimpleValue> cache = map.getCachedMap();
    
    SimpleKey key = new SimpleKey("1");
    SimpleValue value = new SimpleValue("2");
    map.put(key, value);
    assertThat(map.fastPutIfAbsent(key, new SimpleValue("3"))).isFalse();
    assertThat(cache.size()).isEqualTo(1);
    assertThat(map.get(key)).isEqualTo(value);

    SimpleKey key1 = new SimpleKey("2");
    SimpleValue value1 = new SimpleValue("4");
    assertThat(map.fastPutIfAbsent(key1, value1)).isTrue();
    assertThat(cache.size()).isEqualTo(2);
    assertThat(map.get(key1)).isEqualTo(value1);
}
 
Example 10
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadAllEntrySet() {
    RLocalCachedMap<SimpleKey, SimpleValue> map = redisson.getLocalCachedMap("simple12", LocalCachedMapOptions.defaults());
    Map<SimpleKey, SimpleValue> cache = map.getCachedMap();
    map.put(new SimpleKey("1"), new SimpleValue("2"));
    map.put(new SimpleKey("33"), new SimpleValue("44"));
    map.put(new SimpleKey("5"), new SimpleValue("6"));

    assertThat(map.readAllEntrySet().size()).isEqualTo(3);
    assertThat(cache.size()).isEqualTo(3);
    Map<SimpleKey, SimpleValue> testMap = new HashMap<>(map);
    assertThat(map.readAllEntrySet()).containsOnlyElementsOf(testMap.entrySet());
    
    RMap<SimpleKey, SimpleValue> map2 = redisson.getLocalCachedMap("simple12", LocalCachedMapOptions.defaults());
    assertThat(map2.readAllEntrySet()).containsOnlyElementsOf(testMap.entrySet());
}
 
Example 11
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutIfAbsent() throws Exception {
    RLocalCachedMap<SimpleKey, SimpleValue> map = redisson.getLocalCachedMap("simple12", LocalCachedMapOptions.defaults());
    Map<SimpleKey, SimpleValue> cache = map.getCachedMap();

    SimpleKey key = new SimpleKey("1");
    SimpleValue value = new SimpleValue("2");
    map.put(key, value);
    Assert.assertEquals(value, map.putIfAbsent(key, new SimpleValue("3")));
    Assert.assertEquals(value, map.get(key));

    SimpleKey key1 = new SimpleKey("2");
    SimpleValue value1 = new SimpleValue("4");
    Assert.assertNull(map.putIfAbsent(key1, value1));
    Assert.assertEquals(value1, map.get(key1));
    assertThat(cache.size()).isEqualTo(2);
}
 
Example 12
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemove() {
    RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
    Map<String, Integer> cache = map.getCachedMap();
    map.put("12", 1);

    assertThat(cache.size()).isEqualTo(1);
    
    assertThat(map.remove("12")).isEqualTo(1);
    
    assertThat(cache.size()).isEqualTo(0);
    
    assertThat(map.remove("14")).isNull();
}
 
Example 13
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceValue() {
    RLocalCachedMap<SimpleKey, SimpleValue> map = redisson.getLocalCachedMap("simple", LocalCachedMapOptions.defaults());
    Map<SimpleKey, SimpleValue> cache = map.getCachedMap();
    map.put(new SimpleKey("1"), new SimpleValue("2"));

    SimpleValue res = map.replace(new SimpleKey("1"), new SimpleValue("3"));
    Assert.assertEquals("2", res.getValue());
    assertThat(cache.size()).isEqualTo(1);

    SimpleValue val1 = map.get(new SimpleKey("1"));
    Assert.assertEquals("3", val1.getValue());
}
 
Example 14
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceOldValueFail() {
    RLocalCachedMap<SimpleKey, SimpleValue> map = redisson.getLocalCachedMap("simple", LocalCachedMapOptions.defaults());
    Map<SimpleKey, SimpleValue> cache = map.getCachedMap();
    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());
    assertThat(cache.size()).isEqualTo(1);
}
 
Example 15
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveValue() {
    RLocalCachedMap<SimpleKey, SimpleValue> map = redisson.getLocalCachedMap("simple12", LocalCachedMapOptions.defaults());
    Map<SimpleKey, SimpleValue> cache = map.getCachedMap();
    map.put(new SimpleKey("1"), new SimpleValue("2"));

    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());
    assertThat(cache.size()).isEqualTo(0);
}
 
Example 16
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutAllCache() throws InterruptedException {
    RLocalCachedMap<Integer, String> map = redisson.getLocalCachedMap("simple", LocalCachedMapOptions.defaults());
    RLocalCachedMap<Integer, String> map1 = redisson.getLocalCachedMap("simple", LocalCachedMapOptions.defaults());
    Map<Integer, String> cache = map.getCachedMap();
    Map<Integer, String> cache1 = map1.getCachedMap();
    map.put(1, "1");
    map.put(2, "2");
    map.put(3, "3");

    Map<Integer, String> joinMap = new HashMap<Integer, String>();
    joinMap.put(4, "4");
    joinMap.put(5, "5");
    joinMap.put(6, "6");
    map.putAll(joinMap);

    assertThat(cache.size()).isEqualTo(6);
    assertThat(cache1.size()).isEqualTo(0);
    assertThat(map.keySet()).containsOnly(1, 2, 3, 4, 5, 6);
    
    map1.putAll(joinMap);
    
    // waiting for cache cleanup listeners triggering 
    Thread.sleep(500);
    
    assertThat(cache.size()).isEqualTo(3);
    assertThat(cache1.size()).isEqualTo(3);
}
 
Example 17
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testSizeCache() {
    RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
    Map<String, Integer> cache = map.getCachedMap();

    map.put("12", 1);
    map.put("14", 2);
    map.put("15", 3);
    
    assertThat(cache.size()).isEqualTo(3);
    assertThat(map.size()).isEqualTo(3);
}
 
Example 18
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoInvalidationOnRemove() throws InterruptedException {
    LocalCachedMapOptions<String, Integer> options = LocalCachedMapOptions.<String, Integer>defaults()
            .evictionPolicy(EvictionPolicy.LFU)
            .cacheSize(5)
            .syncStrategy(SyncStrategy.NONE);
    
    RLocalCachedMap<String, Integer> map1 = redisson.getLocalCachedMap("test", options);
    Map<String, Integer> cache1 = map1.getCachedMap();
    
    RLocalCachedMap<String, Integer> map2 = redisson.getLocalCachedMap("test", options);
    Map<String, Integer> cache2 = map2.getCachedMap();
    
    map1.put("1", 1);
    map1.put("2", 2);
    
    assertThat(map2.get("1")).isEqualTo(1);
    assertThat(map2.get("2")).isEqualTo(2);
    
    assertThat(cache1.size()).isEqualTo(2);
    assertThat(cache2.size()).isEqualTo(2);

    map1.remove("1");
    map2.remove("2");
    Thread.sleep(50);
    
    assertThat(cache1.size()).isEqualTo(1);
    assertThat(cache2.size()).isEqualTo(1);
}
 
Example 19
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testLocalCacheClear() throws InterruptedException {
    LocalCachedMapOptions<String, Integer> options = LocalCachedMapOptions.<String, Integer>defaults()
            .evictionPolicy(EvictionPolicy.LFU)
            .cacheSize(5)
            .syncStrategy(SyncStrategy.INVALIDATE);
    
    RLocalCachedMap<String, Integer> map1 = redisson.getLocalCachedMap("test", options);
    Map<String, Integer> cache1 = map1.getCachedMap();
    
    RLocalCachedMap<String, Integer> map2 = redisson.getLocalCachedMap("test", options);
    Map<String, Integer> cache2 = map2.getCachedMap();
    
    map1.put("1", 1);
    map1.put("2", 2);
    
    assertThat(map2.get("1")).isEqualTo(1);
    assertThat(map2.get("2")).isEqualTo(2);
    
    assertThat(cache1.size()).isEqualTo(2);
    assertThat(cache2.size()).isEqualTo(2);

    
    map1.clearLocalCache();
    
    assertThat(redisson.getKeys().count()).isEqualTo(1);

    assertThat(cache1.size()).isZero();
    assertThat(cache2.size()).isZero();
}
 
Example 20
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoInvalidationOnUpdate() throws InterruptedException {
    LocalCachedMapOptions<String, Integer> options = LocalCachedMapOptions.<String, Integer>defaults()
            .evictionPolicy(EvictionPolicy.LFU)
            .cacheSize(5)
            .syncStrategy(SyncStrategy.NONE);
    
    RLocalCachedMap<String, Integer> map1 = redisson.getLocalCachedMap("test", options);
    Map<String, Integer> cache1 = map1.getCachedMap();
    
    RLocalCachedMap<String, Integer> map2 = redisson.getLocalCachedMap("test", options);
    Map<String, Integer> cache2 = map2.getCachedMap();
    
    map1.put("1", 1);
    map1.put("2", 2);
    
    assertThat(map2.get("1")).isEqualTo(1);
    assertThat(map2.get("2")).isEqualTo(2);
    
    assertThat(cache1.size()).isEqualTo(2);
    assertThat(cache2.size()).isEqualTo(2);

    map1.put("1", 3);
    map2.put("2", 4);
    Thread.sleep(50);

    assertThat(cache1.size()).isEqualTo(2);
    assertThat(cache2.size()).isEqualTo(2);
}