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

The following examples show how to use org.redisson.api.RLocalCachedMap#get() . 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: RedissonTransactionalLocalCachedMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testPut() throws InterruptedException {
    RLocalCachedMap<String, String> m1 = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
    m1.put("1", "2");
    m1.put("3", "4");
    
    RLocalCachedMap<String, String> m2 = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
    m2.get("1");
    m2.get("3");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = transaction.getLocalCachedMap(m1);
    assertThat(map.put("3", "5")).isEqualTo("4");
    assertThat(map.get("3")).isEqualTo("5");
    
    assertThat(m1.get("3")).isEqualTo("4");
    assertThat(m2.get("3")).isEqualTo("4");
    
    transaction.commit();
    
    assertThat(m1.get("3")).isEqualTo("5");
    assertThat(m2.get("3")).isEqualTo("5");
}
 
Example 2
Source File: RedissonTransactionalLocalCachedMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRollback() {
    RLocalCachedMap<String, String> m1 = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
    m1.put("1", "2");
    m1.put("3", "4");
    
    RLocalCachedMap<String, String> m2 = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
    m2.get("1");
    m2.get("3");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = transaction.getLocalCachedMap(m1);
    assertThat(map.get("1")).isEqualTo("2");
    assertThat(map.remove("3")).isEqualTo("4");
    
    assertThat(m1.get("3")).isEqualTo("4");
    
    transaction.rollback();
    
    assertThat(redisson.getKeys().count()).isEqualTo(1);
    
    assertThat(m1.get("1")).isEqualTo("2");
    assertThat(m1.get("3")).isEqualTo("4");
    assertThat(m2.get("1")).isEqualTo("2");
    assertThat(m2.get("3")).isEqualTo("4");
}
 
Example 3
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 4
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 5
Source File: LocalCachedMapExamples.java    From redisson-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();
    
    LocalCachedMapOptions options = LocalCachedMapOptions.defaults()
            .cacheSize(10000)
            .evictionPolicy(EvictionPolicy.LRU)
            .maxIdle(10, TimeUnit.SECONDS)
            .timeToLive(60, TimeUnit.SECONDS);
            
    RLocalCachedMap<String, Integer> cachedMap = redisson.getLocalCachedMap("myMap", options);
    cachedMap.put("a", 1);
    cachedMap.put("b", 2);
    cachedMap.put("c", 3);
    
    boolean contains = cachedMap.containsKey("a");
    
    Integer value = cachedMap.get("c");
    Integer updatedValue = cachedMap.addAndGet("a", 32);
    
    Integer valueSize = cachedMap.valueSize("c");
    
    Set<String> keys = new HashSet<String>();
    keys.add("a");
    keys.add("b");
    keys.add("c");
    Map<String, Integer> mapSlice = cachedMap.getAll(keys);
    
    // use read* methods to fetch all objects
    Set<String> allKeys = cachedMap.readAllKeySet();
    Collection<Integer> allValues = cachedMap.readAllValues();
    Set<Entry<String, Integer>> allEntries = cachedMap.readAllEntrySet();
    
    // use fast* methods when previous value is not required
    boolean isNewKey = cachedMap.fastPut("a", 100);
    boolean isNewKeyPut = cachedMap.fastPutIfAbsent("d", 33);
    long removedAmount = cachedMap.fastRemove("b");
    
    redisson.shutdown();
}
 
Example 6
Source File: RedissonTransactionalLocalCachedMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutRemove() {
    RLocalCachedMap<String, String> m1 = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
    m1.put("1", "2");
    m1.put("3", "4");
    
    RLocalCachedMap<String, String> m2 = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
    m2.get("1");
    m2.get("3");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = transaction.getLocalCachedMap(m1);
    assertThat(map.get("1")).isEqualTo("2");
    assertThat(map.remove("3")).isEqualTo("4");
    assertThat(map.put("3", "5")).isNull();
    assertThat(map.get("3")).isEqualTo("5");
    
    assertThat(m1.get("3")).isEqualTo("4");
    assertThat(m2.get("3")).isEqualTo("4");
    
    transaction.commit();
    
    assertThat(m1.get("1")).isEqualTo("2");
    assertThat(m1.get("3")).isEqualTo("5");
    assertThat(m2.get("1")).isEqualTo("2");
    assertThat(m2.get("3")).isEqualTo("5");
}
 
Example 7
Source File: RedissonLocalCachedMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 8
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 9
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 10
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());
}