分布式集合
6.1. 字典
Redisson 分布式字典对象继承自 java.util.concurrent.ConcurrentMap
和 java.util.Map
接口。字典的大小限制于Redis,最大为 4 294 967 295.
RMap<String, SomeObject> map = redisson.getMap("anyMap");
SomeObject prevObject = map.put("123", new SomeObject());
SomeObject currentObject = map.putIfAbsent("323", new SomeObject());
SomeObject obj = map.remove("123");
map.fastPut("321", new SomeObject());
map.fastRemove("321");
Future<SomeObject> putAsyncFuture = map.putAsync("321");
Future<Void> fastPutAsyncFuture = map.fastPutAsync("321");
map.fastPutAsync("321", new SomeObject());
map.fastRemoveAsync("321");
6.1.1. 字典回收
Redisson 分布式字典包含回收支持,通过独立的MapCache对象。它同样实现了java.util.concurrent.ConcurrentMap
和 java.util.Map
接口。Redisson包含了 Spring Cache 集成,其实基于Map and MapCache 对象。
当前的redis实现没有包含字典项的回收功能。因此失效项是通过org.redisson.EvictionScheduler
来进行回收的。它一次能够移除100 条失效数据。 任务的执行时间将自动调整,依赖于上次失效的失效项数目,时间变化可能从1 秒到 2 个小时。 因此如果清除任务删除了100个项每次,将会每秒都被执行(最小的执行延迟)。 如果当前的失效项目数目低于前一次执行的数目,执行的延迟时间将会增加1.5倍。
RMapCache<String, SomeObject> map = redisson.getMapCache("anyMap");
// ttl = 10 minutes,
map.put("key1", new SomeObject(), 10, TimeUnit.MINUTES);
// ttl = 10 minutes, maxIdleTime = 10 seconds
map.put("key1", new SomeObject(), 10, TimeUnit.MINUTES, 10, TimeUnit.SECONDS);
// ttl = 3 seconds
map.putIfAbsent("key2", new SomeObject(), 3, TimeUnit.SECONDS);
// ttl = 40 seconds, maxIdleTime = 10 seconds
map.putIfAbsent("key2", new SomeObject(), 40, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);
6.2. 多重字典
Redisson 多重字典对象允许绑定多个值在每一个key上。 Keys 大小受限于redis,最大为4 294 967 295。
6.2.1. 基于多重字典的set
基于多重字典的set在每个key上不允许有重复的数据。
RSetMultimap<SimpleKey, SimpleValue> map = redisson.getSetMultimap("myMultimap");
map.put(new SimpleKey("0"), new SimpleValue("1"));
map.put(new SimpleKey("0"), new SimpleValue("2"));
map.put(new SimpleKey("3"), new SimpleValue("4"));
Set<SimpleValue> allValues = map.get(new SimpleKey("0"));
List<SimpleValue> newValues = Arrays.asList(new SimpleValue("7"), new SimpleValue("6"), new SimpleValue("5"));
Set<SimpleValue> oldValues = map.replaceValues(new SimpleKey("0"), newValues);
Set<SimpleValue> removedValues = map.removeAll(new SimpleKey("0"));
6.2.2. 基于多重字典的list
基于多重字典的list按照插入的顺序进行存储,允许在一个key上映射多个重复的值。
RListMultimap<SimpleKey, SimpleValue> map = redisson.getListMultimap("test1");
map.put(new SimpleKey("0"), new SimpleValue("1"));
map.put(new SimpleKey("0"), new SimpleValue("2"));
map.put(new SimpleKey("0"), new SimpleValue("1"));
map.put(new SimpleKey("3"), new SimpleValue("4"));
List<SimpleValue> allValues = map.get(new SimpleKey("0"));
Collection<SimpleValue> newValues = Arrays.asList(new SimpleValue("7"), new SimpleValue("6"), new SimpleValue("5"));
List<SimpleValue> oldValues = map.replaceValues(new SimpleKey("0"), newValues);
List<SimpleValue> removedValues = map.removeAll(new SimpleKey("0"));
6.3. Set
Redisson 分布式Set对象实现了 java.util.Set
接口。通过元素状态比较的方式保持数据的唯一性。 Set 大小受限于Redis,最大为4 294 967 295。
RSet<SomeObject> set = redisson.getSet("anySet");
set.add(new SomeObject());
set.remove(new SomeObject());
6.3.1. Set 回收
Redisson 分布式Set包含回收的支持通过实现了独立的SetCache 对象。 它同样实现了java.util.Set
接口。
当前的redis实现没有包含set的回收功能。因此失效的值是通过org.redisson.EvictionScheduler
执行的。 它将删除100个失效值一次。任务的执行时间将自动调整,依赖于上次失效的失效项数目,时间变化可能从1 秒到 2 个小时。 因此如果清除任务删除了100个项每次,将会每秒都被执行(最小的执行延迟)。 如果当前的失效项目数目低于前一次执行的数目,执行的延迟时间将会增加1.5倍。
RSetCache<SomeObject> set = redisson.getSetCache("anySet");
// ttl = 10 seconds
set.add(new SomeObject(), 10, TimeUnit.SECONDS);
6.4. 排序的Set
Redisson 分布式的排序Set对象实现了 java.util.SortedSet
接口。使用比较器进行排序和保持对象的唯一性。
RSortedSet<Integer> set = redisson.getSortedSet("anySet");
set.trySetComparator(new MyComparator()); // set object comparator
set.add(3);
set.add(1);
set.add(2);
set.removeAsync(0);
set.addAsync(5);
6.5. 基于分数的排序Set
Redisson 分布式基于分数的排序Set对象。 分数是在元素插入的时候定义的。通过元素的状态比较保持元素的唯一性。
RScoredSortedSet<SomeObject> set = redisson.getScoredSortedSet("simple");
set.add(0.13, new SomeObject(a, b));
set.addAsync(0.251, new SomeObject(c, d));
set.add(0.302, new SomeObject(g, d));
set.pollFirst();
set.pollLast();
int index = set.rank(new SomeObject(g, d)); // get element index
Double score = set.getScore(new SomeObject(g, d)); // get element score
6.6. 词典序Set
Redisson 分布式只通过词典顺序进行排序的set对象,实现了 java.util.Set<String>
接口。通过元素状态比较保持元素的唯一性。
RLexSortedSet set = redisson.getLexSortedSet("simple");
set.add("d");
set.addAsync("e");
set.add("f");
set.lexRangeTail("d", false);
set.lexCountHead("e");
set.lexRange("d", true, "z", false);
6.7. 列表
Redisson 分布式列表对象实现了 java.util.List
接口。通过插入时的顺序进行保存。列表的大小受限于Redis,最大为 4 294 967 295。
RList<SomeObject> list = redisson.getList("anyList");
list.add(new SomeObject());
list.get(0);
list.remove(new SomeObject());
6.8. 队列
Redisson 分布式队列实现了 java.util.Queue
接口。
队列的大小受限于Redis,最大为 4 294 967 295.
RQueue<SomeObject> queue = redisson.getQueue("anyQueue");
queue.add(new SomeObject());
SomeObject obj = queue.peek();
SomeObject someObj = queue.poll();
6.9. 双向队列
Redisson 分布式双向队列实现了 java.util.Deque
接口。
双向队列的大小受限于Redis,最大为 4 294 967 295。
RDeque<SomeObject> queue = redisson.getDeque("anyDeque");
queue.addFirst(new SomeObject());
queue.addLast(new SomeObject());
SomeObject obj = queue.removeFirst();
SomeObject someObj = queue.removeLast();
6.10. 阻塞队列
Redisson 分布式阻塞队列实现了 java.util.concurrent.BlockingQueue
接口。阻塞队列的大小受限于Redis, 最大为 4 294 967 295。
RBlockingQueue<SomeObject> queue = redisson.getBlockingQueue("anyQueue");
queue.offer(new SomeObject());
SomeObject obj = queue.peek();
SomeObject someObj = queue.poll();
SomeObject ob = queue.poll(10, TimeUnit.MINUTES);
poll
, pollFromAny
, pollLastAndOfferFirstTo
and take
methods will be resubscribed automatically during reconnection to Redis server or Redis server failover.
6.11. 阻塞双向队列
Redisson 分布式阻塞双向队列实现了 java.util.concurrent.BlockingDeque
接口。 阻塞双向队列的大小受限于Redis,最大为 4 294 967 295。
RBlockingDeque<Integer> deque = redisson.getBlockingDeque("anyDeque");
deque.putFirst(1);
deque.putLast(2);
Integer firstValue = queue.takeFirst();
Integer lastValue = queue.takeLast();
Integer firstValue = queue.pollFirst(10, TimeUnit.MINUTES);
Integer lastValue = queue.pollLast(3, TimeUnit.MINUTES);
poll
, pollFromAny
, pollLastAndOfferFirstTo
和 take
方法将会自动重新订阅,当重新连接到Redis服务器上或者是redis服务故障恢复的时候。