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 是否存在操作返回
覆蓋 scorefalse
新增并添加 socretrue
-新增并添加 socretrue
@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 是否存在操作返回
覆蓋 score0
新增并添加 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)

更新中…

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐