ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Keys ``` <pre class="calibre29">``` Keys <span class="token">//del, delete, unlink - 删除指定的键 返回删除的键数</span> <span class="token">//unlink 用法同del一致 非阻塞的,并且将异步执行实际删除 v4.0.0++</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">delete</span><span class="token3">(</span><span class="token2">'key1'</span><span class="token3">,</span> <span class="token2">'key2'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 2</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">delete</span><span class="token3">(</span><span class="token3">[</span><span class="token2">'key1'</span><span class="token3">,</span> <span class="token2">'key2'</span><span class="token3">]</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 2</span> <span class="token">//dump(键) -序列化给定 key的值,并返回;使用 RESTORE 命令可以将这个值反序列化</span> <span class="token">//restore(key,ttl,dump序列化的值) - 反序列化给定的dump提供的序列化值,并将它和给定的key关联 </span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">set</span><span class="token3">(</span><span class="token2">'foo'</span><span class="token3">,</span> <span class="token2">'bar'</span><span class="token3">)</span><span class="token3">;</span> $val <span class="token1">=</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">dump</span><span class="token3">(</span><span class="token2">'foo'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//返回序列化后的键值(在浏览器上乱码无关紧要) bar����(</span> $res<span class="token1">=</span>$redis<span class="token1">-</span><span class="token1">></span><span class="token4">restore</span><span class="token3">(</span><span class="token2">'foo-bar'</span><span class="token3">,</span> <span class="token6">0</span><span class="token3">,</span> $val<span class="token3">)</span><span class="token3">;</span> <span class="token">// foo-bar键的值与foo键的值相同都为bar</span> $a<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">'foo'</span><span class="token3">)</span><span class="token3">;</span> $b<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">'foo-bar'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//exists(string or array) - 检查给定 key 是否存在 返回存在key的数量 版本<4.0.0则返回boolean</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">exists</span><span class="token3">(</span><span class="token2">'NonExistingKey'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//0 都不存在返回0</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">'foo'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'foo'</span><span class="token3">,</span> <span class="token2">'bar'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'bar'</span><span class="token3">,</span> <span class="token2">'baz'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'baz'</span><span class="token3">]</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">exists</span><span class="token3">(</span><span class="token3">[</span><span class="token2">'foo'</span><span class="token3">,</span> <span class="token2">'bar'</span><span class="token3">,</span> <span class="token2">'baz'</span><span class="token3">]</span><span class="token3">)</span><span class="token3">;</span><span class="token">//3</span> <span class="token">//</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">exists</span><span class="token3">(</span><span class="token2">'foo'</span><span class="token3">,</span> <span class="token2">'bar'</span><span class="token3">,</span> <span class="token2">'baz'</span><span class="token3">)</span><span class="token3">;</span><span class="token">//3</span> <span class="token">//expire, setTimeout, pexpire -设定指定key的生存时间(s) 返回boolean</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> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setTimeout</span><span class="token3">(</span><span class="token2">'x'</span><span class="token3">,</span> <span class="token6">3</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 设置x3秒的存活时间</span> <span class="token4">sleep</span><span class="token3">(</span><span class="token6">5</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 休眠5秒</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">// 返回false x已经过期了</span> <span class="token">//expireAt, pexpireAt - 设定指定key生存时间(只接受10位数的时间戳)</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">expireAt</span><span class="token3">(</span><span class="token2">'x'</span><span class="token3">,</span> <span class="token4">time</span><span class="token3">(</span><span class="token3">)</span><span class="token1">+</span><span class="token6">3</span><span class="token3">)</span><span class="token3">;</span><span class="token">//3秒的生存时间时间</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">expireAt</span><span class="token3">(</span><span class="token2">'x'</span><span class="token3">,</span> <span class="token6">1577811600</span><span class="token3">)</span><span class="token3">;</span><span class="token">//2020/01/01 00:00:00过期</span> <span class="token">//keys, getKeys - 返回满足给定pattern的所有key ( 查看键的总数dbSize) </span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">keys</span><span class="token3">(</span><span class="token2">'user'</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">keys</span><span class="token3">(</span><span class="token2">'user*'</span><span class="token3">)</span><span class="token3">;</span><span class="token">//如user1...</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">keys</span><span class="token3">(</span><span class="token2">'*'</span><span class="token3">)</span><span class="token3">;</span><span class="token">//返回所有键</span> <span class="token">//scan - 迭代数据库的键 (Redis >= 2.8.0) 实际生产中使用这个避免使用keys阻塞数据库</span> $it<span class="token1">=</span>NULL<span class="token3">;</span> $count<span class="token1">=</span><span class="token6">1000</span><span class="token3">;</span><span class="token">//不填第三个参数是返回所有匹配到的键</span> $match<span class="token1">=</span><span class="token2">'active*'</span><span class="token3">;</span><span class="token">//匹配键</span> $arr_keys <span class="token1">=</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">scan</span><span class="token3">(</span>$it<span class="token3">,</span>$match<span class="token3">,</span>$count<span class="token3">)</span><span class="token3">;</span> <span class="token">//与原生的redis scan有点区别</span> <span class="token">//仿原生:</span> $it <span class="token1">=</span> <span class="token5">null</span><span class="token3">;</span><span class="token">//游标</span> $match <span class="token1">=</span> <span class="token2">'active*'</span><span class="token3">;</span> $count <span class="token1">=</span> <span class="token6">10</span><span class="token3">;</span> $index <span class="token1">=</span> <span class="token6">0</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setOption</span><span class="token3">(</span>Redis<span class="token3">:</span><span class="token3">:</span>OPT_SCAN<span class="token3">,</span>Redis<span class="token3">:</span><span class="token3">:</span>SCAN_RETRY<span class="token3">)</span><span class="token3">;</span> $iterator <span class="token1">=</span> <span class="token5">null</span><span class="token3">;</span> <span class="token5">while</span> <span class="token3">(</span>$keys <span class="token1">=</span> <span class="token4">call_user_func_array</span><span class="token3">(</span><span class="token4">array</span><span class="token3">(</span>$redis<span class="token3">,</span> <span class="token2">'scan'</span><span class="token3">)</span><span class="token3">,</span> <span class="token4">array</span><span class="token3">(</span><span class="token1">&</span>$it<span class="token3">,</span> $match<span class="token3">,</span>$count<span class="token3">)</span><span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> echo $it<span class="token3">.</span> PHP_EOL<span class="token3">.</span><span class="token2">'<br>'</span><span class="token3">;</span> foreach <span class="token3">(</span>$keys as $key<span class="token3">)</span> <span class="token3">{</span> echo $key <span class="token3">.</span> PHP_EOL<span class="token3">.</span><span class="token2">'<br>'</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//migrate(host,port,key/keys,targetDB(0~15),timeout,不移除源实例上的key(拷贝),替换目标实例上已存在的key) - 将key迁移到其他Redis实例 多个key迁移版本必须>=3.0.6</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">migrate</span><span class="token3">(</span><span class="token2">'127.0.0.1'</span><span class="token3">,</span> <span class="token6">6379</span><span class="token3">,</span> <span class="token2">'foo'</span><span class="token3">,</span> <span class="token6">0</span><span class="token3">,</span> <span class="token6">3600</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">migrate</span><span class="token3">(</span><span class="token2">'backup'</span><span class="token3">,</span> <span class="token6">6379</span><span class="token3">,</span> <span class="token2">'foo'</span><span class="token3">,</span> <span class="token6">0</span><span class="token3">,</span> <span class="token6">3600</span><span class="token3">,</span> <span class="token6">true</span><span class="token3">,</span> <span class="token6">true</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// copy and replace</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">migrate</span><span class="token3">(</span><span class="token2">'backup'</span><span class="token3">,</span> <span class="token6">6379</span><span class="token3">,</span> <span class="token2">'foo'</span><span class="token3">,</span> <span class="token6">0</span><span class="token3">,</span> <span class="token6">3600</span><span class="token3">,</span> <span class="token6">false</span><span class="token3">,</span> <span class="token6">true</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// just REPLACE flag</span> <span class="token">/* 迁移多个keys (version >= 3.0.6) $redis->migrate('backup', 6379, ['key1', 'key2', 'key3'], 0, 3600); //move - 将key移动到其他数据库 $redis->select(0); // switch to DB 0 $redis->set('x', '42'); // write 42 to x $redis->move('x', 1); // move to DB 1 $redis->select(1); // switch to DB 1 $redis->get('x'); // will return 42 //object(指定返回内容,key) 返回指定key的Redis对象的内部结构 //REFCOUNT: 返回给定key引用所储存的值的次数。此命令主要用于除错 //ENCODING:返回给定key所储存的值所使用的内部编码方式 //字符串raw(一般字符串)或int (为了节约内存,Redis 会将字符串表示的 64 位有符号整数编码为整数来进行储存) //列表:ziplist(是为节约大小较小的列表空间而作的特殊表示)或linkedlist; //集合:intset(是只储存数字的小集合的特殊表示)或者hashtable; //哈希:zipmap(是小哈希表的特殊表示)或者hashtable; //有序集合:ziplist(用于表示小的有序集合)或者skiplist(用于表示任何大小的有序集合) //IDLETIME:返回给定 key 自储存以来的空闲时间(idle, 没有被读取也没有被写入),以秒为单位 //返回值: REFCOUNT 和 IDLETIME 返回数字;ENCODING 返回相应的编码类型;如果密钥不存在则为FALSE。 $b=$redis->object("refcount", "month_active"); // → 1 $a=$redis->object("encoding", "active31"); // → raw $c=$redis->object("idletime", "active31"); //→ 400 //persist(键) - 移除给定key的生存时间(key变成永不过时的key)返回boolean $redis->persist('key'); //randomKey - 从当前数据库中随机返回(不删除)一个 key $redis->randomKey();//随机返回当前数据库总的key //rename(旧键名,新键名), renameKey(旧键名,新键名) - 修改键名 返回boolean $redis->rename('x', 'y'); renameNx(旧键名,新键名) - 当修改的新键名不存在时才修改键名 //type(键名) - 返回指定key所储存的值的类型 $redis->type('key'); //sort(参数) - 对列表、有序集合和无序集合中的元素进行排序 /* 参数有: 'by' => 'some_pattern_*', 'limit' => [0, 1], 'get' => 'some_other_pattern_*' or an array of patterns, 'sort' => 'asc' or 'desc', 'alpha' => TRUE, 'store' => 'external-key' */</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">delete</span><span class="token3">(</span><span class="token2">'s'</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">sAdd</span><span class="token3">(</span><span class="token2">'s'</span><span class="token3">,</span> <span class="token6">5</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">sAdd</span><span class="token3">(</span><span class="token2">'s'</span><span class="token3">,</span> <span class="token6">4</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">sAdd</span><span class="token3">(</span><span class="token2">'s'</span><span class="token3">,</span> <span class="token6">2</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">sAdd</span><span class="token3">(</span><span class="token2">'s'</span><span class="token3">,</span> <span class="token6">1</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">sAdd</span><span class="token3">(</span><span class="token2">'s'</span><span class="token3">,</span> <span class="token6">3</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">sort</span><span class="token3">(</span><span class="token2">'s'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 1,2,3,4,5</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">sort</span><span class="token3">(</span><span class="token2">'s'</span><span class="token3">,</span> <span class="token3">[</span><span class="token2">'sort'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'desc'</span><span class="token3">]</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 5,4,3,2,1</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">sort</span><span class="token3">(</span><span class="token2">'s'</span><span class="token3">,</span> <span class="token3">[</span><span class="token2">'sort'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'desc'</span><span class="token3">,</span> <span class="token2">'store'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'out'</span><span class="token3">]</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// (int)5</span> <span class="token">//ttl(键), pttl(键) -返回指定键的剩余生存时间 ttl(返回秒) pttl(返回毫秒)</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">ttl</span><span class="token3">(</span><span class="token2">'key'</span><span class="token3">)</span><span class="token3">;</span> ``` ```