SpringBoot RedisTemplate opsForZSet
SpringBoot RedisTemplate opsForZSet1 SpringBoot RedisTemplatespring-data-redis-3.0.0-M2.jar@Autowiredprivate RedisTemplate redisTemplate;@Overridepublic ZSetOperations<K, V> opsForZSet() {return
SpringBoot RedisTemplate opsForZSet
- 1 SpringBoot RedisTemplate
- 1 Boolean add(K key, V value, double score)
- 2 Long add(K key, Set< TypedTuple< V>> tuples)
- 3 Long addIfAbsent(K key, Set< TypedTuple< V>> tuples)
- 4 Boolean addIfAbsent(K key, V value, double score)
- 5 Long count(K key, double min, double max)
- 6 Set< V> difference(K key, K otherKey)
- 7 Set< V> difference(K key, Collection< K> otherKeys)
- 8 Long differenceAndStore(K key, Collection<K> otherKeys, K destKey)
- 9 Set< TypedTuple< V>> differenceWithScores(K key, Collection< K> otherKeys)
- 10 Set< TypedTuple< V>> differenceWithScores(K key, K otherKey)
- 11 Set< V> distinctRandomMembers(K key, long count)
- 12 Set< ZSetOperations.TypedTuple< V>> distinctRandomMembersWithScore(K, long)
- 13 Double incrementScore(K key, V value, double delta)
- 14 Set< V> intersect(K key, Collection< K> otherKeys)
- 15 Long intersectAndStore(K key, Collection< K> otherKeys, K destKey)
- 16 intersectAndStore(K, Collection<K>, K, Aggregate, Weights)
- 17 intersectAndStore(K, K, K)
- 18 intersectWithScores(K, Collection<K>)
- 19 intersectWithScores(K, Collection<K>, Aggregate, Weights)
spring-data-redis-3.0.0-M2.jar
@Autowired
private RedisTemplate redisTemplate;
@Override
public ZSetOperations<K, V> opsForZSet() {
return zSetOps;
}
类型 | Value | 适用场景 |
---|---|---|
String | 字符串 | 缓存结构体信息,计数功能 |
List | 列表 | 做异步队列使用,秒杀抢购场景,队列,栈 |
Hash | 字典 | 缓存对象信息 |
Set | 无序集合 | 数据去重,求交集、并集、差集等操作 (共同好友) |
zset(Sorted Set) | 有序集合 | 数据去重,求交集、并集、差集等操作 (共同好友),优先级排序 |
1 SpringBoot RedisTemplate
1 SpringBoot RedisTemplate opsForValue
2 SpringBoot RedisTemplate opsForHash
3 SpringBoot RedisTemplate opsForList
4 SpringBoot RedisTemplate opsForSet
5 SpringBoot RedisTemplate opsForZSet
1 Boolean add(K key, V value, double score)
單個新增數據
K 是否存在 | V 是否存在 | 操作 | 返回 |
---|---|---|---|
是 | 是 | 覆蓋 score | false |
是 | 否 | 新增并添加 socre | true |
否 | - | 新增并添加 socre | true |
@Test
public void test() {
Boolean value1 = redisTemplate.opsForZSet().add("OpsForZSet", "A", 1);
Boolean value2 = redisTemplate.opsForZSet().add("OpsForZSet", "B", 3);
Boolean value3 = redisTemplate.opsForZSet().add("OpsForZSet", "C", 2);
Boolean value4 = redisTemplate.opsForZSet().add("OpsForZSet", "D", 4);
Boolean value5 = redisTemplate.opsForZSet().add("OpsForZSet", "A", 1);
System.out.println(value1);
System.out.println(value5);
}
true
false
2 Long add(K key, Set< TypedTuple< V>> tuples)
批量新增數據
K 是否存在 | V 是否存在 | 操作 | 返回 |
---|---|---|---|
是 | 是 | 覆蓋 score | 0 |
是 | 否 | 新增并添加 socre | 成功新增數量 |
否 | - | 新增并添加 socre | 成功新增數量 |
@Test
public void add() {
ZSetOperations.TypedTuple<Object> object1 = new DefaultTypedTuple<>("E", 6.0D);
ZSetOperations.TypedTuple<Object> object2 = new DefaultTypedTuple<>("F", 7.0D);
ZSetOperations.TypedTuple<Object> object3 = new DefaultTypedTuple<>("G", 5.0D);
ZSetOperations.TypedTuple<Object> object4 = new DefaultTypedTuple<>("A", 8D);
Set<ZSetOperations.TypedTuple<Object>> set = new HashSet<>();
set.add(object1);
set.add(object2);
set.add(object3);
set.add(object4);
Long value1 = redisTemplate.opsForZSet().add("OpsForZSet", set);
System.out.println(value1);
}
3
3 Long addIfAbsent(K key, Set< TypedTuple< V>> tuples)
批量新增不存在的數據(如果值不存在就新增加數據)
K 是否存在 | V 是否存在 | 操作 | 返回 |
---|---|---|---|
是 | 是 | - | 0 |
是 | 否 | 新增并添加 socre | 成功新增數量 |
否 | - | 新增并添加 socre | 成功新增數量 |
@Test
public void addIfAbsent() {
ZSetOperations.TypedTuple<Object> object1 = new DefaultTypedTuple<>("A", 1.550D);
ZSetOperations.TypedTuple<Object> object2 = new DefaultTypedTuple<>("B", 2.660D);
ZSetOperations.TypedTuple<Object> object3 = new DefaultTypedTuple<>("D", 4.0001D);
ZSetOperations.TypedTuple<Object> object4 = new DefaultTypedTuple<>("E", 5.6D);
Set<ZSetOperations.TypedTuple<Object>> set = new HashSet<>();
set.add(object1);
set.add(object2);
set.add(object3);
set.add(object4);
Long value1 = redisTemplate.opsForZSet().addIfAbsent("OpsForZSet", set);
Long value2 = redisTemplate.opsForZSet().addIfAbsent("OpsForZSetTemp", set);
System.out.println(value1);
}
2
4 Boolean addIfAbsent(K key, V value, double score)
單個新增不存在的數據(如果值不存在就新增加數據)
5 Long count(K key, double min, double max)
獲取 min~max score 的值的數量
K 是否存在 | min~max score 之間是否有數據 | 返回 |
---|---|---|
是 | 是 | 返回 value 的數量 |
是 | 否 | 0 |
否 | - | 0 |
@Test
public void count() {
Long value1 = redisTemplate.opsForZSet().count("OpsForZSet", 0, 2);
System.out.println(value1);
}
2
6 Set< V> difference(K key, K otherKey)
獲取兩個鍵的差集的值
key 和 otherKey 的差集是否有數據 | 返回 |
---|---|
是 | key 與 otherKey 的差集 |
否 | 空 Set |
@Test
public void difference() {
Set<String> value1 = redisTemplate.opsForZSet().difference("OpsForZSetB", "OpsForZSet");
System.out.println(value1);
}
[D, E]
7 Set< V> difference(K key, Collection< K> otherKeys)
獲取多個鍵的差集的值
@Test
public void difference1() {
List<String> keys1 = new ArrayList<>();
Set<String> value1 = redisTemplate.opsForZSet().difference("OpsForZSet", keys1);
System.out.println(value1);
}
8 Long differenceAndStore(K key, Collection otherKeys, K destKey)
獲取多個鍵的差集的值並保存到destKey中
key 和 otherKey 的差集是否有數據 | destKey 是否存在 | 操作 | 返回 |
---|---|---|---|
是 | 是 | 將 key 與 otherKeys 的差集新增到 destKey 中(如果 destKey 有相同的數據,則會覆蓋舊的數據) | 新增成功的數據 |
否 | 是 | - | 0 |
否 | 否 | - | 0 |
@Test
public void differenceAndStore() {
List<String> keys = new ArrayList<>();
keys.add("OpsForZSetB");
Long value1 = redisTemplate.opsForZSet().differenceAndStore("OpsForZSet", keys, "OpsForZSet-V");
System.out.println(value1);
}
2
9 Set< TypedTuple< V>> differenceWithScores(K key, Collection< K> otherKeys)
獲取多個鍵的差集的值和score
key 和 otherKey 的差集是否有數據 | 返回 |
---|---|
是 | 差集及 score |
否 | 空 Set |
@Test
public void differenceWithScores() {
List<String> keys = new ArrayList<>();
keys.add("OpsForZSetB");
Set<String> value1 = redisTemplate.opsForZSet().differenceWithScores("OpsForZSet", keys);
System.out.println(value1);
}
[DefaultTypedTuple [score=4.0001, value=D], DefaultTypedTuple [score=5.6, value=E]]
10 Set< TypedTuple< V>> differenceWithScores(K key, K otherKey)
獲取多兩個鍵的差集的值和score
@Test
public void differenceWithScores1() {
Set<Object> value1 = redisTemplate.opsForZSet().differenceWithScores("OpsForZSet", "OpsForZSetB");
System.out.println(value1);
}
[DefaultTypedTuple [score=4.0001, value=D], DefaultTypedTuple [score=5.6, value=E]]
11 Set< V> distinctRandomMembers(K key, long count)
随机获取值
@Test
public void distinctRandomMembers() {
Set<Object> value1 = redisTemplate.opsForZSet().distinctRandomMembers("OpsForZSet", 2);
System.out.println(value1);
}
[E, G]
12 Set< ZSetOperations.TypedTuple< V>> distinctRandomMembersWithScore(K, long)
随机获取值和score
@Test
public void distinctRandomMembersWithScore() {
Set<Object> value = redisTemplate.opsForZSet().distinctRandomMembersWithScore("OpsForZSet", 2);
System.out.println(value);
}
[DefaultTypedTuple [score=7.0, value=F], DefaultTypedTuple [score=8.0, value=A]]
13 Double incrementScore(K key, V value, double delta)
修改键值对的score
key 和 value 时候存在 | 返回 |
---|---|
是 | 返回增长后的score |
否 | 新增键值对并返回delta |
@Test
public void incrementScore() {
Double value = redisTemplate.opsForZSet().incrementScore("OpsForZSet", "A", 2);
System.out.println(value);
}
10.0
14 Set< V> intersect(K key, Collection< K> otherKeys)
15 Long intersectAndStore(K key, Collection< K> otherKeys, K destKey)
16 intersectAndStore(K, Collection, K, Aggregate, Weights)
17 intersectAndStore(K, K, K)
18 intersectWithScores(K, Collection)
19 intersectWithScores(K, Collection, Aggregate, Weights)
lexCount(K, Range)
popMax(K)
popMax(K, long)
popMax(K, long, TimeUnit)
popMin(K)
popMin(K, long)
popMin(K, long, TimeUnit)
randomMember(K)
randomMembers(K, long)
randomMembersWithScore(K, long)
randomMemberWithScore(K)
range(K, long, long)
rangeByLex(K, Range, Limit)
rangeByScore(K, double, double)
rangeByScore(K, double, double, long, long)
rangeByScore(K, String, String)
rangeByScore(K, String, String, long, long)
rangeByScoreWithScores(K, double, double)
rangeByScoreWithScores(K, double, double, long, long)
rangeWithScores(K, long, long)
rank(K, Object)
remove(K, Object…)
removeRange(K, long, long)
removeRangeByLex(K, Range)
removeRangeByScore(K, double, double)
reverseRange(K, long, long)
reverseRangeByLex(K, Range, Limit)
reverseRangeByScore(K, double, double)
reverseRangeByScore(K, double, double, long, long)
reverseRangeByScoreWithScores(K, double, double)
reverseRangeByScoreWithScores(K, double, double, long, long)
reverseRangeWithScores(K, long, long)
reverseRank(K, Object)
scan(K, ScanOptions)
score(K, Object)
score(K, Object…)
size(K)
union(K, Collection)
unionAndStore(K, Collection, K)
unionAndStore(K, Collection, K, Aggregate, Weights)
unionAndStore(K, K, K)
unionWithScores(K, Collection)
unionWithScores(K, Collection, Aggregate, Weights)
zCard(K)
更新中…
更多推荐
所有评论(0)