码迷,mamicode.com
首页 > Web开发 > 详细

PHP-Redis扩展使用手册(四)(待续)

时间:2015-11-19 20:34:42      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

/* 添加一个或者多个value到无顺序集合总,如果value已经成set中,则返回false
     * @param key set名称
     * @param value 
     * ...
     * @return int 新set长度
     */
    $redis->sAdd(‘key1‘ , ‘member1‘); /* 1, ‘key1‘ => {‘member1‘} */
    $redis->sAdd(‘key1‘ , ‘member2‘, ‘member3‘); /* 2, ‘key1‘ => {‘member1‘, ‘member2‘, ‘member3‘}*/
    $redis->sAdd(‘key1‘ , ‘member2‘); /* 0, ‘key1‘ => {‘member1‘, ‘member2‘, ‘member3‘}*/
    
    /* 返回set的value数量;sSize
     * @param key set 
     * @return int size 
     */
    $redis->sAdd(‘key1‘ , ‘member1‘);
    $redis->sAdd(‘key1‘ , ‘member2‘);
    $redis->sAdd(‘key1‘ , ‘member3‘); /* ‘key1‘ => {‘member1‘, ‘member2‘, ‘member3‘}*/
    $redis->sCard(‘key1‘); /* 3 */
    $redis->sCard(‘keyX‘); /* 0 */
    
    /* 返回n个set的差集
     * @param key set1
     * ...
     * @param key setn
     * @return array 差集
     */
    $redis->delete(‘s0‘, ‘s1‘, ‘s2‘);
    $redis->sAdd(‘s0‘, ‘1‘);
    $redis->sAdd(‘s0‘, ‘2‘);
    $redis->sAdd(‘s0‘, ‘3‘);
    $redis->sAdd(‘s0‘, ‘4‘);
    $redis->sAdd(‘s1‘, ‘1‘);
    $redis->sAdd(‘s2‘, ‘3‘);
    var_dump($redis->sDiff(‘s0‘, ‘s1‘, ‘s2‘)); // array(0=>‘2‘,1=>‘4‘)
    
    /* 求差集,并把差集保存在第一个set中
     * @param key set1
     * ...
     * @param key setn
     * @return int 返回差集元素个数或者false
     */
    $redis->delete(‘s0‘, ‘s1‘, ‘s2‘);
    $redis->sAdd(‘s0‘, ‘1‘);
    $redis->sAdd(‘s0‘, ‘2‘);
    $redis->sAdd(‘s0‘, ‘3‘);
    $redis->sAdd(‘s0‘, ‘4‘);
    $redis->sAdd(‘s1‘, ‘1‘);
    $redis->sAdd(‘s2‘, ‘3‘);
    var_dump($redis->sDiffStore(‘dst‘, ‘s0‘, ‘s1‘, ‘s2‘));    //2
    var_dump($redis->sMembers(‘dst‘));    //array(0=>‘2‘,1=>‘4‘)
    
    /* 求交集,
     * @param key set1
     * ...
     * @param key setn
     * @return array 差集
     */
    $redis->sAdd(‘key1‘, ‘val1‘);
    $redis->sAdd(‘key1‘, ‘val2‘);
    $redis->sAdd(‘key1‘, ‘val3‘);
    $redis->sAdd(‘key1‘, ‘val4‘);
    $redis->sAdd(‘key2‘, ‘val3‘);
    $redis->sAdd(‘key2‘, ‘val4‘);
    $redis->sAdd(‘key3‘, ‘val3‘);
    $redis->sAdd(‘key3‘, ‘val4‘);
    var_dump($redis->sInter(‘key1‘, ‘key2‘, ‘key3‘));    //array(0=>‘val3‘,1=>‘val4‘)
    
    /* 求交集,并把差集保存在第一个set中
     * @param key set1
     * ...
     * @param key setn
     * @return int 返回交集元素个数或者false
     */
    $redis->sAdd(‘key1‘, ‘val1‘);
    $redis->sAdd(‘key1‘, ‘val2‘);
    $redis->sAdd(‘key1‘, ‘val3‘);
    $redis->sAdd(‘key1‘, ‘val4‘);
    $redis->sAdd(‘key2‘, ‘val3‘);
    $redis->sAdd(‘key2‘, ‘val4‘);
    $redis->sAdd(‘key3‘, ‘val3‘);
    $redis->sAdd(‘key3‘, ‘val4‘);
    var_dump($redis->sInterStore(‘output‘, ‘key1‘, ‘key2‘, ‘key3‘));    //2 
    var_dump($redis->sMembers(‘output‘));    // array(0=>‘val3‘,1=>‘val4‘)
    
    /* 判断value是否在set里面,别名:sContains
     * @param key 
     * @param value 
     * @return bool 包含返回true,失败返回false
     */
    $redis->sAdd(‘key1‘ , ‘member1‘);
    $redis->sAdd(‘key1‘ , ‘member2‘);
    $redis->sAdd(‘key1‘ , ‘member3‘); /* ‘key1‘ => {‘member1‘, ‘member2‘, ‘member3‘}*/
    $redis->sIsMember(‘key1‘, ‘member1‘); /* TRUE */
    $redis->sIsMember(‘key1‘, ‘memberX‘); /* FALSE */
    
    /* 返回一个set
     * @param key set
     * @param array set_val_array
     */
    $redis->delete(‘s‘);
    $redis->sAdd(‘s‘, ‘a‘);
    $redis->sAdd(‘s‘, ‘b‘);
    $redis->sAdd(‘s‘, ‘a‘);
    $redis->sAdd(‘s‘, ‘c‘);
    var_dump($redis->sMembers(‘s‘));    //array(0=>‘c‘,1=>‘a‘,3=>‘b‘)
    
    /* 将set1中元素移动set2中,set1不保留
     * @param key scrkey 
     * @param key deskey 
     * @param value 元素
     * @return bool 成功返回true,失败返回false
     */
     $redis->sAdd(‘key1‘ , ‘member11‘);
    $redis->sAdd(‘key1‘ , ‘member12‘);
    $redis->sAdd(‘key1‘ , ‘member13‘); /* ‘key1‘ => {‘member11‘, ‘member12‘, ‘member13‘}*/
    $redis->sAdd(‘key2‘ , ‘member21‘);
    $redis->sAdd(‘key2‘ , ‘member22‘); /* ‘key2‘ => {‘member21‘, ‘member22‘}*/
    $redis->sMove(‘key1‘, ‘key2‘, ‘member13‘); 
    /* ‘key1‘ =>  {‘member11‘, ‘member12‘} */
    /* ‘key2‘ =>  {‘member21‘, ‘member22‘, ‘member13‘} */
    
    /* 移除并返回随机一个元素从set中
     * @param string key
     * @return bool/string 元素返回值或者false
     */
    $redis->sAdd(‘key1‘ , ‘member1‘);
    $redis->sAdd(‘key1‘ , ‘member2‘);
    $redis->sAdd(‘key1‘ , ‘member3‘); /* ‘key1‘ => {‘member3‘, ‘member2‘, ‘member1‘}*/
    $redis->sPop(‘key1‘); /* ‘member1‘, ‘key1‘ => {‘member3‘, ‘member2‘} */
    $redis->sPop(‘key1‘); /* ‘member3‘, ‘key1‘ => {‘member2‘} */
    
    /* 随机返回一个元素,但是不移除该元素
     * @param key list 
     * @param int(optional) 
     * 如果count是整数且小于元素的个数,返回含有 count
     * 个不同的元素的数组,如果count是个整数且大于集合中元素的个数时,
     * 仅返回整个集合的所有元素,当count是负数,
     * 则会返回一个包含count的绝对值的个数元素的数组,
     * 如果count的绝对值大于元素的个数,则返回的结果集里会出现一个元素出现多次的情况
     * @return string/array/false 如果不提供count参数,则返回元素,如果提供count,
     * 则返回元素数组,如果有误,则返回false
     */
    $redis->sAdd(‘key1‘ , ‘member1‘);
    $redis->sAdd(‘key1‘ , ‘member2‘);
    $redis->sAdd(‘key1‘ , ‘member3‘);     /* ‘key1‘ => {‘member3‘, ‘member1‘, ‘member2‘}*/
    // No count
    $redis->sRandMember(‘key1‘);        /* ‘member1‘, ‘key1‘ => {‘member3‘, ‘member1‘, ‘member2‘} */
    $redis->sRandMember(‘key1‘);         /* ‘member3‘, ‘key1‘ => {‘member3‘, ‘member1‘, ‘member2‘} */
    // With a count
    $redis->sRandMember(‘key1‘, 3);     // Will return an array with all members from the set
    $redis->sRandMember(‘key1‘, 2);     // Will an array with 2 members of the set
    $redis->sRandMember(‘key1‘, -100);     // Will return an array of 100 elements, picked from our set (with dups)
    $redis->sRandMember(‘empty-set‘, 100); // Will return an empty array
    $redis->sRandMember(‘not-a-set‘, 100); // Will return FALSE

 

PHP-Redis扩展使用手册(四)(待续)

标签:

原文地址:http://www.cnblogs.com/pandang/p/4978779.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!