Home > php教程 > php手册 > body text

Redis PHP operation manual

WBOY
Release: 2016-08-04 08:53:13
Original
1467 people have browsed it

String type operation

String is the most basic type of redis, and the string type is binary safe. This means that the redis string can contain any data. For example, jpg images or serialized objects

<span style="font-size: 15px;"><code><span class="hljs-variable">$redis-><span class="hljs-built_in">set(<span class="hljs-string">'key',<span class="hljs-string">'TK');
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis-><span class="hljs-built_in">set(<span class="hljs-string">'number',<span class="hljs-string">'1');
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->setex(<span class="hljs-string">'key',<span class="hljs-number">5,<span class="hljs-string">'TK'); <span class="hljs-comment">//设置有效期为5秒的键值
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->psetex(<span class="hljs-string">'key',<span class="hljs-number">5000,<span class="hljs-string">'TK'); <span class="hljs-comment">//设置有效期为5000毫秒(同5秒)的键值
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->setnx(<span class="hljs-string">'key',<span class="hljs-string">'XK'); <span class="hljs-regexp">//若键值存在返回<span class="hljs-keyword">false 不存在返回<span class="hljs-keyword">true
</span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->delete(<span class="hljs-string">'key'); 删除键值 可以传入数组 <span class="hljs-keyword">array(<span class="hljs-string">'key1',<span class="hljs-string">'key2')删除多个键
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->getSet(<span class="hljs-string">'key',<span class="hljs-string">'XK'); <span class="hljs-comment">//将键key的值设置为XK, 并返回这个键值原来的值TK
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code> <span class="hljs-variable">$ret = <span class="hljs-variable">$redis->multi()  <span class="hljs-comment">//批量事务处理,不保证处理数据的原子性
        ->set(<span class="hljs-string">'key1', <span class="hljs-string">'val1')
        ->get(<span class="hljs-string">'key1')
        ->setnx(<span class="hljs-string">'key', <span class="hljs-string">'val2')
        ->get(<span class="hljs-string">'key2')
        ->exec();
</span></span></span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->watch(<span class="hljs-string">'key');   // 监控键key 是否被其他客户端修改
                           如果KEY在调用watch()和<span class="hljs-built_in">exec()之间被修改,<span class="hljs-built_in">exec失败
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">f<span class="hljs-params">(<span class="hljs-variable">$redis, <span class="hljs-variable">$chan, <span class="hljs-variable">$msg) {  <span class="hljs-comment">//频道订阅
    <span class="hljs-keyword">switch(<span class="hljs-variable">$chan) {
        <span class="hljs-keyword">case <span class="hljs-string">'chan-1':
            <span class="hljs-keyword">echo <span class="hljs-variable">$msg;
            <span class="hljs-keyword">break;

        <span class="hljs-keyword">case <span class="hljs-string">'chan-2':
            <span class="hljs-keyword">echo <span class="hljs-variable">$msg;
            <span class="hljs-keyword">break;

        <span class="hljs-keyword">case <span class="hljs-string">'chan-2':
            <span class="hljs-keyword">echo <span class="hljs-variable">$msg;
            <span class="hljs-keyword">break;
    }
}

<span class="hljs-variable">$redis->subscribe(<span class="hljs-keyword">array(<span class="hljs-string">'chan-1', <span class="hljs-string">'chan-2', <span class="hljs-string">'chan-3'), <span class="hljs-string">'f'); <span class="hljs-comment">// subscribe to 3 chans

<span class="hljs-variable">$redis->publish(<span class="hljs-string">'chan-1', <span class="hljs-string">'hello, world!'); <span class="hljs-comment">// send message. 
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->exists(<span class="hljs-string">'key'); <span class="hljs-regexp">//验证键是否存在,存在返回<span class="hljs-keyword">true
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->incr(<span class="hljs-string">'number'); <span class="hljs-comment">//键值加1
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->incrby(<span class="hljs-string">'number',-<span class="hljs-number">10); <span class="hljs-comment">//键值加减10
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->incrByFloat(<span class="hljs-string">'number', +/- <span class="hljs-number">1.5); <span class="hljs-comment">//键值加减小数
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->decr(<span class="hljs-string">'number'); <span class="hljs-comment">// 键值减1
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->decrBy(<span class="hljs-string">'number',<span class="hljs-number">10); <span class="hljs-comment">// 键值减10
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$mget = <span class="hljs-variable">$redis->mget(<span class="hljs-keyword">array(<span class="hljs-string">'number',<span class="hljs-string">'key')); <span class="hljs-comment">// 批量获取键值,返回一个数组
</span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->mset(<span class="hljs-keyword">array(<span class="hljs-string">'key0' => <span class="hljs-string">'value0', <span class="hljs-string">'key1' => <span class="hljs-string">'value1')); <span class="hljs-comment">// 批量设置键值
</span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->msetnx(<span class="hljs-keyword">array(<span class="hljs-string">'key0' => <span class="hljs-string">'value0', <span class="hljs-string">'key1' => <span class="hljs-string">'value1')); 
                                        <span class="hljs-comment">// 批量设置键值,类似将setnx()方法批量操作
</span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->append(<span class="hljs-string">'key', <span class="hljs-string">'-Smudge'); <span class="hljs-comment">//原键值TK,将值追加到键值后面,键值为TK-Smudge
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->getRange(<span class="hljs-string">'key', <span class="hljs-number">0, <span class="hljs-number">5); <span class="hljs-comment">// 键值截取从0位置开始到5位置结束
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->getRange(<span class="hljs-string">'key', -<span class="hljs-number">6, -<span class="hljs-number">1); <span class="hljs-comment">// 字符串截取从-6(倒数第6位置)开始到-1(倒数第1位置)结束
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->setRange(<span class="hljs-string">'key', <span class="hljs-number">0, <span class="hljs-string">'Smudge'); 
                                    <span class="hljs-comment">// 键值中替换字符串,0表示从0位置开始
                                       有多少个字符替换多少位置,其中汉字占<span class="hljs-number">2个位置
</span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->strlen(<span class="hljs-string">'key'); <span class="hljs-comment">//键值长度
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->getBit(<span class="hljs-string">'key');
</span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis-><span class="hljs-built_in">setBit(<span class="hljs-string">'key');
</span></span></span></code></span>
Copy after login

list operation

<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->delete(<span class="hljs-string">'list-key'); <span class="hljs-comment">// 删除链表
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->lPush(<span class="hljs-string">'list-key', <span class="hljs-string">'A'); <span class="hljs-comment">//插入链表头部/左侧,返回链表长度
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->rPush(<span class="hljs-string">'list-key', <span class="hljs-string">'B'); <span class="hljs-comment">//插入链表尾部/右侧,返回链表长度
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->lPushx(<span class="hljs-string">'list-key', <span class="hljs-string">'C'); 
                 <span class="hljs-comment">// 插入链表头部/左侧,链表不存在返回0,存在即插入成功,返回当前链表长度
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->rPushx(<span class="hljs-string">'list-key', <span class="hljs-string">'C'); 
                 <span class="hljs-comment">// 插入链表尾部/右侧,链表不存在返回0,存在即插入成功,返回当前链表长度
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->lPop(<span class="hljs-string">'list-key'); <span class="hljs-comment">//返回LIST顶部(左侧)的VALUE ,后入先出(栈)
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->rPop(<span class="hljs-string">'list-key'); <span class="hljs-comment">//返回LIST尾部(右侧)的VALUE ,先入先出(队列)
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->blPop();
</span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->brPop();
</span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->lSize(<span class="hljs-string">'list-key'); 
                    <span class="hljs-comment">// 如果是链表则返回链表长度,空链表返回0 
                       若不是链表或者不为空,则返回<span class="hljs-keyword">false ,判断非链表 <span class="hljs-string">" === false "                          
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->lGet(<span class="hljs-string">'list-key',-<span class="hljs-number">1); <span class="hljs-comment">// 通过索引获取链表元素 0获取左侧一个  -1获取最后一个
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->lSet(<span class="hljs-string">'list-key', <span class="hljs-number">0, <span class="hljs-string">'X'); <span class="hljs-comment">//0位置元素替换为 X
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->lRange(<span class="hljs-string">'list-key', <span class="hljs-number">0, <span class="hljs-number">3); 
                    <span class="hljs-comment">//链表截取 从0开始 3位置结束 ,结束位置为-1 获取开始位置之后的全部
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->lTrim(<span class="hljs-string">'list-key', <span class="hljs-number">0, <span class="hljs-number">1); <span class="hljs-comment">// 截取链表(不可逆) 从0索引开始 1索引结束 
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->lRem(<span class="hljs-string">'list-key', <span class="hljs-string">'C', <span class="hljs-number">2); <span class="hljs-comment">//链表从左开始删除元素2个C
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->lInsert(<span class="hljs-string">'list-key', Redis::BEFORE, <span class="hljs-string">'C', <span class="hljs-string">'X'); 
                    <span class="hljs-comment">// 在C元素前面插入X  , Redis::AfTER(表示后面插入) 
                       链表不存在则插入失败 返回<span class="hljs-number">0 若元素不存在返回-<span class="hljs-number">1
</span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->rpoplpush(<span class="hljs-string">'list-key', <span class="hljs-string">'list-key2'); 
                    <span class="hljs-comment">//从源LIST的最后弹出一个元素
                      并且把这个元素从目标<span class="hljs-keyword">LIST的顶部(左侧)压入目标<span class="hljs-keyword">LIST。 
</span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code>
<span class="hljs-variable">$redis->brpoplpush();
                    <span class="hljs-comment">//rpoplpush的阻塞版本,这个版本有第三个参数用于设置阻塞时间
                      即如果源<span class="hljs-keyword">LIST为空,那么可以阻塞监听timeout的时间,如果有元素了则执行操作。
</span></span></span></code></span>
Copy after login

Set collection type

<span style="font-size: 15px;"><code><span class="hljs-keyword">set无序集合 不允许出现重复的元素 服务端可以实现多个 集合操作
</span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->sMembers(<span class="hljs-string">'key'); <span class="hljs-comment">//获取容器key中所有元素
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->sAdd(<span class="hljs-string">'key' , <span class="hljs-string">'TK');
                 <span class="hljs-regexp">// (从左侧插入,最后插入的元素在<span class="hljs-number">0位置),集合中已经存在<span class="hljs-constant">TK 则返回<span class="hljs-keyword">false 
                     不存在添加成功 返回<span class="hljs-keyword">true
</span></span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->sRem(<span class="hljs-string">'key' , <span class="hljs-string">'TK'); <span class="hljs-comment">// 移除容器中的TK
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->sMove(<span class="hljs-string">'key',<span class="hljs-string">'key1',<span class="hljs-string">'TK'); <span class="hljs-comment">//将容易key中的元素TK 移动到容器key1  操作成功返回TRUE
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->sIsMember(<span class="hljs-string">'key',<span class="hljs-string">'TK'); <span class="hljs-comment">//检查VALUE是否是SET容器中的成员
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->sCard(<span class="hljs-string">'key'); <span class="hljs-comment">//返回SET容器的成员数
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->sPop(<span class="hljs-string">'key'); <span class="hljs-comment">//随机返回容器中一个元素,并移除该元素
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->sRandMember(<span class="hljs-string">'key');<span class="hljs-comment">//随机返回容器中一个元素,不移除该元素
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->sInter(<span class="hljs-string">'key',<span class="hljs-string">'key1'); 
     <span class="hljs-comment">// 返回两个集合的交集 没有交集返回一个空数组,若参数只有一个集合,则返回集合对应的完整的数组
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->sInterStore(<span class="hljs-string">'store',<span class="hljs-string">'key',<span class="hljs-string">'key1'); <span class="hljs-comment">//将集合key和集合key1的交集 存入容器store 成功返回1
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->sUnion(<span class="hljs-string">'key',<span class="hljs-string">'key1'); <span class="hljs-comment">//集合key和集合key1的并集  注意即使多个集合有相同元素 只保留一个

<span class="hljs-variable">$redis->sUnionStore(<span class="hljs-string">'store',<span class="hljs-string">'key',<span class="hljs-string">'key1'); 
            <span class="hljs-comment">//集合key和集合key1的并集保存在集合store中,  注意即使多个集合有相同元素 只保留一个
</span></span></span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->sDiff(<span class="hljs-string">'key',<span class="hljs-string">'key1',<span class="hljs-string">'key2'); <span class="hljs-comment">//返回数组,该数组元素是存在于key集合而不存在于集合key1 key2
</span></span></span></span></span></code></span>
Copy after login

Zset data type

**(stored set) is a collection of strings like set. The difference is that each element is associated with a double type score
The list type of redis is actually a doubly linked list in which each sub-element is of string type. . **

<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zAdd(<span class="hljs-string">'tkey', <span class="hljs-number">1, <span class="hljs-string">'A'); 
                           <span class="hljs-comment">//  插入集合tkey中,A元素关联一个分数,插入成功返回1
                               同时集合元素不可以重复, 如果元素已经存在返回 <span class="hljs-number">0
</span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zRange(<span class="hljs-string">'tkey',<span class="hljs-number">0,-<span class="hljs-number">1); <span class="hljs-comment">// 获取集合元素,从0位置 到 -1 位置
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zRange(<span class="hljs-string">'tkey',<span class="hljs-number">0,-<span class="hljs-number">1, <span class="hljs-keyword">true); 
                    <span class="hljs-comment">// 获取集合元素,从0位置 到 -1 位置, 返回一个关联数组 带分数 
                      <span class="hljs-keyword">array([A] => <span class="hljs-number">0.01,[B] => <span class="hljs-number">0.02,[D] => <span class="hljs-number">0.03) 其中小数来自zAdd方法第二个参数
</span></span></span></span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zDelete(<span class="hljs-string">'tkey', <span class="hljs-string">'B'); <span class="hljs-comment">// 移除集合tkey中元素B  成功返回1 失败返回 0
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zRevRange(<span class="hljs-string">'tkey', <span class="hljs-number">0, -<span class="hljs-number">1); <span class="hljs-comment">// 获取集合元素,从0位置 到 -1 位置,数组按照score降序处理

<span class="hljs-variable">$redis->zRevRange(<span class="hljs-string">'tkey', <span class="hljs-number">0, -<span class="hljs-number">1,<span class="hljs-keyword">true); 
                <span class="hljs-comment">// 获取集合元素,从0位置 到 -1 位置,数组按照score降序处理 返回score关联数组
</span></span></span></span></span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zRangeByScore(<span class="hljs-string">'tkey', <span class="hljs-number">0, <span class="hljs-number">0.2,<span class="hljs-keyword">array(<span class="hljs-string">'withscores' => <span class="hljs-keyword">true)); 
            <span class="hljs-comment">//获取几个tkey中score在区间[0,0.2]元素 ,score由低到高排序,
                元素具有相同的score,那么会按照字典顺序排列 , withscores 控制返回关联数组
</span></span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zRangeByScore(<span class="hljs-string">'tkey', <span class="hljs-number">0.1, <span class="hljs-number">0.36, <span class="hljs-keyword">array(<span class="hljs-string">'withscores' => <span class="hljs-keyword">TRUE, <span class="hljs-string">'limit' => <span class="hljs-keyword">array(<span class="hljs-number">0, <span class="hljs-number">1)));
             <span class="hljs-comment">//其中limit中 0和1 表示取符合条件集合中 从0位置开始,向后扫描1个 返回关联数组
</span></span></span></span></span></span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zCount(<span class="hljs-string">'tkey', <span class="hljs-number">2, <span class="hljs-number">10); <span class="hljs-comment">// 获取tkey中score在区间[2, 10]元素的个数
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code>$redis->zRemRangeByScore('tkey', 1, 3); // 移除tkey中score在区间[<span class="hljs-link_label">1, 3](<span class="hljs-link_url">含边界)的元素
</span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zRemRangeByRank(<span class="hljs-string">'tkey', <span class="hljs-number">0, <span class="hljs-number">1); 
                         <span class="hljs-comment">//默认元素score是递增的,移除tkey中元素 从0开始到-1位置结束
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zSize(<span class="hljs-string">'tkey');  <span class="hljs-comment">//返回存储在key对应的有序集合中的元素的个数
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zScore(<span class="hljs-string">'tkey', <span class="hljs-string">'A'); <span class="hljs-comment">// 返回集合tkey中元素A的score值
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zRank(<span class="hljs-string">'tkey', <span class="hljs-string">'A'); 
                      <span class="hljs-comment">// 返回集合tkey中元素A的索引值 
                         z集合中元素按照score从低到高进行排列 ,即最低的score index索引为<span class="hljs-number">0
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zIncrBy(<span class="hljs-string">'tkey', <span class="hljs-number">2.5, <span class="hljs-string">'A'); <span class="hljs-comment">// 将集合tkey中元素A的score值 加 2.5
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zUnion(<span class="hljs-string">'union', <span class="hljs-keyword">array(<span class="hljs-string">'tkey', <span class="hljs-string">'tkey1')); 
        <span class="hljs-comment">// 将集合tkey和集合tkey1元素合并于集合union , 并且新集合中元素不能重复
           返回新集合的元素个数, 如果元素A在tkey和tkey1都存在,则合并后的元素A的score相加
</span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zUnion(<span class="hljs-string">'ko2', <span class="hljs-keyword">array(<span class="hljs-string">'k1', <span class="hljs-string">'k2'), <span class="hljs-keyword">array(<span class="hljs-number">5, <span class="hljs-number">2)); 
        <span class="hljs-comment">// 集合k1和集合k2并集于k02 ,array(5,1)中元素的个数与子集合对应,然后 5 对应k1 
           k1每个元素score都要乘以<span class="hljs-number">5 ,同理<span class="hljs-number">1对应k2,k2每个元素score乘以<span class="hljs-number">1 
           然后元素按照递增排序,默认相同的元素score(SUM)相加
</span></span></span></span></span></span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zUnion(<span class="hljs-string">'ko2', <span class="hljs-keyword">array(<span class="hljs-string">'k1', <span class="hljs-string">'k2'), <span class="hljs-keyword">array(<span class="hljs-number">10, <span class="hljs-number">2),<span class="hljs-string">'MAX'); 
        <span class="hljs-comment">// 各个子集乘以因子之后,元素按照递增排序,相同的元素的score取最大值(MAX)
           也可以设置MIN 取最小值
</span></span></span></span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zInter(<span class="hljs-string">'ko1', <span class="hljs-keyword">array(<span class="hljs-string">'k1', <span class="hljs-string">'k2')); 
        <span class="hljs-comment">// 集合k1和集合k2取交集于k01 ,且按照score值递增排序
           如果集合元素相同,则新集合中的元素的score值相加
</span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zInter(<span class="hljs-string">'ko1', <span class="hljs-keyword">array(<span class="hljs-string">'k1', <span class="hljs-string">'k2'), <span class="hljs-keyword">array(<span class="hljs-number">5, <span class="hljs-number">1)); 
        <span class="hljs-comment">//集合k1和集合k2取交集于k01 ,array(5,1)中元素的个数与子集合对应,然后 5 对应k1 
          k1每个元素score都要乘以<span class="hljs-number">5 ,同理<span class="hljs-number">1对应k2,k2每个元素score乘以<span class="hljs-number">1 
          ,然后元素score按照递增排序,默认相同的元素score(SUM)相加
</span></span></span></span></span></span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->zInter(<span class="hljs-string">'ko1', <span class="hljs-keyword">array(<span class="hljs-string">'k1', <span class="hljs-string">'k2'), <span class="hljs-keyword">array(<span class="hljs-number">5, <span class="hljs-number">1),<span class="hljs-string">'MAX'); 
        <span class="hljs-comment">// 各个子集乘以因子之后,元素score按照递增排序,相同的元素score取最大值(MAX)
           也可以设置MIN 取最小值
</span></span></span></span></span></span></span></span></span></span></code></span>
Copy after login

Hash data type

redis hash is a mapping table of string type fields and values. Its addition and deletion operations are O(1) (on average). Hash is particularly suitable for storing objects.

<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->hSet(<span class="hljs-string">'h', <span class="hljs-string">'name', <span class="hljs-string">'TK'); <span class="hljs-comment">// 在h表中 添加name字段 value为TK
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->hSetNx(<span class="hljs-string">'h', <span class="hljs-string">'name', <span class="hljs-string">'TK');
         <span class="hljs-regexp">// 在h表中 添加name字段 value为<span class="hljs-constant">TK 如果字段name的value存在返回<span class="hljs-keyword">false 否则返回 <span class="hljs-keyword">true
</span></span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->hGet(<span class="hljs-string">'h', <span class="hljs-string">'name'); <span class="hljs-comment">// 获取h表中name字段value
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->hLen(<span class="hljs-string">'h'); <span class="hljs-comment">// 获取h表长度即字段的个数
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->hDel(<span class="hljs-string">'h',<span class="hljs-string">'email'); <span class="hljs-comment">// 删除h表中email 字段
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->hKeys(<span class="hljs-string">'h'); <span class="hljs-comment">// 获取h表中所有字段
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->hVals(<span class="hljs-string">'h'); <span class="hljs-comment">// 获取h表中所有字段value
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->hGetAll(<span class="hljs-string">'h'); <span class="hljs-comment">// 获取h表中所有字段和value 返回一个关联数组(字段为键值)
</span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->hExists(<span class="hljs-string">'h', <span class="hljs-string">'email'); <span class="hljs-regexp">//判断email 字段是否存在与表h 不存在返回<span class="hljs-keyword">false
</span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->hSet(<span class="hljs-string">'h', <span class="hljs-string">'age', <span class="hljs-number">28);
</span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->hIncrBy(<span class="hljs-string">'h', <span class="hljs-string">'age', -<span class="hljs-number">2); 
 <span class="hljs-regexp">// 设置h表中age字段value加(-<span class="hljs-number">2) 如果value是个非数值 则返回<span class="hljs-keyword">false 否则,返回操作后的value
</span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->hIncrByFloat(<span class="hljs-string">'h', <span class="hljs-string">'age', -<span class="hljs-number">0.<span class="hljs-number">33);  
        <span class="hljs-regexp">// 设置h表中age字段value加(-<span class="hljs-number">2.6) 如果value是个非数值 则返回<span class="hljs-keyword">false 否则
           返回操作后的value(小数点保留<span class="hljs-number">15位)
</span></span></span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->hMset(<span class="hljs-string">'h', <span class="hljs-keyword">array(<span class="hljs-string">'score' => <span class="hljs-string">'80', <span class="hljs-string">'salary' => <span class="hljs-number">2000)); <span class="hljs-comment">// 表h 批量设置字段和value
</span></span></span></span></span></span></span></span></code></span>
Copy after login
<span style="font-size: 15px;"><code><span class="hljs-variable">$redis->hMGet(<span class="hljs-string">'h', <span class="hljs-keyword">array(<span class="hljs-string">'score',<span class="hljs-string">'salary')); <span class="hljs-comment">// 表h 批量获取字段的value</span></span></span></span></span></span></code></span>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template