[redis] redis学习之 ~ command ZINTERSTORE详解

致访客:值得一读
参考网址
redis官网文档
redis菜鸟教程
redis中文网

command 语法:

ZINTERSTORE destination numkeys key [key …] [WEIGHTS weight [weight …]] [AGGREGATE SUM|MIN|MAX]

command 意义:

ZINTERSTORE ~ 命令关键字
destination ~指定结果集保存的集合key
numkeys key [key …] ~ numkeys 指定有多少个集合参与交集运算, key 指定参与交集运算的集合的key
[WEIGHTS weight [weight …]] ~ 指定参与交集运算各集合score的权重参数
[AGGREGATE SUM|MIN|MAX] ~ 指定交集中元素的score的取值方式,例如:sum 等于各集合中该元素的score乘以权重求和
注:
1 默认情况下:权重皆为1 ,取值方式为 sum
2 若指定的destination已经存在,则结果集会覆盖这个key ,无论之前这个key是什么类型,是否包含元素,使用时务必注意

Redis Zinterstore 命令计算给定的一个或多个有序集的交集,其中给定 key 的数量必须以 numkeys 参数指定,并将该交集(结果集)储存到 destination 。结果集中元素score为 取值方式计算各集合中该元素score乘以权重结果

command 示例:

#新增一个有序集合
127.0.0.1:6379> zadd zkey1 1 one 2 two 3 three 4 foure 5 five
(integer) 5
127.0.0.1:6379> zrange zkey1 0 -1 withscores
 1) "one"
 2) "1"
 3) "two"
 4) "2"
 5) "three"
 6) "3"
 7) "foure"
 8) "4"
 9) "five"
10) "5"

#新增一个有序集合
127.0.0.1:6379> zadd zkey2 10 one 20 two 60 six 70 seven
(integer) 4
127.0.0.1:6379> zrange zkey2 0 -1 withscores
1) "one"
2) "10"
3) "two"
4) "20"
5) "six"
6) "60"
7) "seven"
8) "70"

#取zkey1 和zkey2 2个有序集合的交集 保存至有序集合zkey3(zkey3若不存在则新建,若存在则覆盖)
127.0.0.1:6379> zinterstore zkey3 2 zkey1 zkey2 
(integer) 2
127.0.0.1:6379> zrange zkey3 0 -1 withscores
1) "one"
2) "11"
3) "two"
4) "22"

#取zkey1 和zkey2 2个有序集合的交集 保存至有序集合zkey3,权重配置为 10 和 1(zkey3若不存在则新建,若存在则覆盖)
# zkey3 score = (zkey1 score)*10 + (zkey2 score)*1 
# 例如 one score: 20 = 1*10+10*1
127.0.0.1:6379> zinterstore zkey3 2 zkey1 zkey2 weights 10 1
(integer) 2
127.0.0.1:6379> zrange zkey3 0 -1 withscores
1) "one"
2) "20"
3) "two"
4) "40"

# 没有指定numberkeys 
127.0.0.1:6379> zinterstore zkey3 zkey1 zkey2 aggregate min
(error) ERR value is not an integer or out of range

#取zkey1 和zkey2 2个有序集合的交集 保存至有序集合zkey3,取值方式为min(zkey3若不存在则新建,若存在则覆盖)
# zkey3 score = min( (zkey1 score)*1  , (zkey2 score)*1 )
# 例如 one score:1 = min(1,10)
127.0.0.1:6379> zinterstore zkey3 2 zkey1 zkey2 aggregate min
(integer) 2
127.0.0.1:6379> zrange zkey3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"

#测试 保存结果的key已经存在 实际覆盖这个key
#新增一个无序集合 setkey1
127.0.0.1:6379> sadd setkey1 1
(integer) 1
127.0.0.1:6379> smembers setkey1
 1) "1"
#取zkey1 和zkey2 2个有序集合的交集 保存至无序结合setkey1中
127.0.0.1:6379> zinterstore setkey1 2 zkey1 zkey2
(integer) 2
#setkey1 的类型从无序集合变为有序集合了 原有元素已经不存在了
127.0.0.1:6379> type setkey1
zset
127.0.0.1:6379> smembers setkey1 
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> zrange setkey1 0 -1 withscores
1) "one"
2) "11"
3) "two"
4) "22"
127.0.0.1:6379> 


新增加字符串函数 ds_append ds_incrby 新增加类似redis的hashs功能,用法一样 ds_hdel ds_hget ds_hset ds_hmget ds_hmset ds_hincrby ds_hgetall redis-storage 基于最新的redis-2.6.7开发的 用luajit替换LUA,增强lua执行性能 author: 七夜, shenzhe QQ: 531020471 QQ群: 62116204(已满) QQ群: 154249567 (未满) mail: lijinxing@gmail.com, shenzhe163@gmail.com 安装 redis-storage https://github.com/qiye/redis-storage 获取源码 make init make MALLOC=tcmalloc_minimal 这一步需要root权限 make install PREFIX=/usr/local/redis 修改redis配置文件 ds:create_if_missing 1 //if the specified database didn't exist will create a new one ds:error_if_exists 0 //if the opened database exsits will throw exception ds:paranoid_checks 0 ds:block_cache_size 10000 ds:write_buffer_size 100000000 //写缓存大小 ds:block_size 4096 ds:max_open_files 8000 //leveldb最多可以使用的檔案數,一個檔案可以儲存 2MB 的資料。 ds:block_restart_interval 16 ds:path /usr/local/redis/db/leveldb //leveldb save path redis new cmd 用法跟redis的一样 ds_append ds_incrby ds_hdel ds_hget ds_hset ds_hmget ds_hmset ds_hincrby ds_hgetall ds_set name qiye ds_get name ds_del name ds_mset key value age 20 ds_mget key age ds_del key age rl_set name shenzhe //先把数据存到leveldb,再存到redis rl_get name //先尝试从redis取数据,如没取到,再尝试从redis取数据 rl_del name //先从leveldb删除数据,再从redis删除数据 cd php-hiredis/ //php code include "redis.php"; $db = new redis("127.0.0.1", 6379); $rc = $db->connect(); if(!$rc) { echo "can not connect redis server\r\n"; exit; } $data = $db->multi(array('DEL test', 'SET test 1', 'GET test')); print_r($data); echo $db->set("name", "qiye"); echo $db->get("name"); $db->ds_set("name", "qiye"); $db->ds_set("age", "20"); $data = $db->ds_mget( "name", "age"); print_r($data); php开发者推荐使用 phpredis 加强版 专门针对redis-storage的php扩展 地址: https://github.com/shenzhe/phpredis $redis->dsSet("name", "shenzhe"); //把数据存到leveldb $redis->dsGet("name"); //从leveldb取出数据, 输出 shenzhe $redis->dsMSet(array("daniu"=>"qiye","cainiao"=>"shenzhe")); //批量把数据存到leveldb; keys结构 array("key1"=>"val1", "key2"=>"val2") $redis->dsMGet(array("qiye", "cainiao")); //批量从leveldb取出数据 $redis->dsDel("name"); //从leveldb删除数据, $key可以是字符串,也可是key的数组集合(相当于批量删除) $redis->dsDel(array("daniu","cainiao")); //从leveldb删除数据, $key可以是字符串,也可是key的数组集合(相当于批量删除) $redis->rlSet("name", "zeze"); //先把数据存到leveldb,再存到redis $redis->rlGet("name"); $redis->get("name"); $redis->dsGet("name"); $redis->rlDel("name"); 标签:redis 分享 window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdMini": "2", "bdMiniList": [], "bdPic": "", "bdStyle": "1", "bdSize": "24" }, "share": {} }; with (document)0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' ~(-new Date() / 36e5)];\r\n \r\n \r\n \r\n \r\n \u8f6f\u4ef6\u9996\u9875\r\n \u8f6f\u4ef6\u4e0b\u8f7d\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\nwindow.changyan.api.config({\r\nappid: 'cysXjLKDf', conf: 'prod_33c27aefa42004c9b2c12a759c851039' });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值