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

The following examples show how to use org.redisson.api.RMapCache#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: RedissonMapCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutGetTTL() throws InterruptedException {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple04");
    Assert.assertNull(map.get(new SimpleKey("33")));

    map.put(new SimpleKey("33"), new SimpleValue("44"), 2, TimeUnit.SECONDS);

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

    Thread.sleep(1000);

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

    Thread.sleep(1000);

    Assert.assertNull(map.get(new SimpleKey("33")));
    map.destroy();
}
 
Example 2
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 3
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceOldValueFail() {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("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());
    map.destroy();
}
 
Example 4
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceOldValueSuccess() {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
    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());
    map.destroy();
}
 
Example 5
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceValueTTL() throws InterruptedException {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
    map.put(new SimpleKey("1"), new SimpleValue("2"), 1, TimeUnit.SECONDS);

    Thread.sleep(1000);
    
    SimpleValue res = map.replace(new SimpleKey("1"), new SimpleValue("3"));
    assertThat(res).isNull();

    SimpleValue val1 = map.get(new SimpleKey("1"));
    assertThat(val1).isNull();
    map.destroy();
}
 
Example 6
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
  public void testReplaceValueTTLIdleUpdate() throws InterruptedException {
      RMapCache<SimpleKey, SimpleValue> map = null;
SimpleValue val1;
try {
	map = redisson.getMapCache("simple");
	map.put(new SimpleKey("1"), new SimpleValue("2"), 2, TimeUnit.SECONDS, 1, TimeUnit.SECONDS);

	Thread.sleep(750);

	// update value, would like idle timeout to be refreshed
	SimpleValue res = map.replace(new SimpleKey("1"), new SimpleValue("3"));
	assertThat(res).isNotNull();

	Thread.sleep(750);

	// if idle timeout has been updated val1 will be not be null, else it will be null
	val1 = map.get(new SimpleKey("1"));
	assertThat(val1).isNotNull(); 

	Thread.sleep(750);
	
	// val1 will have expired due to TTL
	val1 = map.get(new SimpleKey("1"));
	assertThat(val1).isNull();

} catch (Exception e) {
	e.printStackTrace();
} finally {
       map.remove(new SimpleKey("1"));
}
  }
 
Example 7
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutAllGetTTL() throws InterruptedException {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple06");
    Assert.assertNull(map.get(new SimpleKey("33")));
    Assert.assertNull(map.get(new SimpleKey("55")));

    Map<SimpleKey, SimpleValue> entries = new HashMap<>();
    entries.put(new SimpleKey("33"), new SimpleValue("44"));
    entries.put(new SimpleKey("55"), new SimpleValue("66"));
    map.putAll(entries, 2, TimeUnit.SECONDS);

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

    Thread.sleep(1000);

    Assert.assertEquals(2, map.size());
    SimpleValue val3 = map.get(new SimpleKey("33"));
    Assert.assertEquals("44", val3.getValue());
    SimpleValue val4 = map.get(new SimpleKey("55"));
    Assert.assertEquals("66", val4.getValue());
    Assert.assertEquals(2, map.size());

    Thread.sleep(1000);

    Assert.assertNull(map.get(new SimpleKey("33")));
    Assert.assertNull(map.get(new SimpleKey("55")));
    map.destroy();
}
 
Example 8
Source File: FileUtils.java    From kkFileViewOfficeEdit with Apache License 2.0 4 votes vote down vote up
/**
 * 已转换过的文件,根据文件名获取
 * @return
 */
public String getConvertedFile(String key) {
    RMapCache<String, String> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_KEY);
    return convertedList.get(key);
}
 
Example 9
Source File: CacheServiceRedisImpl.java    From kkFileView with Apache License 2.0 4 votes vote down vote up
@Override
public String getPDFCache(String key) {
    RMapCache<String, String> convertedList = redissonClient.getMapCache(FILE_PREVIEW_PDF_KEY);
    return convertedList.get(key);
}
 
Example 10
Source File: CacheServiceRedisImpl.java    From kkFileView with Apache License 2.0 4 votes vote down vote up
@Override
public List<String> getImgCache(String key) {
    RMapCache<String, List<String>> convertedList = redissonClient.getMapCache(FILE_PREVIEW_IMGS_KEY);
    return convertedList.get(key);
}
 
Example 11
Source File: CacheServiceRedisImpl.java    From kkFileView with Apache License 2.0 4 votes vote down vote up
@Override
public Integer getPdfImageCache(String key) {
    RMapCache<String, Integer> convertedList = redissonClient.getMapCache(FILE_PREVIEW_PDF_IMGS_KEY);
    return convertedList.get(key);
}
 
Example 12
Source File: MapCacheExamples.java    From redisson-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();
    
    RMapCache<String, Integer> mapCache = redisson.getMapCache("test");
    
    // with ttl = 10 seconds
    Integer prevValue = mapCache.put("1", 10, 10, TimeUnit.SECONDS);
    // with ttl = 15 seconds and maxIdleTime = 5 seconds
    Integer prevValue2 = mapCache.put("2", 20, 15, TimeUnit.SECONDS, 5, TimeUnit.SECONDS);
    // store value permanently 
    Integer prevValue3 = mapCache.put("3", 30);
    
    // with ttl = 30 seconds
    Integer currValue = mapCache.putIfAbsent("4", 40, 30, TimeUnit.SECONDS);
    // with ttl = 40 seconds and maxIdleTime = 10 seconds
    Integer currValue2 = mapCache.putIfAbsent("5", 50, 40, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);
    // try to add new key-value permanently 
    Integer currValue3 = mapCache.putIfAbsent("6", 60);
    
    // use fast* methods when previous value is not required

    // with ttl = 20 seconds
    boolean isNewKey1 = mapCache.fastPut("7", 70, 20, TimeUnit.SECONDS);
    // with ttl = 40 seconds and maxIdleTime = 20 seconds
    boolean isNewKey2 = mapCache.fastPut("8", 80, 40, TimeUnit.SECONDS, 20, TimeUnit.SECONDS);
    // store value permanently 
    boolean isNewKey3 = mapCache.fastPut("9", 90);
    
    // try to add new key-value permanently
    boolean isNewKeyPut = mapCache.fastPutIfAbsent("10", 100);

    boolean contains = mapCache.containsKey("a");
    
    Integer value = mapCache.get("c");
    Integer updatedValue = mapCache.addAndGet("a", 32);
    
    Integer valueSize = mapCache.valueSize("c");
    
    Set<String> keys = new HashSet<String>();
    keys.add("a");
    keys.add("b");
    keys.add("c");
    Map<String, Integer> mapSlice = mapCache.getAll(keys);
    
    // use read* methods to fetch all objects
    Set<String> allKeys = mapCache.readAllKeySet();
    Collection<Integer> allValues = mapCache.readAllValues();
    Set<Entry<String, Integer>> allEntries = mapCache.readAllEntrySet();
    
    redisson.shutdown();
}
 
Example 13
Source File: RedissonMapCacheTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testRemoveValueTTL() throws InterruptedException {
    RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
    map.put(new SimpleKey("1"), new SimpleValue("2"), 1, TimeUnit.SECONDS);

    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());
    
    map.put(new SimpleKey("3"), new SimpleValue("4"), 1, TimeUnit.SECONDS);

    Thread.sleep(1000);
    
    assertThat(map.remove(new SimpleKey("3"), new SimpleValue("4"))).isFalse();

    assertThat(map.get(new SimpleKey("3"))).isNull();
    map.destroy();
}
 
Example 14
Source File: FileUtils.java    From kkFileViewOfficeEdit with Apache License 2.0 2 votes vote down vote up
/**
 * 获取redis中压缩包内图片文件
 * @param fileKey
 * @return
 */
public List getRedisImgUrls(String fileKey){
    RMapCache<String, List> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_IMGS_KEY);
    return convertedList.get(fileKey);
}