🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# string ``` <pre class="calibre29">``` Strings <span class="token">//append -向指定的key追加值</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">set</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token2">'hello'</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">append</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token2">'world'</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">get</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//'helloworld'</span> <span class="token">//setBit(key,offset,value) - 更改字符串(二进制)的单个位 (offset:位偏移 value:布尔、0或者1) 返回设置之前的位的值(0或者1)</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">set</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token2">"*"</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// ord("*") = 42(ASCII) = 0x2f(16进制) = 00101010(二进制)</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setBit</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token6">5</span><span class="token3">,</span> <span class="token6">1</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 0 (00101110)</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setBit</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token6">7</span><span class="token3">,</span> <span class="token6">1</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 0 (00101111)</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">get</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// "/"</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">getBit</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token6">5</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//1 返回指定某个键偏移量的值</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">bitCount</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//计算key 所储存的字符串值中,被设置为 1 的比特位的数量</span> <span class="token">/* setBit 与bitCount实现用户上线次数统计等计数 例子:记录用户上线了多少天 首先我们要知道网站的上线时间,没过一天则位增加一个(8个才一个字节) 如今天是网站上线的101天 那么我们以用户名为key 网站上线的天数为offset位偏移 用户一上线就将位偏移设置为1 */</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setBit</span><span class="token3">(</span>tom<span class="token3">,</span> <span class="token6">100</span><span class="token3">,</span> <span class="token6">1</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//在网站上线的100天的那天登录过(昨天)</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setBit</span><span class="token3">(</span>tom<span class="token3">,</span> <span class="token6">101</span><span class="token3">,</span> <span class="token6">1</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//在网站上线的101天的那天登录过(今天)</span> <span class="token">//然后我们通过 bitCount统计tom的二进制位偏移有多少个1 (有多少个1就上线了几天)</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">bitCount</span><span class="token3">(</span><span class="token2">'tom'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//tom上线了多少天</span> <span class="token">//bitOp - 在多个键上按位运算(redis现支持的按位操录作符有: "AND", "OR", "NOT", "XOR") BITOP可能是一个缓慢的命令容易阻塞</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">set</span><span class="token3">(</span><span class="token2">'k1'</span><span class="token3">,</span> <span class="token2">'foobar'</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">set</span><span class="token3">(</span><span class="token2">'k2'</span><span class="token3">,</span> <span class="token2">'abcdef'</span><span class="token3">)</span><span class="token3">;</span> $a<span class="token1">=</span>$redis<span class="token1">-</span><span class="token1">></span><span class="token4">bitOp</span><span class="token3">(</span><span class="token2">'and'</span><span class="token3">,</span><span class="token2">'newkey'</span><span class="token3">,</span><span class="token2">'k1'</span><span class="token3">,</span><span class="token2">'k2'</span><span class="token3">)</span><span class="token3">;</span><span class="token">//6 `bc`ab</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">get</span><span class="token3">(</span><span class="token2">'newkey'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//`bc`ab</span> <span class="token">/*过程: 1100110 1101111 1101111 1100010 1100001 1110010 1100001 1100010 1100011 1100100 1100101 1100110 按位与(and)之后: 1100000 1100010 1100011 1100000 1100001 1100010 (`bc`ab) */</span> <span class="token">//计算网站每日活跃用户(登录了多少用户)</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setBit</span><span class="token3">(</span>active01<span class="token3">,</span> userid<span class="token1">=</span><span class="token6">1</span><span class="token3">,</span> <span class="token6">1</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//每月1号为active01,..每月15号为active15 第二个参数用户的id作为偏移量 只要用户一登录就执行这个设置偏移值为1</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">bitCount</span><span class="token3">(</span><span class="token2">'active01'</span><span class="token3">)</span><span class="token3">;</span><span class="token">//计算昨天(1)号的在线人数</span> <span class="token">//计算网站每月活跃用户(一个月内登陆的用户去重总数)即可把30天的所有bitmap做or计算,然后再计算bitcount</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">bitOp</span><span class="token3">(</span><span class="token2">'or'</span><span class="token3">,</span><span class="token2">'month_active'</span><span class="token3">,</span><span class="token2">'active01'</span><span class="token3">,</span><span class="token2">'active02'</span><span class="token3">,</span><span class="token2">'...'</span><span class="token3">,</span><span class="token2">'active31'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//计算留存率(次日留存=昨天今天连续登的人数/昨天登录的人数)</span> <span class="token">//自减:</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">decr</span><span class="token3">(</span>键<span class="token3">,</span>每次自减的位数默认<span class="token6">1</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//命令将 key 中储存的数字值增一,如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作,且将key的有效时间设置为长期有效</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">decr</span><span class="token3">(</span><span class="token2">'key1'</span><span class="token3">,</span> <span class="token6">10</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">decrBy</span><span class="token3">(</span>键<span class="token3">,</span>每次自减的位数默认<span class="token6">1</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//命令将 key 中储存的数字加上指定的增量值,如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作(命令用于为哈希表中的字段值加上指定增量值)</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">decrBy</span><span class="token3">(</span><span class="token2">'key1'</span><span class="token3">,</span> <span class="token6">10</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//自增:(同decr、decrBy)</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">incr</span><span class="token3">(</span><span class="token2">'key1'</span><span class="token3">,</span><span class="token6">10</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//假设key1 不存在, 自增前自动设置为0现在是0+10=10</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">incrBy</span><span class="token3">(</span><span class="token2">'key1'</span><span class="token3">,</span><span class="token6">10</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//10+10=20 </span> <span class="token">//incrByFloat -使用浮点精度自增自减</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">incrByFloat</span><span class="token3">(</span><span class="token2">'key1'</span><span class="token3">,</span> <span class="token6">1.5</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//假设key1不存在 自增前自动赋值为0 现在是0+1.5=1.5</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">incrByFloat</span><span class="token3">(</span><span class="token2">'key1'</span><span class="token3">,</span> <span class="token6">1.5</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//1.5+1.5=3</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">incrByFloat</span><span class="token3">(</span><span class="token2">'key1'</span><span class="token3">,</span> <span class="token1">-</span><span class="token6">1.5</span><span class="token3">)</span><span class="token3">;</span><span class="token">//3-1.5=1.5</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">get</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//获取redis某个键的值</span> <span class="token">//setRange(键,替换的起始位置,替换的字符) - 字符替换</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">set</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token2">'Hello world'</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setRange</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token6">6</span><span class="token3">,</span> <span class="token2">"redis"</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//Hello redis</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setRange</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token6">6</span><span class="token3">,</span> <span class="token2">"my"</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//Hello mydis</span> <span class="token">//getRange(键, 开始位, 结束位); 返回指定范围的字符串</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">getRange</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token6">0</span><span class="token3">,</span> <span class="token6">5</span><span class="token3">)</span><span class="token3">;</span><span class="token">//'Hello '</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">getRange</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token1">-</span><span class="token6">5</span><span class="token3">,</span> <span class="token1">-</span><span class="token6">1</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//'mydis'</span> <span class="token">//getSet - 设置一个值并返回该键的上一个条目</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">set</span><span class="token3">(</span><span class="token2">'x'</span><span class="token3">,</span> <span class="token2">'42'</span><span class="token3">)</span><span class="token3">;</span> $exValue <span class="token1">=</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">getSet</span><span class="token3">(</span><span class="token2">'x'</span><span class="token3">,</span> <span class="token2">'lol'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//返回'42'</span> $newValue <span class="token1">=</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">get</span><span class="token3">(</span><span class="token2">'x'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//返回'lol'</span> <span class="token">//mGet, getMultiple - 获取所有指定键的值。如果一个或多个键不存在,则数组在键的位置将包含FALSE</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">set</span><span class="token3">(</span><span class="token2">'key1'</span><span class="token3">,</span> <span class="token2">'value1'</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">mGet</span><span class="token3">(</span><span class="token3">[</span><span class="token2">'key0'</span><span class="token3">,</span> <span class="token2">'key1'</span><span class="token3">,</span> <span class="token2">'key5'</span><span class="token3">]</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//[`FALSE`, 'value1', `FALSE`]; 返回与键的值相关的数组</span> <span class="token">//mSet, mSetNX -批量设置键值 返回boolean</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">mSet</span><span class="token3">(</span><span class="token3">[</span><span class="token2">'key0'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'value0'</span><span class="token3">,</span> <span class="token2">'key1'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'value1'</span><span class="token3">]</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span>mSetNX <span class="token3">(</span><span class="token3">[</span><span class="token2">'dada'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'value0'</span><span class="token3">,</span> <span class="token2">'xiaoxiao'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'value1'</span><span class="token3">]</span><span class="token3">)</span><span class="token3">;</span><span class="token">//当且仅当所有给定key全都不存在时才批量设置键值 返回boolean key0和key1都不存在才能设置,否则两个都不会设置成功</span> <span class="token">//set(键,值,过期时间或者扩展数组) - 设置键的值。如果您使用Redis> = 2.6.12,则可以传递扩展选项</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">set</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span><span class="token2">'value'</span><span class="token3">,</span> <span class="token6">10</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//TTL, time to live(生存时间)</span> <span class="token">//setEx, pSetEx - 将设置为指定key的值,并设置生存时间。 PSETEX使用TTL,以毫秒为单位</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setEx</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token6">3600</span><span class="token3">,</span> <span class="token2">'value'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 设置键值, with 1h TTL. 以秒为单位</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">pSetEx</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token6">100</span><span class="token3">,</span> <span class="token2">'value'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 设置键值, with 0.1 sec TTL. 以毫秒为单位</span> <span class="token">//setNx - 键不存在才赋值 返回boolean</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setNx</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token2">'value'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//TRUE </span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setNx</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token2">'value'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//FALSE</span> <span class="token">//strLen(键) -获取键对应字符串值的长度</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">set</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">,</span> <span class="token2">'value'</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">strlen</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 5</span> ``` ```