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

The following examples show how to use org.redisson.api.RMap#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: 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: RedisJobRepositoryImpl.java    From earth-frost with Apache License 2.0 6 votes vote down vote up
@Override
public void updateJob(JobInfo jobInfo) {
  RMap<String, JobInfo> map = redissonClient.getMap(Container.JOB_INFO);
  // script
  RListMultimap<String, JobScript> scriptList = redissonClient
      .getListMultimap(Container.JOB_INFO_SCRIPT);
  if (jobInfo.getScript() != null && JobInfo.TYPE.SCRIPT.name().equals(jobInfo.getType())) {
    JobInfo local = map.get(jobInfo.getId());
    if (!Objects.equals(jobInfo.getType(), local.getType())) {
      JobScript script = build(jobInfo);
      scriptList.removeAll(jobInfo.getId());
      scriptList.put(jobInfo.getId(), script);
    }
  } else if (JobInfo.TYPE.BEAN.name().equals(jobInfo.getType())) {
    scriptList.removeAll(jobInfo.getId());
  }
  jobInfo.setScript(null);
  map.put(jobInfo.getId(), jobInfo);
}
 
Example 3
Source File: RedisJobRepositoryImpl.java    From earth-frost with Apache License 2.0 6 votes vote down vote up
@Override
public JobInfo findJobInfoById(String id) {
  RMap<String, JobInfo> map = redissonClient.getMap(Container.JOB_INFO);
  JobInfo jobInfo = map.get(id);
  if (jobInfo == null) {
    return null;
  }
  RListMultimap<String, JobScript> scriptList = redissonClient
      .getListMultimap(Container.JOB_INFO_SCRIPT);
  RList<JobScript> list = scriptList.get(id);
  int size = list.size();
  if (size > 0) {
    JobScript script = list.get(size - 1);
    jobInfo.setScript(script.getScript());
  }
  return jobInfo;
}
 
Example 4
Source File: TimeoutTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
public void testReplaceTimeout() throws InterruptedException, ExecutionException {
    RMap<Integer, Integer> map = redisson.getMap("simple");
    for (int i = 0; i < 1000; i++) {
        map.put(i, i * 1000);
        map.replace(i, i * 1000 + 1);
        Thread.sleep(1000);
        System.out.println(i);
    }

    for (int i = 0; i < 1000; i++) {
        Integer r = map.get(i);
        System.out.println(r);
    }
}
 
Example 5
Source File: BaseMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceValue() {
    RMap<SimpleKey, SimpleValue> map = getMap("simple");
    map.put(new SimpleKey("1"), new SimpleValue("2"));

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

    SimpleValue val1 = map.get(new SimpleKey("1"));
    Assert.assertEquals("3", val1.getValue());
    destroy(map);
}
 
Example 6
Source File: BaseMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplace() {
    RMap<SimpleKey, SimpleValue> map = getMap("simple");
    map.put(new SimpleKey("1"), new SimpleValue("2"));
    map.put(new SimpleKey("33"), new SimpleValue("44"));
    map.put(new SimpleKey("5"), new SimpleValue("6"));

    SimpleValue val1 = map.get(new SimpleKey("33"));
    Assert.assertEquals("44", val1.getValue());

    map.put(new SimpleKey("33"), new SimpleValue("abc"));
    SimpleValue val2 = map.get(new SimpleKey("33"));
    Assert.assertEquals("abc", val2.getValue());
    destroy(map);
}
 
Example 7
Source File: BaseMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceOldValueFail() {
    RMap<SimpleKey, SimpleValue> map = getMap("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());
    destroy(map);
}
 
Example 8
Source File: BaseMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutGet() {
    RMap<SimpleKey, SimpleValue> map = getMap("simple");
    map.put(new SimpleKey("1"), new SimpleValue("2"));
    map.put(new SimpleKey("33"), new SimpleValue("44"));
    map.put(new SimpleKey("5"), new SimpleValue("6"));

    SimpleValue val1 = map.get(new SimpleKey("33"));
    Assert.assertEquals("44", val1.getValue());

    SimpleValue val2 = map.get(new SimpleKey("5"));
    Assert.assertEquals("6", val2.getValue());
    destroy(map);
}
 
Example 9
Source File: BaseMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testLong() {
    RMap<Long, Long> map = getMap("test_long");
    map.put(1L, 2L);
    map.put(3L, 4L);

    assertThat(map.size()).isEqualTo(2);

    Long val = map.get(1L);
    assertThat(val).isEqualTo(2);

    Long val2 = map.get(3L);
    assertThat(val2).isEqualTo(4);
    destroy(map);
}
 
Example 10
Source File: BaseMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testInteger() {
    RMap<Integer, Integer> map = getMap("test_int");
    map.put(1, 2);
    map.put(3, 4);

    assertThat(map.size()).isEqualTo(2);

    Integer val = map.get(1);
    assertThat(val).isEqualTo(2);

    Integer val2 = map.get(3);
    assertThat(val2).isEqualTo(4);
    destroy(map);
}
 
Example 11
Source File: BaseMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveValueFail() {
    RMap<SimpleKey, SimpleValue> map = getMap("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());
    destroy(map);
}
 
Example 12
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 13
Source File: RedisJobRepositoryImpl.java    From earth-frost with Apache License 2.0 5 votes vote down vote up
@Override
public JobExecuteRecord findJobExecuteRecordById(String id) {
  RMap<String, JobExecuteRecord> map = redissonClient.getMap(Container.RECORD);
  JobExecuteRecord record = map.get(id);
  if (record == null) {
    return null;
  }
  RListMultimap<String, JobRecordStatus> recordStatus = redissonClient
      .getListMultimap(Container.RECORD_STATUS);
  List<JobRecordStatus> statuses = recordStatus.getAll(id);
  statuses.forEach(r -> r.fill(record));
  record.setRecordStatuses(statuses);
  return record;
}
 
Example 14
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 15
Source File: BaseMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveValue() {
    RMap<SimpleKey, SimpleValue> map = getMap("simple");
    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());
    destroy(map);
}
 
Example 16
Source File: MultiCache.java    From mPaaS with Apache License 2.0 4 votes vote down vote up
/** 读(Hash) */
@SuppressWarnings("unchecked")
public <T> T hget(String key, String field) {
    Object map = null;
    Object value = null;
    /**
     * CACHE:key==NULL 表示key不存在<br>
     * CACHE:key==MAP 表示key存在
     */
    // 从线程中读
    Map<String, Object> cache = getThreadLocal();
    if (cache != null) {
        map = cache.get(key);
        if (map != null) {
            if (map == NULL) {
                // key不存在
                return null;
            }
            value = ((Map<String, Object>) map).get(field);
            if (value != null) {
                return value == NULL ? null : (T) value;
            }
        }
    }
    // 从redis读并回写线程
    RMap<String, Object> rmap = redisson.getMap(key,
            JsonJacksonCodec.INSTANCE);
    boolean exists = map != null || rmap.isExists();
    if (exists) {
        value = rmap.get(field);
    }
    if (cache != null) {
        if (exists) {
            if (map == null) {
                map = new HashMap<>(16);
                cache.put(key, map);
            }
            ((Map<String, Object>) map).put(field,
                    value == null ? NULL : value);
        } else {
            cache.put(key, NULL);
        }
    }
    return (T) value;
}
 
Example 17
Source File: ExecutorServiceExamples.java    From redisson-examples with Apache License 2.0 4 votes vote down vote up
@Override
public String call() throws Exception {
    RMap<String, String> map = redisson.getMap("myMap");
    map.put("1", "2");
    return map.get("3");
}
 
Example 18
Source File: SchedulerServiceExamples.java    From redisson-examples with Apache License 2.0 4 votes vote down vote up
@Override
public String call() throws Exception {
    RMap<String, String> map = redisson.getMap("myMap");
    map.put("1", "2");
    return map.get("3");
}
 
Example 19
Source File: RedisJobLoggerImpl.java    From earth-frost with Apache License 2.0 4 votes vote down vote up
private void removeOldestLogger(String loggerId) {
  long maxLogSize = Container.get().getJobExecutorProperties().getMaxLogSize();
  if (maxLogSize < 0) {
    return;
  }

  RMap<String, String> logMap = redissonClient.getMap(Container.LOG_BIND);
  String jobId = logMap.get(loggerId);
  if (jobId == null) {
    return;
  }

  RListMultimap<String, String> sortmap = redissonClient.getListMultimap(Container.RECORD_SORT);
  RList<String> list = sortmap.get(jobId);
  long size = list.size() - maxLogSize;
  if (size <= 0) {
    return;
  }

  JobInfo job = jobRepository.findJobInfoById(jobId);
  if (job == null) {
    return;
  }

  RListMultimap<String, JobRecordStatus> statusMultimap = redissonClient
      .getListMultimap(Container.RECORD_STATUS);
  RMap<String, JobExecuteRecord> map = redissonClient.getMap(Container.RECORD);
  RList<String> logIds = redissonClient.<String, String>getListMultimap(Container.LOG_REL)
      .get(jobId);

  JobGroup group = job.getGroup();
  for (String key : list.subList(0, (int) size).readAll()) {
    sortmap.get(Strings.EMPTY).remove(key);
    if (group != null) {
      sortmap.get(String.join(Strings.COLON, group.getGroupKey(), group.getJobKey())).remove(key);
      sortmap.get(group.getGroupKey()).remove(key);
    }
    statusMultimap.removeAll(key);
    list.remove(key);
    map.remove(key);
    logMap.remove(key);
    logIds.remove(key);
    redissonClient.getKeys().delete(String.format(Container.EVENT_SHARDING, jobId, loggerId));
  }

}
 
Example 20
Source File: MultiCache.java    From mPass with Apache License 2.0 4 votes vote down vote up
/** 读(Hash) */
@SuppressWarnings("unchecked")
public <T> T hget(String key, String field) {
    Object map = null;
    Object value = null;
    /**
     * CACHE:key==NULL 表示key不存在<br>
     * CACHE:key==MAP 表示key存在
     */
    // 从线程中读
    Map<String, Object> cache = getThreadLocal();
    if (cache != null) {
        map = cache.get(key);
        if (map != null) {
            if (map == NULL) {
                // key不存在
                return null;
            }
            value = ((Map<String, Object>) map).get(field);
            if (value != null) {
                return value == NULL ? null : (T) value;
            }
        }
    }
    // 从redis读并回写线程
    RMap<String, Object> rmap = redisson.getMap(key,
            JsonJacksonCodec.INSTANCE);
    boolean exists = map != null || rmap.isExists();
    if (exists) {
        value = rmap.get(field);
    }
    if (cache != null) {
        if (exists) {
            if (map == null) {
                map = new HashMap<>(16);
                cache.put(key, map);
            }
            ((Map<String, Object>) map).put(field,
                    value == null ? NULL : value);
        } else {
            cache.put(key, NULL);
        }
    }
    return (T) value;
}