• 分布式集合
    • TOC
    • Map"> Map
      • Map eviction"> Map eviction
    • MultiMap"> MultiMap
      • 基于 Set 的 MultiMap"> 基于 Set 的 MultiMap
      • 基于 List 的 MultiMap"> 基于 List 的 MultiMap
      • MultiMap eviction"> MultiMap eviction
    • Set"> Set
      • Set eviction"> Set eviction
    • SortedSet"> SortedSet
    • ScoredSortedSet"> ScoredSortedSet
    • LexSortedSet"> LexSortedSet
    • List"> List
    • Queue"> Queue
    • Deque"> Deque
    • BlockingQueue"> BlockingQueue
    • BlockingDeque"> BlockingDeque

    分布式集合

    TOC

    Map

    Redisson 分布式的 Map 对象,实现了 java.util.concurrent.ConcurrentMapjava.util.Map 接口。 Map 的大小由 Redis 限制为 4 294 967 295

    1. RMap<String, SomeObject> map = redisson.getMap("anyMap");
    2. SomeObject prevObject = map.put("123", new SomeObject());
    3. SomeObject currentObject = map.putIfAbsent("323", new SomeObject());
    4. SomeObject obj = map.remove("123");
    5. map.fastPut("321", new SomeObject());
    6. map.fastRemove("321");
    7. Future<SomeObject> putAsyncFuture = map.putAsync("321");
    8. Future<Void> fastPutAsyncFuture = map.fastPutAsync("321");
    9. map.fastPutAsync("321", new SomeObject());
    10. map.fastRemoveAsync("321");

    Redisson PRO 版本的 Map 对象 在集群模式中支持 数据分区

    Map eviction

    Redisson 分布式的 Map 可通过独立的 MapCache 对象支持 eviction。 它也实现了 java.util.concurrent.ConcurrentMapjava.util.Map 接口。 Redisson 有一个基于 Map 和 MapCache 对象的 Spring Cache 集成

    当前 Redis 实现中没有 map 项的 eviction 功能。 因此,过期的项由 org.redisson.EvictionScheduler 来清理。 它一次可移除 100 条过期项。 任务的调度时间会根据上次任务中删除的过期项数量自动调整,时间在 1 秒到 2 个小时内。 因此若清理任务每次删除了 100 项数据,它将每秒钟执行一次(最小的执行延迟)。 但如果当前过期项数量比前一次少,则执行延迟将扩大为 1.5 倍。

    1. RMapCache<String, SomeObject> map = redisson.getMapCache("anyMap");
    2. // ttl = 10 minutes,
    3. map.put("key1", new SomeObject(), 10, TimeUnit.MINUTES);
    4. // ttl = 10 minutes, maxIdleTime = 10 seconds
    5. map.put("key1", new SomeObject(), 10, TimeUnit.MINUTES, 10, TimeUnit.SECONDS);
    6. // ttl = 3 seconds
    7. map.putIfAbsent("key2", new SomeObject(), 3, TimeUnit.SECONDS);
    8. // ttl = 40 seconds, maxIdleTime = 10 seconds
    9. map.putIfAbsent("key2", new SomeObject(), 40, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);

    MultiMap

    Redisson 分布式的 MultiMap 对象允许给每个键绑定多个值。 键的数量限制由 Redis 限制为 4 294 967 295

    基于 Set 的 MultiMap

    基于 Set 的 MultiMap 不允许每个键中的值有重复。

    1. RSetMultimap<SimpleKey, SimpleValue> map = redisson.getSetMultimap("myMultimap");
    2. map.put(new SimpleKey("0"), new SimpleValue("1"));
    3. map.put(new SimpleKey("0"), new SimpleValue("2"));
    4. map.put(new SimpleKey("3"), new SimpleValue("4"));
    5. Set<SimpleValue> allValues = map.get(new SimpleKey("0"));
    6. List<SimpleValue> newValues = Arrays.asList(new SimpleValue("7"), new SimpleValue("6"), new SimpleValue("5"));
    7. Set<SimpleValue> oldValues = map.replaceValues(new SimpleKey("0"), newValues);
    8. Set<SimpleValue> removedValues = map.removeAll(new SimpleKey("0"));

    基于 List 的 MultiMap

    基于 List 的 MultiMap 会存储插入的顺序且允许键对应的值中存在重复。

    1. RListMultimap<SimpleKey, SimpleValue> map = redisson.getListMultimap("test1");
    2. map.put(new SimpleKey("0"), new SimpleValue("1"));
    3. map.put(new SimpleKey("0"), new SimpleValue("2"));
    4. map.put(new SimpleKey("0"), new SimpleValue("1"));
    5. map.put(new SimpleKey("3"), new SimpleValue("4"));
    6. List<SimpleValue> allValues = map.get(new SimpleKey("0"));
    7. Collection<SimpleValue> newValues = Arrays.asList(new SimpleValue("7"), new SimpleValue("6"), new SimpleValue("5"));
    8. List<SimpleValue> oldValues = map.replaceValues(new SimpleKey("0"), newValues);
    9. List<SimpleValue> removedValues = map.removeAll(new SimpleKey("0"));

    MultiMap eviction

    Multimap 对象可通过独立的 MultimapCache 对象来支持 eviction。 它对基于 Set 和 List 的 MultiMap 分别有 RSetMultimapCacheRListMultimapCache 对象。

    过期的项由 org.redisson.EvictionScheduler 来清理。 它一次可移除 100 条过期项。 任务的调度时间会根据上次任务中删除的过期项数量自动调整,时间在 1 秒到 2 个小时内。 因此若清理任务每次删除了 100 项数据,它将每秒钟执行一次(最小的执行延迟)。 但如果当前过期项数量比前一次少,则执行延迟将扩大为 1.5 倍。

    RSetMultimapCache 示例:

    1. RSetMultimapCache<String, String> multimap = redisson.getSetMultimapCache("myMultimap");
    2. map.put("1", "a");
    3. map.put("1", "b");
    4. map.put("1", "c");
    5. map.put("2", "e");
    6. map.put("2", "f");
    7. map.expireKey("2", 10, TimeUnit.MINUTES);

    Set

    Redisson 分布式的 Set 对象,实现了 java.util.Set 接口。 它通过元素状态比较来保持元素的独立性。 Set 大小由 Redis 限制为 4 294 967 295

    1. RSet<SomeObject> set = redisson.getSet("anySet");
    2. set.add(new SomeObject());
    3. set.remove(new SomeObject());

    Redisson PRO 版本的 Set 对象 在集群模式中支持 数据分区

    Set eviction

    Redisson 分布式的 Set 对象可通过独立的 SetCache 对象来支持 eviction。 它也实现了 java.util.Set 接口。

    当前 Redis 实现中没有 set 值的 eviction 功能。 因此,过期的项由 org.redisson.EvictionScheduler 来清理。 它一次可移除 100 条过期项。 任务的调度时间会根据上次任务中删除的过期项数量自动调整,时间在 1 秒到 2 个小时内。 因此若清理任务每次删除了 100 项数据,它将每秒钟执行一次(最小的执行延迟)。 但如果当前过期项数量比前一次少,则执行延迟将扩大为 1.5 倍。

    1. RSetCache<SomeObject> set = redisson.getSetCache("anySet");
    2. // ttl = 10 seconds
    3. set.add(new SomeObject(), 10, TimeUnit.SECONDS);

    SortedSet

    Redisson 分布式的 SortedSet 对象,实现了 java.util.SortedSet 接口。 它通过比较器来排列元素并保持唯一性。

    1. RSortedSet<Integer> set = redisson.getSortedSet("anySet");
    2. set.trySetComparator(new MyComparator()); // set object comparator
    3. set.add(3);
    4. set.add(1);
    5. set.add(2);
    6. set.removeAsync(0);
    7. set.addAsync(5);

    ScoredSortedSet

    Redisson 分布式的 ScoredSortedSet 对象。 它通过在元素插入时定义的分数来排列元素, 通过元素状态的比较来保持元素的唯一性。

    1. RScoredSortedSet<SomeObject> set = redisson.getScoredSortedSet("simple");
    2. set.add(0.13, new SomeObject(a, b));
    3. set.addAsync(0.251, new SomeObject(c, d));
    4. set.add(0.302, new SomeObject(g, d));
    5. set.pollFirst();
    6. set.pollLast();
    7. int index = set.rank(new SomeObject(g, d)); // get element index
    8. Double score = set.getScore(new SomeObject(g, d)); // get element score

    LexSortedSet

    Redisson 分布式的 Set 对象,它仅允许字典序的 String 对象,并实现了 java.util.Set<String> 接口。 它通过元素状态比较来保持元素的唯一性。

    1. RLexSortedSet set = redisson.getLexSortedSet("simple");
    2. set.add("d");
    3. set.addAsync("e");
    4. set.add("f");
    5. set.lexRangeTail("d", false);
    6. set.lexCountHead("e");
    7. set.lexRange("d", true, "z", false);

    List

    Redisson 分布式的 List 对象,实现了 java.util.List 接口。 它保留元素的插入顺序。 List 大小由 Redis 限制为 4 294 967 295

    1. RList<SomeObject> list = redisson.getList("anyList");
    2. list.add(new SomeObject());
    3. list.get(0);
    4. list.remove(new SomeObject());

    Queue

    Redisson 分布式的 Queue 对象,实现了 java.util.Queue 接口。 Queue 大小由 Redis 限制为 4 294 967 295

    1. RQueue<SomeObject> queue = redisson.getQueue("anyQueue");
    2. queue.add(new SomeObject());
    3. SomeObject obj = queue.peek();
    4. SomeObject someObj = queue.poll();

    Deque

    Redisson 分布式的 Deque 对象,实现了 java.util.Deque 接口。 Deque 大小由 Redis 限制为 4 294 967 295

    1. RDeque<SomeObject> queue = redisson.getDeque("anyDeque");
    2. queue.addFirst(new SomeObject());
    3. queue.addLast(new SomeObject());
    4. SomeObject obj = queue.removeFirst();
    5. SomeObject someObj = queue.removeLast();

    BlockingQueue

    Redisson 分布式的 BlockingQueue 对象,实现了 java.util.concurrent.BlockingQueue 接口。 BlockingQueue 大小由 Redis 限制为 4 294 967 295

    1. RBlockingQueue<SomeObject> queue = redisson.getBlockingQueue("anyQueue");
    2. queue.offer(new SomeObject());
    3. SomeObject obj = queue.peek();
    4. SomeObject someObj = queue.poll();
    5. SomeObject ob = queue.poll(10, TimeUnit.MINUTES);

    pollpollFromAnypollLastAndOfferFirstTotake 方法 在重连到 Redis 服务器或 Redis 服务器故障恢复时会自动被重新订阅。

    BlockingDeque

    Redisson 分布式的 BlockingDeque 对象,实现了 java.util.concurrent.BlockingDeque 接口。 BlockingDeque 大小由 Redis 限制为 4 294 967 295

    1. RBlockingDeque<Integer> deque = redisson.getBlockingDeque("anyDeque");
    2. deque.putFirst(1);
    3. deque.putLast(2);
    4. Integer firstValue = queue.takeFirst();
    5. Integer lastValue = queue.takeLast();
    6. Integer firstValue = queue.pollFirst(10, TimeUnit.MINUTES);
    7. Integer lastValue = queue.pollLast(3, TimeUnit.MINUTES);

    pollpollFromAnypollLastAndOfferFirstTotake 方法 在重连到 Redis 服务器或 Redis 服务器故障恢复时会自动被重新订阅。