Java Code Examples for org.redisson.api.RMap#addAndGet()

The following examples show how to use org.redisson.api.RMap#addAndGet() . 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: BaseMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddAndGet() throws InterruptedException {
    RMap<Integer, Integer> map = getMap("getAll", new CompositeCodec(redisson.getConfig().getCodec(), IntegerCodec.INSTANCE));
    map.put(1, 100);

    Integer res = map.addAndGet(1, 12);
    assertThat(res).isEqualTo(112);
    res = map.get(1);
    assertThat(res).isEqualTo(112);

    RMap<Integer, Double> map2 = getMap("getAll2", new CompositeCodec(redisson.getConfig().getCodec(), DoubleCodec.INSTANCE));
    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 = getMap("mapStr", new CompositeCodec(redisson.getConfig().getCodec(), IntegerCodec.INSTANCE));
    assertThat(mapStr.put("1", 100)).isNull();

    assertThat(mapStr.addAndGet("1", 12)).isEqualTo(112);
    assertThat(mapStr.get("1")).isEqualTo(112);
    destroy(map);
}
 
Example 2
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 3
Source File: MapExamples.java    From redisson-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();
    
    RMap<String, Integer> map =  redisson.getMap("myMap");
    map.put("a", 1);
    map.put("b", 2);
    map.put("c", 3);
    
    boolean contains = map.containsKey("a");
    
    Integer value = map.get("c");
    Integer updatedValue = map.addAndGet("a", 32);
    
    Integer valueSize = map.valueSize("c");
    
    Set<String> keys = new HashSet<String>();
    keys.add("a");
    keys.add("b");
    keys.add("c");
    Map<String, Integer> mapSlice = map.getAll(keys);
    
    // use read* methods to fetch all objects
    Set<String> allKeys = map.readAllKeySet();
    Collection<Integer> allValues = map.readAllValues();
    Set<Entry<String, Integer>> allEntries = map.readAllEntrySet();
    
    // use fast* methods when previous value is not required
    boolean isNewKey = map.fastPut("a", 100);
    boolean isNewKeyPut = map.fastPutIfAbsent("d", 33);
    long removedAmount = map.fastRemove("b");
    
    redisson.shutdown();
}
 
Example 4
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);
}