zoukankan      html  css  js  c++  java
  • PHP-Redis扩展使用手册(三)

    /* 序列化key对应的value,如果key不存在则返回false
         * @param key 
         * @return 序列化后的val或者false
         */
        $redis->set('key_1','value_1');
        $redis->dump('key_1');
        
        /* 反序列化key对应的value
         * @param key 
         * @param int ttl ,可以为0,不设置ttl
         * @param val 已通过dump反序列的值
         * @return 成功返回1
         */
        $redis->set('foo', 'bar');
        $val = $redis->dump('foo');
        $redis->restore('bar', 0, $val); // key(bar) - val(bar) 返回1
        
        /* 将一个key移到不同的redis实例中
         * @param string host redis实例ip 
         * @param int port  redis实例端口
         * @param string key 移动的key
         * @param int db_index 要移到redis实例数据库索引
         * @param int timeout 最大移动时间
         * @param bool copy 是否复制
         * @param flag replace 是否替换
         */
        $redis->migrate('backup', 6379, 'foo', 0, 3600);
        $redis->migrate('backup', 6379, 'foo', 0, 3600, true, true); /* copy and replace */
        $redis->migrate('backup', 6379, 'foo', 0, 3600, false, true); /* just REPLACE flag */
    
        /* 向哈希表h添加key-value
         * @param key 哈希表名称
         * @param hashkey key 
         * @param value value 
         * @return bool/int 如果value不存在,插入成功返回1,如果value已经存在,则代替,然后返回0,出错返回false
         */
        $redis->delete('h')
        $redis->hSet('h', 'key1', 'hello'); /* 1, 'key1' => 'hello' in the hash at "h" */
        $redis->hGet('h', 'key1'); /* returns "hello" */
    
        $redis->hSet('h', 'key1', 'plop'); /* 0, value was replaced. */
        $redis->hGet('h', 'key1'); /* returns "plop" */
        
        /* 当key不存在的时候,向哈希表h插入key-value 
         * @param key 
         * @param hashkey 
         * @param value value 
         * @return bool 成功返回true,失败返回false
         */
        $redis->delete('h')
        $redis->hSetNx('h', 'key1', 'hello'); /* TRUE, 'key1' => 'hello' in the hash at "h" */
        $redis->hSetNx('h', 'key1', 'world'); /* FALSE, 'key1' => 'hello' in the hash at "h". No change since the field wasn't replaced. */
        
        /* 获取哈希表中key的value
         * @param string 哈希表 
         * @param string hashkey key 
         * @return string value 
         */
        $redis->delete('h');
        $redis->hSet('h','h_key_1','h_val_1');
        $redis->hGet('h','h_key_1');    // echo h_val_1
        
        /* 返回哈希表的key-value对数
         * @param hash_table 哈希表
         * @return long 返回key-value键值对数量
         */
        $redis->delete('h')
        $redis->hSet('h', 'key1', 'hello');
        $redis->hSet('h', 'key2', 'plop');
        $redis->hLen('h'); /* returns 2 */
        
        /* 充哈希表中删除key-value 
         * @param string hash_table 
         * @param string key 
         * @return bool value 
         */
        $redis->delete('h');
        $redis->hSet('h','h_key_1','h_val_1');
        $redis->hDel('h','h_key_1');
        
        /* 返回哈希表中key,就像array_keys()
         * @param hash_table
         * @return array key的数组
         */
        $redis->delete('h');
        $redis->hSet('h', 'a', 'x');
        $redis->hSet('h', 'b', 'y');
        $redis->hSet('h', 'c', 'z');
        $redis->hSet('h', 'd', 't');
        var_dump($redis->hKeys('h'));
        
        /* 返回哈希表中val
         * @param string hash_table
         * @return array value的数组
         */
        $redis->delete('h');
        $redis->hSet('h', 'a', 'x');
        $redis->hSet('h', 'b', 'y');
        $redis->hSet('h', 'c', 'z');
        $redis->hSet('h', 'd', 't');
        var_dump($redis->hVals('h'));
        
        /* 把整个hash表当做php数组返回
         * @param string hash_table
         * @return array key-value数组
         */
        $redis->delete('h');
        $redis->hSet('h', 'a', 'x');
        $redis->hSet('h', 'b', 'y');
        $redis->hSet('h', 'c', 'z');
        $redis->hSet('h', 'd', 't');
        var_dump($redis->hGetAll('h'));
        
        /* 判断key是否存在哈希表中
         * @param string hash_table
         * @return bool 存在返回true,否则返回false
         */
        $redis->hSet('h', 'a', 'x');
        $redis->hExists('h', 'a'); /*  TRUE */
        $redis->hExists('h', 'NonExistingKey'); /* FALSE */
        
        /* 增加member值到指定的key中
         * @param string hash_table
         * @param string key
         * @param int value 要增加的值 
         */
        $redis->delete('h');
        $redis->hIncrBy('h', 'x', 2); /* returns 2: h[x] = 2 now. */
        $redis->hIncrBy('h', 'x', 1); /* h[x] ← 2 + 1. Returns 3 */
        
        /* 增加member值到指定的key中
         * @param string hash_table
         * @param string key
         * @param float value 要增加的值 
         */
        $redis->delete('h');
        $redis->hIncrByFloat('h','x', 1.5); /* returns 1.5: h[x] = 1.5 now */
        $redis->hIncrByFLoat('h', 'x', 1.5); /* returns 3.0: h[x] = 3.0 now */
        $redis->hIncrByFloat('h', 'x', -3.0); /* returns 0.0: h[x] = 0.0 now */
        
        /* 一次设置多个key-value到哈希表中
         * @param string hash_table
         * @param key-value 数组
         * @return bool 
         */
        $redis->delete('user:1');
        $redis->hMset('user:1', array('name' => 'Joe', 'salary' => 2000));
        
        /* 从哈希表中获取多个key-value
         * @param string 哈希表
         * @param key-array 
         * @return value-array 
         */
        $redis->delete('h');
        $redis->hSet('h', 'field1', 'value1');
        $redis->hSet('h', 'field2', 'value2');
        $redis->hmGet('h', array('field1', 'field2')); /* returns array('field1' => 'value1', 'field2' => 'value2') */
        
        /* 遍历哈希表
         * @param string hash_table 
         * @param pattern
         */
        $it = NULL;
        $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
        while($arr_keys = $redis->hscan('hash', $it)) {
            foreach($arr_keys as $str_field => $str_value) {
                echo "$str_field => $str_value
    "; 
            }
        }
        
        /* 阻塞版本,弹出列表的最新元素,如果列表都为空,则等到有元素进入或者超时才返回
         * @param array(list1,list2,...) or list1,list2,list3,,,
         * @param int timeout 设置超时时间
         * @return array array(listname,value) 列表key和头部元素值
         */
        $redis->lpush('list1','list_val_1');
        $redis->lpush('list1','list_val_2');
        $redis->lpush('list1','list_val_3');
        $redis->delete('list2');
        $redis->blpop('list1',10);            //array('list1','list_val_3')
        $redis->blpop('list1','list2',10);    //array('list1','list_val_2')
        $redis->blpop('list3',10);    //超时返回空数组array()
        
        /* 功能查考blpop,但是brpop是弹出列表的最早元素
         * @param array(list1,list2,...) or list1,list2,list3,,,
         * @param int timeout 设置超时时间,单位为秒
         * @return array array(listname,value) 列表key和头部元素值,不存在返回空数组
         */
        $redis->lpush('list1','list_val_1');
        $redis->lpush('list1','list_val_2');
        $redis->lpush('list1','list_val_3');
        $redis->lpush('list1','list_val_4');
        $redis->delete('list2');
        $redis->brpop('list1',10);            //array('list1','list_val_1')
        $redis->brpop('list1','list2',10);    //array('list1','list_val_2')
        $redis->brpop('list3',10);            //array()
        
        /* 从srckey列表中弹出尾部元素,并把改元素压入dstkey列表中,返回改元素值
         * @param key srckey 弹出元素列表
         * @param key dstkey 压入元素列表
         * @return string 返回改元素值或者false
         */
        $redis->delete('x', 'y');
        $redis->lPush('x', 'abc');
        $redis->lPush('x', 'def');
        $redis->lPush('y', '123');
        $redis->lPush('y', '456');
        $redis->rpoplpush('x', 'y');    //abc 
        $redis->lRange('x', 0, -1);        //array('def')
        $redis->lRange('y', 0, -1);        //array('abc','456','123')
        
        /* 阻塞版本rpoplpush,详情参考rpoplpush
         * @param key srckey 弹出元素列表
         * @param key dstkey 压入元素列表
         * @param int timeout 超时时间
         * @return string 返回改元素值或者false
         */
        $redis->delete('x', 'y');
        $redis->lPush('x', 'abc');
        $redis->lPush('x', 'def');
        $redis->lPush('y', '123');
        $redis->lPush('y', '456');
        $redis->brpoplpush('x', 'y',10);    //abc 
        $redis->lRange('x', 0, -1);            //array('def')
        $redis->lRange('y', 0, -1);            //array('abc','456','123')
        
        /* 获取列表中对应index下标的元素值,IIndex函数一样
         * @key list 列表名
         * @index int 下标,如果为负数,这是倒数第index个元素
         * @return string/bool 成功返回val,失败返回false,包括越界 
         */
        $redis->rPush('key1', 'A');
        $redis->rPush('key1', 'B');
        $redis->rPush('key1', 'C'); /* key1 => [ 'A', 'B', 'C' ] */
        $redis->lGet('key1', 0); /* 'A' */
        $redis->lGet('key1', -1); /* 'C' */
        $redis->lGet('key1', 10); /* `FALSE` */
        
        /* 插入元素,如果列表不存在这不插入
         * @param key 列表key
         * @param position Redis::BEFORE/Redis::AFTER 
         * @param 相对位置的值value,在该值之前或之后
         * @param value 待插入的值
         */
        $redis->delete('key1');
        $redis->lInsert('key1', Redis::AFTER, 'A', 'X');     /* 0 */
        $redis->lPush('key1', 'A');
        $redis->lPush('key1', 'B');
        $redis->lPush('key1', 'C');
        $redis->lInsert('key1', Redis::BEFORE, 'C', 'X');     /* 4 */
        $redis->lRange('key1', 0, -1);                         /* array('A', 'B', 'X', 'C') */
        $redis->lInsert('key1', Redis::AFTER, 'C', 'Y');     /* 5 */
        $redis->lRange('key1', 0, -1);                         /* array('A', 'B', 'X', 'C', 'Y') */
        $redis->lInsert('key1', Redis::AFTER, 'W', 'value'); /* -1 */
        
        /* 弹出列表头元素
         * @param 列表 
         * @return string/bool 返回头元素或者false
         */
        $redis->rPush('key1', 'A');
        $redis->rPush('key1', 'B');
        $redis->rPush('key1', 'C'); /* key1 => [ 'A', 'B', 'C' ] */
        $redis->lPop('key1'); /* key1 => [ 'B', 'C' ] */
         
        /* 压入元素到列表尾部,列表如果不存在则创建列表插入
         * @param key 列表key
         * @param string 元素
         * @param long/bool 成功返回列表新长度,失败返回false
         */
        $redis->delete('key1');
        $redis->lPush('key1', 'C'); // returns 1
        $redis->lPush('key1', 'B'); // returns 2
        $redis->lPush('key1', 'A'); // returns 3
        /* key1 now points to the following list: [ 'A', 'B', 'C' ] */
        
        /* 参考lpush,如果列表不存在,则不操作
         * @param key 列表key
         * @param string 元素
         * @param long/bool 成功返回列表新长度,失败返回false
         */
        $redis->delete('key1');
        $redis->lPushx('key1', 'A'); // returns 0
        $redis->lPush('key1', 'A'); // returns 1
        $redis->lPushx('key1', 'B'); // returns 2
        $redis->lPushx('key1', 'C'); // returns 3
        /* key1 now points to the following list: [ 'A', 'B', 'C' ] */
        
        /* 获取列表指定范围元素,lGetRange别名
         * @param list 列表key 
         * @param int start_index 
         * @param int end_index 
         * @return array 
         */
        $redis->rPush('key1', 'A');
        $redis->rPush('key1', 'B');
        $redis->rPush('key1', 'C');
        $redis->lRange('key1', 0, -1); /* array('A', 'B', 'C')  第一个到倒数第一个元素 */
        
        /* 移除第一个值为value的元素,如果count为0,则全部移除值为value的元素
         * 如果值为正数,则从头到尾移除count个,如果值为负数,则从尾部到头部移除count个
         * 别名: lRemove
         * @param key 列表
         * @param value 值
         * @param count 个数
         * @return long/bool 成功返回移除个数,失败返回false
         */
        $redis->lPush('key1', 'A');
        $redis->lPush('key1', 'B');
        $redis->lPush('key1', 'C'); 
        $redis->lPush('key1', 'A'); 
        $redis->lPush('key1', 'A'); 
        $redis->lRange('key1', 0, -1);         /* array('A', 'A', 'C', 'B', 'A') */
        $redis->lRem('key1', 'A', 2);         /* 2 */
        $redis->lRange('key1', 0, -1);         /* array('C', 'B', 'A') */
        
        /* 设置列表index下标的值
         * @param string key 
         * @param int index下标
         * @param string value 
         * @return bool 成功返回true,失败返回false
         */
        $redis->rPush('key1', 'A');
        $redis->rPush('key1', 'B');
        $redis->rPush('key1', 'C'); /* key1 => [ 'A', 'B', 'C' ] */
        $redis->lGet('key1', 0); /* 'A' */
        $redis->lSet('key1', 0, 'X');
        $redis->lGet('key1', 0); /* 'X' */ 
        
        /* 剪裁列表,不在范围中元素会被删除
         * @param string list 
         * @param int start_index 
         * @param int stop_index 
         * @param array/bool 如果失败返回false 
         */
        $redis->rPush('key1', 'A');
        $redis->rPush('key1', 'B');
        $redis->rPush('key1', 'C');
        $redis->lRange('key1', 0, -1); /* array('A', 'B', 'C') */
        $redis->lTrim('key1', 0, 1);
        $redis->lRange('key1', 0, -1); /* array('A', 'B') */
        
        /* 返回和移除列表中最新的元素
         * @param string list 
         * @return string/bool 成功返回元素val,失败返回false
         */
        $redis->rPush('key1', 'A');
        $redis->rPush('key1', 'B');
        $redis->rPush('key1', 'C'); /* key1 => [ 'A', 'B', 'C' ] */
        $redis->rPop('key1'); /* key1 => [ 'A', 'B' ] */
        
        /* 添加一个string元素到列表尾部,如果列表不存在,则创建列表
         * 如果添加到的容器不是列表,则返回false
         * @param string list
         * @param string value 
         * @return int/bool 成功返回列表新长度,失败返回false
         */
        $redis->delete('key1');
        $redis->rPush('key1', 'A'); // returns 1
        $redis->rPush('key1', 'B'); // returns 2
        $redis->rPush('key1', 'C'); // returns 3
        /* key1 now points to the following list: [ 'A', 'B', 'C' ] */
        
        /* 参考rPush,如果列表不存在,则不创建, rPushx
         * @param string list
         * @param string value 
         * @return int/bool 成功返回列表新长度,失败返回false
         */
        $redis->delete('key1');
        $redis->rPushx('key1', 'A');     // returns 0
        $redis->rPush('key1', 'A');     // returns 1
        $redis->rPushx('key1', 'B');     // returns 2
        $redis->rPushx('key1', 'C');     // returns 3
        /* key1 now points to the following list: [ 'A', 'B', 'C' ] */
        
        /* 获取列表长度
         * @param string list 
         * @return int 返回列表长度
         */
        $redis->rPush('key1', 'A');
        $redis->rPush('key1', 'B');
        $redis->rPush('key1', 'C');     /* key1 => [ 'A', 'B', 'C' ] */
        $redis->lSize('key1');            /* 3 */
        $redis->rPop('key1'); 
        $redis->lSize('key1');            /* 2 */
  • 相关阅读:
    可爱的中国电信 请问我们的电脑还属于我们自己吗?
    了解客户的需求,写出的代码或许才是最优秀的............
    DELPHI DATASNAP 入门操作(3)简单的主从表的简单更新【含简单事务处理】
    用数组公式获取字符在字符串中最后出现的位置
    在ehlib的DBGridEh控件中使用过滤功能(可以不用 MemTableEh 控件 适用ehlib 5.2 ehlib 5.3)
    格式化json返回的时间
    ExtJs中使用Ajax赋值给全局变量异常解决方案
    java compiler level does not match the version of the installed java project facet (转)
    收集的资料(六)ASP.NET编程中的十大技巧
    收集的资料共享出来(五)Asp.Net 权限解决办法
  • 原文地址:https://www.cnblogs.com/pandang/p/4945091.html
Copyright © 2011-2022 走看看