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

The following examples show how to use org.redisson.api.RMap#put() . 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 testEquals() {
    RMap<String, String> map = getMap("simple");
    map.put("1", "7");
    map.put("2", "4");
    map.put("3", "5");

    Map<String, String> testMap = new HashMap<String, String>();
    testMap.put("1", "7");
    testMap.put("2", "4");
    testMap.put("3", "5");

    assertThat(map).isEqualTo(testMap);
    assertThat(testMap.hashCode()).isEqualTo(map.hashCode());
    destroy(map);
}
 
Example 2
Source File: BaseMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 5000)
public void testDeserializationErrorReturnsErrorImmediately() throws Exception {
    RMap<String, SimpleObjectWithoutDefaultConstructor> map = getMap("deserializationFailure", new JsonJacksonCodec());
    Assume.assumeTrue(!(map instanceof RLocalCachedMap));
    SimpleObjectWithoutDefaultConstructor object = new SimpleObjectWithoutDefaultConstructor("test-val");

    Assert.assertEquals("test-val", object.getTestField());
    map.put("test-key", object);

    try {
        map.get("test-key");
        Assert.fail("Expected exception from map.get() call");
    } catch (Exception e) {
        e.printStackTrace();
    }
    destroy(map);
}
 
Example 3
Source File: BaseMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testValueIterator() {
    RMap<Integer, Integer> map = getMap("simple");
    map.put(1, 0);
    map.put(3, 5);
    map.put(4, 6);
    map.put(7, 8);

    Collection<Integer> values = map.values();
    assertThat(values).containsOnly(0, 5, 6, 8);
    for (Iterator<Integer> iterator = map.values().iterator(); iterator.hasNext();) {
        Integer value = iterator.next();
        if (!values.remove(value)) {
            Assert.fail();
        }
    }

    assertThat(values.size()).isEqualTo(0);
    destroy(map);
}
 
Example 4
Source File: BaseMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAll() {
    RMap<Integer, Integer> map = getMap("getAll");
    map.put(1, 100);
    map.put(2, 200);
    map.put(3, 300);
    map.put(4, 400);

    Map<Integer, Integer> filtered = map.getAll(new HashSet<Integer>(Arrays.asList(2, 3, 5)));

    Map<Integer, Integer> expectedMap = new HashMap<Integer, Integer>();
    expectedMap.put(2, 200);
    expectedMap.put(3, 300);
    assertThat(filtered).isEqualTo(expectedMap);
    destroy(map);
}
 
Example 5
Source File: BaseMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriterFastRemove() {
    Map<String, String> store = new HashMap<>();
    RMap<String, String> map = getWriterTestMap("test", store);

    map.put("1", "11");
    map.put("2", "22");
    map.put("3", "33");
    
    map.fastRemove("1", "2", "4");
    
    Map<String, String> expected = new HashMap<>();
    expected.put("3", "33");
    assertThat(store).isEqualTo(expected);
    destroy(map);
}
 
Example 6
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 7
Source File: RedissonITest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {
  final RedisServer redisServer = new RedisServer();
  TestUtil.retry(new Runnable() {
    @Override
    public void run() {
      redisServer.start();
    }
  }, 10);

  try {
    final Config config = new Config();
    config.useSingleServer().setAddress("redis://127.0.0.1:6379");
    final RedissonClient redissonClient = Redisson.create(config);
    try {
      final RMap<String,String> map = redissonClient.getMap("map");
      map.put("key", "value");
      if (!"value".equals(map.get("key")))
        throw new AssertionError("ERROR: failed to get key value");
    }
    finally {
      redissonClient.shutdown();
    }
  }
  finally {
    redisServer.stop();
    TestUtil.checkSpan(new ComponentSpanCount("java-redis", 2));
    // RedisServer process doesn't exit on 'stop' therefore call System.exit
    System.exit(0);
  }
}
 
Example 8
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 9
Source File: RedissonBaseTransactionalMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAll() {
    RMap<String, String> m = getMap();
    m.put("1", "2");
    m.put("3", "4");
    
    RTransaction t = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = getTransactionalMap(t);
    assertThat(map.getAll(new HashSet<String>(Arrays.asList("1", "3"))).values()).containsOnly("2", "4");
    
    t.commit();
}
 
Example 10
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 11
Source File: BaseMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutAllBatched() {
    RMap<Integer, String> map = getMap("simple");
    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, 5);

    assertThat(map.keySet()).containsOnly(1, 2, 3, 4, 5, 6);
    
    Map<Integer, String> joinMap2 = new HashMap<Integer, String>();
    joinMap2.put(7, "7");
    joinMap2.put(8, "8");
    joinMap2.put(9, "9");
    joinMap2.put(10, "10");
    joinMap2.put(11, "11");
    map.putAll(joinMap2, 5);
    
    assertThat(map.keySet()).containsOnly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);

    Map<Integer, String> joinMap3 = new HashMap<Integer, String>();
    joinMap3.put(12, "12");
    joinMap3.put(13, "13");
    joinMap3.put(14, "14");
    joinMap3.put(15, "15");
    joinMap3.put(16, "16");
    joinMap3.put(17, "17");
    joinMap3.put(18, "18");
    map.putAll(joinMap3, 5);
    
    assertThat(map.keySet()).containsOnly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18);
    
    destroy(map);
}
 
Example 12
Source File: BaseMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testKeySetByPattern() {
    RMap<String, String> map = getMap("simple", StringCodec.INSTANCE);
    map.put("10", "100");
    map.put("20", "200");
    map.put("30", "300");

    assertThat(map.keySet("?0")).containsExactly("10", "20", "30");
    assertThat(map.keySet("1")).isEmpty();
    assertThat(map.keySet("10")).containsExactly("10");
    destroy(map);
}
 
Example 13
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 14
Source File: IpRule.java    From kk-anti-reptile with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected boolean doExecute(HttpServletRequest request, HttpServletResponse response) {
    String ipAddress = getIpAddr(request);
    List<String> ignoreIpList = properties.getIpRule().getIgnoreIp();
    if (ignoreIpList != null && ignoreIpList.size() > 0) {
        for (String ignoreIp : ignoreIpList) {
            if (ignoreIp.endsWith("*")) {
                ignoreIp = ignoreIp.substring(0, ignoreIp.length() - 1);
            }
            if (ipAddress.startsWith(ignoreIp)) {
                return false;
            }
        }
    }
    String requestUrl = request.getRequestURI();
    //毫秒,默认5000
    int expirationTime = properties.getIpRule().getExpirationTime();
    //最高expirationTime时间内请求数
    int requestMaxSize = properties.getIpRule().getRequestMaxSize();
    RAtomicLong rRequestCount = redissonClient.getAtomicLong(RATELIMITER_COUNT_PREFIX.concat(requestUrl).concat(ipAddress));
    RAtomicLong rExpirationTime = redissonClient.getAtomicLong(RATELIMITER_EXPIRATIONTIME_PREFIX.concat(requestUrl).concat(ipAddress));
    if (!rExpirationTime.isExists()) {
        rRequestCount.set(0L);
        rExpirationTime.set(0L);
        rExpirationTime.expire(expirationTime, TimeUnit.MILLISECONDS);
    } else {
        RMap rHitMap = redissonClient.getMap(RATELIMITER_HIT_CRAWLERSTRATEGY);
        if ((rRequestCount.incrementAndGet() > requestMaxSize) || rHitMap.containsKey(ipAddress)) {
            //触发爬虫策略 ,默认10天后可重新访问
            long lockExpire = properties.getIpRule().getLockExpire();
            rExpirationTime.expire(lockExpire, TimeUnit.SECONDS);
            //保存触发来源
            rHitMap.put(ipAddress, requestUrl);
            LOGGER.info("Intercepted request, uri: {}, ip:{}, request :{}, times in {} ms。Automatically unlock after {} seconds", requestUrl, ipAddress, requestMaxSize, expirationTime,lockExpire);
            return true;
        }
    }
    return false;
}
 
Example 15
Source File: BaseMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutIfAbsent() throws Exception {
    RMap<SimpleKey, SimpleValue> map = getMap("simple");
    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));
    destroy(map);
}
 
Example 16
Source File: RedissonCodecTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testListOfStrings() {
    Config config = createConfig();
    config.setCodec(new JsonJacksonCodec());
    RedissonClient redisson = Redisson.create(config);

    RMap<String, List<String>> map = redisson.getMap("list of strings", jsonListOfStringCodec);
    map.put("foo", new ArrayList<String>(Arrays.asList("bar")));

    RMap<String, List<String>> map2 = redisson.getMap("list of strings", jsonListOfStringCodec);

    assertThat(map2).isEqualTo(map);
    
    redisson.shutdown();
}
 
Example 17
Source File: RedissonInstrumentationTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void testHash() {
    try (Scope scope = tracer.startRootTransaction(getClass().getClassLoader()).withName("transaction").activateInScope()) {
        RMap<String, String> rMap = redisson.getMap("map1");
        rMap.put("key1", "value1");

        assertThat(rMap.get("key1")).isEqualTo("value1");
    }

    assertTransactionWithRedisSpans("EVAL", "HGET");
}
 
Example 18
Source File: RedissonMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadAllEntrySet() {
    RMap<Integer, String> map = redisson.getMap("simple12");
    map.put(1, "12");
    map.put(2, "33");
    map.put(3, "43");

    assertThat(map.readAllEntrySet().size()).isEqualTo(3);
    Map<Integer, String> testMap = new HashMap<Integer, String>(map);
    assertThat(map.readAllEntrySet()).containsOnlyElementsOf(testMap.entrySet());
}
 
Example 19
Source File: BaseMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadAllValues() {
    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"));

    assertThat(map.readAllValues().size()).isEqualTo(3);
    Map<SimpleKey, SimpleValue> testMap = new HashMap<>(map);
    assertThat(map.readAllValues()).containsOnlyElementsOf(testMap.values());
    destroy(map);
}
 
Example 20
Source File: BaseMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testFastRemove() throws InterruptedException, ExecutionException {
    RMap<Integer, Integer> map = getMap("simple");
    map.put(1, 3);
    map.put(3, 5);
    map.put(4, 6);
    map.put(7, 8);

    assertThat(map.fastRemove(1, 3, 7)).isEqualTo(3);
    Thread.sleep(1);
    assertThat(map.size()).isEqualTo(1);
    destroy(map);
}