ZUNIONSTORE记录

看了下redis文档,卧槽写的一点都不清楚,网上找的大部分都是抄的文档,后边自己好好研究了下,说下自己的理解,做个记录,免得以后自己也忘了,还是按文档上的例子来吧

127.0.0.1:6379> zadd zset1 1 "one" 2 "two"
(integer) 2
127.0.0.1:6379> zadd zset2 1 "one" 2 "two" 3 "three"

先来个不带这玩意的:WEIGHTS

127.0.0.1:6379> zunionstore out 2 zset1 zset2
(integer) 3
127.0.0.1:6379> zrange out 0 -1 withscores
1) "one"
2) "2"     (解释: 1 +1 =2 )  也就是第一个有续集和的one的分值跟第二个集合one的分值相加,跟                                                   one的顺序没关系,只是相同成员的分值相加
3) "three"
4) "3"   (解释: 0 +3 =2 )
5) "two"
6) "4"  (解释: 2 +2 =4 )

zunionstore out 2 zset1 zset2 这条命令中的2表示两个有序集合,如果合并三个集合,这里就是3

现在带上这玩意:WEIGHTS

127.0.0.1:6379> zunionstore out2 2 zset1 zset2 weights 2 3
(integer) 3

127.0.0.1:6379> zrange out2 0 -1 withscores
1) "one"
2) "5"  (解释: 1*2 + 1*3 = 5 也就是第一个集合的每个成员的分值乘以2加上第二个集合的没一个成             员的分值乘以3
3) "three"
4) "9"  (0*2 + 3*3 = 9)
5) "two"
6) "10"  (2*2 + 2*3 = 10)

这样就清楚多了

原文链接:https://blog.csdn.net/laiyijian/article/details/123505580

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用Redis可以通过Spring Data Redis来实现。Spring Data Redis提供了对Redis的操作和管理的支持,包括连接池配置、数据存取、事务管理等。 要使用Spring Boot和Redis,首先需要在pom.xml文件中添加相关依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 然后,在application.properties或application.yml文件中配置Redis连接信息: ```properties spring.redis.host=127.0.0.1 spring.redis.port=6379 ``` 接下来,可以通过注入RedisTemplate来使用Redis的各种操作。例如,使用RedisTemplate进行字符串存取操作的示例代码如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; @Component public class RedisExample { @Autowired private RedisTemplate<String, String> redisTemplate; public void setKey(String key, String value) { redisTemplate.opsForValue().set(key, value); } public String getValue(String key) { return redisTemplate.opsForValue().get(key); } } ``` 在上述示例中,通过@Autowired注解将RedisTemplate注入到RedisExample类中,然后可以使用opsForValue()方法获取字符串操作的接口,进而进行存取操作。 至于ZUNIONSTORE命令,它是Redis的一个集合操作命令,用于计算多个集合的并集并将结果存储到一个新的集合中。在Spring Boot中使用ZUNIONSTORE命令,可以通过RedisTemplate的execute方法来执行自定义的Redis命令。以下是一个示例代码: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; @Component public class RedisExample { @Autowired private RedisTemplate<String, String> redisTemplate; public Long unionStore(String destinationKey, String... keys) { return redisTemplate.execute((RedisCallback<Long>) connection -> { byte[][] rawKeys = new byte[keys.length][]; for (int i = 0; i < keys.length; i++) { rawKeys[i] = redisTemplate.getKeySerializer().serialize(keys[i]); } return connection.zUnionStore(redisTemplate.getKeySerializer().serialize(destinationKey), rawKeys); }); } } ``` 在上述示例中,通过execute方法执行自定义的Redis命令,使用zUnionStore方法计算多个集合的并集并将结果存储到destinationKey指定的新集合中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值