목차
redis中文手册:http://readthedocs.org/docs/redis/en/latest/?
백엔드 개발 PHP 튜토리얼 phpredis汉语言手册——《redis中文手册》 php版

phpredis汉语言手册——《redis中文手册》 php版

Jun 13, 2016 pm 12:19 PM
array gt key redis string

phpredis中文手册——《redis中文手册》 php版

redis中文手册:http://readthedocs.org/docs/redis/en/latest/?

本文是参考《redis中文手册》,将示例代码用php来实现,注意php-redis与redis_cli的区别(主要是返回值类型和参数用法)。

目录(使用CTRL+F快速查找命令):

KeyStringHashListSet
  • 键(Key)
    • DEL
    • KEYS
    • RANDOMKEY
    • TTL
    • EXISTS
    • MOVE
    • RENAME
    • RENAMENX
    • TYPE
    • EXPIRE
    • EXPIREAT
    • OBJECT
    • PERSIST
    • SORT
  • 字符串(String)
    • SET
    • SETNX
    • SETEX
    • SETRANGE
    • MSET
    • MSETNX
    • APPEND
    • GET
    • MGET
    • GETRANGE
    • GETSET
    • STRLEN
    • INCR
    • INCRBY
    • DECR
    • DECRBY
    • SETBIT
    • GETBIT
  • 哈希表(Hash)
    • HSET
    • HSETNX
    • HMSET
    • HGET
    • HMGET
    • HGETALL
    • HDEL
    • HLEN
    • HEXISTS
    • HINCRBY
    • HKEYS
    • HVALS
  • 表(List)
    • LPUSH
    • LPUSHX
    • RPUSH
    • RPUSHX
    • LPOP
    • RPOP
    • BLPOP
    • BRPOP
    • LLEN
    • LRANGE
    • LREM
    • LSET
    • LTRIM
    • LINDEX
    • LINSERT
    • RPOPLPUSH
    • BRPOPLPUSH
  • 集合(Set)
    • SADD
    • SREM
    • SMEMBERS
    • SISMEMBER
    • SCARD
    • SMOVE
    • SPOP
    • SRANDMEMBER
    • SINTER
    • SINTERSTORE
    • SUNION
    • SUNIONSTORE
    • SDIFF
    • SDIFFSTORE
?Sorted SetPub/SubTransactionConnectionServer
  • 有序集(Sorted Set)
    • ZADD
    • ZREM
    • ZCARD
    • ZCOUNT
    • ZSCORE
    • ZINCRBY
    • ZRANGE
    • ZREVRANGE
    • ZRANGEBYSCORE
    • ZREVRANGEBYSCORE
    • ZRANK
    • ZREVRANK
    • ZREMRANGEBYRANK
    • ZREMRANGEBYSCORE
    • ZINTERSTORE
    • ZUNIONSTORE
  • 发布/订阅(Pub/Sub)
    • PUBLISH
    • SUBSCRIBE
    • PSUBSCRIBE
    • UNSUBSCRIBE
    • PUNSUBSCRIBE
  • 事务(Transaction)
    • WATCH
    • UNWATCH
    • MULTI
    • EXEC
    • DISCARD
  • 连接(Connection)
    • AUTH
    • PING
    • SELECT
    • ECHO
    • QUIT
  • 服务器(Server)
    • BGREWRITEAOF
    • BGSAVE
    • SAVE
    • LASTSAVE
    • DBSIZE
    • SLAVEOF
    • FLUSHALL
    • FLUSHDB
    • SHUTDOWN
    • SLOWLOG
    • INFO
    • CONFIG GET
    • CONFIG SET
    • CONFIG RESETSTAT
    • DEBUG OBJECT
    • DEBUG SEGFAULT
    • MONITOR
    • SYNC

phpredis是redis的php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系

很有用;以下是redis官方提供的命令使用技巧:

下载地址如下:

https://github.com/owlient/phpredis(支持redis 2.0.4)

Redis::__construct构造函数
$redis = new Redis();

connect, open 链接redis服务
参数
host: string,服务地址
port: int,端口号
timeout: float,链接时长 (可选, 默认为 0 ,不限链接时间)
注: 在redis.conf中也有时间,默认为300

pconnect, popen 不会主动关闭的链接
参考上面

setOption 设置redis模式

getOption 查看redis设置的模式

ping?查看连接状态

?

KEY相关操作

DEL

移除给定的一个或多个key。

如果key不存在,则忽略该命令。

时间复杂度:
O(N),N为要移除的key的数量。
移除单个字符串类型的key,时间复杂度为O(1)。
移除单个列表、集合、有序集合或哈希表类型的key,时间复杂度为O(M),M为以上数据结构内的元素数量。返回值:被移除key的数量。

?

复制代码
<span   style="max-width:90%">//</span><span style="color: #008000; line-height: 1.5 !important;">DEL</span><span style="color: #008000; line-height: 1.5 !important;"> #</span><span style="color: #008000; line-height: 1.5 !important;"> 情况1: 删除单个key</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->set('myname','ikodota'); <span style="color: #0000ff; line-height: 1.5 !important;">echo</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->get('myname').'<br>'; <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 返回:ikodota</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->del('myname');<span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 返回 TRUE(1)</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->get('myname')); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 返回 bool(false)</span> <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 情况2: 删除一个不存在的key</span><span style="color: #0000ff; line-height: 1.5 !important;">if</span>(!<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->exists('fake_key')) <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 不存在</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->del('fake_key')); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 返回 int(0)</span> <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 情况3: 同时删除多个key</span><span style="color: #800080; line-height: 1.5 !important;">$array_mset</span>=<span style="color: #0000ff; line-height: 1.5 !important;">array</span>('first_key'=>'first_val',          'second_key'=>'second_val',          'third_key'=>'third_val'); <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->mset(<span style="color: #800080; line-height: 1.5 !important;">$array_mset</span>); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;">用MSET一次储存多个值</span><span style="color: #800080; line-height: 1.5 !important;">$array_mget</span>=<span style="color: #0000ff; line-height: 1.5 !important;">array</span>('first_key','second_key','third_key'); <span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->mget(<span style="color: #800080; line-height: 1.5 !important;">$array_mget</span>)); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;">一次返回多个值 //array(3) { [0]=> string(9) "first_val" [1]=> string(10) "second_val" [2]=> string(9) "third_val" }</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>-><strong>del</strong>(<span style="color: #800080; line-height: 1.5 !important;">$array_mget</span>); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;">同时删除多个key</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->mget(<span style="color: #800080; line-height: 1.5 !important;">$array_mget</span>)); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;">返回 array(3) { [0]=> bool(false) [1]=> bool(false) [2]=> bool(false) }</span>
로그인 후 복사
复制代码

?

KEYSKEYS pattern?查找符合给定模式的key
KEYS?*命中数据库中所有key
KEYS?h?llo命中hello,?hallo?and?hxllo等。
KEYS?h*llo命中hlloheeeeello等。
KEYS?h[ae]llo命中hellohallo,但不命中hillo

特殊符号用"\"隔开

时间复杂度:O(N),N为数据库中key的数量。返回值:符合给定模式的key列表。

警告 :KEYS的速度非常快,但在一个大的数据库中使用它仍然可能造成性能问题,如果你需要从一个数据集中查找特定的key,你最好还是用集合(Set)

?

复制代码
<span   style="max-width:90%">//</span><span style="color: #008000; line-height: 1.5 !important;">KEYS</span><span style="color: #008000; line-height: 1.5 !important;"> #</span><span style="color: #008000; line-height: 1.5 !important;">$redis->FLUSHALL();</span><span style="color: #800080; line-height: 1.5 !important;">$array_mset_keys</span>=<span style="color: #0000ff; line-height: 1.5 !important;">array</span>('one'=>'1',          'two'=>'2',          'three '=>'3',          'four'=>'4'); <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->mset(<span style="color: #800080; line-height: 1.5 !important;">$array_mset_keys</span>); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;">用MSET一次储存多个值</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->keys('*o*')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">array(3) { [0]=> string(4) "four" [1]=> string(3) "two" [2]=> string(3) "one" }</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->keys('t??')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">array(1) { [0]=> string(3) "two" }</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->keys('t[w]*')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">array(1) { [0]=> string(3) "two" }</span><span style="color: #008080; line-height: 1.5 !important;">print_r</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->keys('*')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">Array ( [0] => four [1] => three [2] => two [3] => one )</span>
로그인 후 복사
复制代码

?

RANDOMKEY

从当前数据库中随机返回(不删除)一个key。

时间复杂度:O(1)返回值:
当数据库不为空时,返回一个key
当数据库为空时,返回nil。

?

复制代码
<span   style="max-width:90%">//</span><span style="color: #008000; line-height: 1.5 !important;">RANDOMKEY</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->FLUSHALL(); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 情况1:数据库不为空</span><span style="color: #800080; line-height: 1.5 !important;">$array_mset_randomkey</span>=<span style="color: #0000ff; line-height: 1.5 !important;">array</span>('fruit'=>'apple',                'drink'=>'beer',                'food'=>'cookis'); <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->mset(<span style="color: #800080; line-height: 1.5 !important;">$array_mset_randomkey</span>); <span style="color: #0000ff; line-height: 1.5 !important;">echo</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->randomkey(); <span style="color: #008080; line-height: 1.5 !important;">print_r</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->keys('*')); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 查看数据库内所有key,证明RANDOMKEY并不删除key//Array ( [0] => food [1] => drink [2] => fruit )</span> <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 情况2:数据库为空</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->flushdb(); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 删除当前数据库所有key</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>-> randomkey()); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">bool(false)</span>
로그인 후 복사
复制代码

?

TTL
TTL key

返回给定key的剩余生存时间(time to live)(以秒为单位)。

时间复杂度:O(1)返回值:
key的剩余生存时间(以秒为单位)。
key不存在或没有设置生存时间时,返回-1?。

?

复制代码
<span   style="max-width:90%">//</span><span style="color: #008000; line-height: 1.5 !important;">TTL</span><span style="color: #008000; line-height: 1.5 !important;"> #</span><span style="color: #008000; line-height: 1.5 !important;"> 情况1:带TTL的key</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->flushdb(); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">$redis->set('name','ikodota'); # 设置一个key</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->expire('name',30); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 设置生存时间为30秒 //return (integer) 1</span><span style="color: #0000ff; line-height: 1.5 !important;">echo</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->get('name'); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">return ikodota</span><span style="color: #0000ff; line-height: 1.5 !important;">echo</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->ttl('name'); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">(integer) 25//echo $redis->ttl('name');  # 30秒过去,name过期 //(integer) -1</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->get('name')); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 过期的key将被删除 //return bool(false);</span> <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 情况2:不带TTL的key</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->set('site','wikipedia.org');<span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">OK</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->ttl('site'));<span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">int(-1)</span> <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 情况3:不存在的key</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->EXISTS('not_exists_key');<span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">int(0)</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->TTL('not_exists_key'));<span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">int(-1)</span>
로그인 후 복사
复制代码

?

EXISTSEXISTS key

检查给定key是否存在。

时间复杂度:O(1)返回值:key存在,返回1,否则返回0
<span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">EXISTS</span><span style="color: #0000ff; line-height: 1.5 !important;">echo</span> '<br>EXISTS<br>'; <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->set('db',"redis"); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">bool(true) </span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->exists('db')); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> key存在 //bool(true) </span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->del('db'); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 删除key //int(1)</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->exists('db')) <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> key不存在 //bool(false)</span>
로그인 후 복사

?

MOVE
MOVE key db

将当前数据库(默认为0)的key移动到给定的数据库db当中。

如果当前数据库(源数据库)和给定数据库(目标数据库)有相同名字的给定key,或者key不存在于当前数据库,那么MOVE没有任何效果。

因此,也可以利用这一特性,将MOVE当作锁(locking)原语。

时间复杂度:O(1)返回值:移动成功返回1,失败则返回0
复制代码
<span   style="max-width:90%">//</span><span style="color: #008000; line-height: 1.5 !important;">MOVE</span><span style="color: #0000ff; line-height: 1.5 !important;">echo</span> '<br><br>MOVE<br>'; <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 情况1: key存在于当前数据库</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SELECT(0); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> redis默认使用数据库0,为了清晰起见,这里再显式指定一次。//OK</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SET('song',"secret base - Zone"); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">OK</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span> (<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->MOVE('song',1)); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 将song移动到数据库1 //bool(true)</span> <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 情况2:当key不存在的时候</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SELECT(1); <span style="color: #008080; line-height: 1.5 !important;">var_dump</span> (<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->EXISTS('fake_key'));<span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">bool(false);</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->MOVE('fake_key', 0)); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 试图从数据库1移动一个不存在的key到数据库0,失败) //bool(false)</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SELECT(0); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 使用数据库0</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->EXISTS('fake_key')); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 证实fake_key不存在 //bool(false)</span> <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 情况3:当源数据库和目标数据库有相同的key时</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SELECT(0); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 使用数据库0</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SET('favorite_fruit',"banana"); <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SELECT(1); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 使用数据库1</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SET('favorite_fruit',"apple"); <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SELECT(0); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 使用数据库0,并试图将favorite_fruit移动到数据库1</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->MOVE('favorite_fruit',1)); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 因为两个数据库有相同的key,MOVE失败 //return bool(false)</span><span style="color: #0000ff; line-height: 1.5 !important;">echo</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->GET('favorite_fruit'); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 数据库0的favorite_fruit没变 //return banana</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SELECT(1); <span style="color: #0000ff; line-height: 1.5 !important;">echo</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->GET('favorite_fruit'); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 数据库1的favorite_fruit也是 //return apple</span>
로그인 후 복사
复制代码

?

RENAME?

RENAME key newkey

将key改名为newkey。

当key和newkey相同或者key不存在时,返回一个错误。

当newkey已经存在时,RENAME命令将覆盖旧值。

时间复杂度:O(1)返回值:改名成功时提示OK,失败时候返回一个错误。
复制代码
<span   style="max-width:90%">//</span><span style="color: #008000; line-height: 1.5 !important;">RENAME</span><span style="color: #0000ff; line-height: 1.5 !important;">echo</span> '<br><br>RENAME<br>'; <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 情况1:key存在且newkey不存在</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SET('message',"hello world"); <span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>-><span style="color: #008080; line-height: 1.5 !important;">RENAME</span>('message','greeting')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">bool(true)</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->EXISTS('message')); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> message不复存在 //bool(false)</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->EXISTS('greeting')); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> greeting取而代之 //bool(true)</span> <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 情况2:当key不存在时,返回错误 ,php返回false;</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>-><span style="color: #008080; line-height: 1.5 !important;">RENAME</span>('fake_key','never_exists')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">bool(false)</span> <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 情况3:newkey已存在时,RENAME会覆盖旧newkey</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SET('pc',"lenovo"); <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SET('personal_computer',"dell"); <span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>-><span style="color: #008080; line-height: 1.5 !important;">RENAME</span>('pc','personal_computer')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">bool(true)</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->GET('pc')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">(nil)   bool(false)</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->GET('personal_computer')); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> dell“没有”了 //string(6) "lenovo"</span>
로그인 후 복사
复制代码

?

RENAMENX?RENAMENX key newkey

当且仅当newkey不存在时,将key改为newkey。

出错的情况和RENAME一样(key不存在时报错)。

时间复杂度:O(1)返回值:
修改成功时,返回1
如果newkey已经存在,返回0
复制代码
<span   style="max-width:90%">//</span><span style="color: #008000; line-height: 1.5 !important;">RENAMENX</span><span style="color: #0000ff; line-height: 1.5 !important;">echo</span> '<br><br>RENAMENX<br>'; <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 情况1:newkey不存在,成功</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SET('player',"MPlyaer"); <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->EXISTS('best_player'); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">int(0)</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->RENAMENX('player','best_player')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;"> bool(true) </span> <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 情况2:newkey存在时,失败</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SET('animal',"bear"); <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SET('favorite_animal', "butterfly"); <span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->RENAMENX('animal', 'favorite_animal'));<span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;"> bool(false)</span> <span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->get('animal')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">string(4) "bear"</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->get('favorite_animal')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">string(9) "butterfly"</span>
로그인 후 복사
复制代码
TYPETYPE key

返回key所储存的值的类型。

时间复杂度:O(1)返回值:
none(key不存在) int(0)
string(字符串) int(1)
list(列表) int(3)
set(集合) int(2)
zset(有序集) int(4)
hash(哈希表) int(5)

?

复制代码
<span   style="max-width:90%">//</span><span style="color: #008000; line-height: 1.5 !important;">TYPE</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->flushALL(); <span style="color: #0000ff; line-height: 1.5 !important;">echo</span> '<br><br>TYPE<br>'; <span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->TYPE('fake_key')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">none /int(0)</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SET('weather',"sunny"); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 构建一个字符串</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->TYPE('weather'));<span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">string / int(1)</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SADD('pat',"dog"); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 构建一个集合</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->TYPE('pat')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">set /int(2)</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->LPUSH('book_list',"programming in scala"); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 构建一个列表</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->TYPE('book_list'));<span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">list / int(3) </span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->ZADD('pats',1,'cat'); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 构建一个zset (sorted set) // int(1)</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->ZADD('pats',2,'dog'); <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->ZADD('pats',3,'pig'); <span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->zRange('pats',0,-1)); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;"> array(3) { [0]=> string(3) "cat" [1]=> string(3) "dog" [2]=> string(3) "pig" }</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->TYPE('pats')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">zset / int(4)</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->HSET('website','google','www.g.cn'); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 一个新域</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->HGET('website','google')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">string(8) "www.g.cn"</span><span style="color: #008080; line-height: 1.5 !important;">var_dump</span>(<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->TYPE('website')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">hash /int(5)</span>
로그인 후 복사
复制代码

EXPIRE

EXPIRE key seconds

为给定key设置生存时间。

当key过期时,它会被自动删除。

在Redis中,带有生存时间的key被称作“易失的”(volatile)。

?

在低于2.1.3版本的Redis中,已存在的生存时间不可覆盖。
从2.1.3版本开始,key的生存时间可以被更新,也可以被PERSIST命令移除。(详情参见?http://redis.io/topics/expire)。

?

时间复杂度:O(1)返回值:
设置成功返回1
key不存在或者不能为key设置生存时间时(比如在低于2.1.3中你尝试更新key的生存时间),返回0
复制代码
<span   style="max-width:90%">//</span><span style="color: #008000; line-height: 1.5 !important;">EXPIRE</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->select(7); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">$redis->flushdb();</span> <span style="color: #0000ff; line-height: 1.5 !important;">echo</span> '<br><br>EXPIRE<br>'; <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SET('cache_page',"www.cnblogs.com/ikodota"); <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->EXPIRE('cache_page', 30); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 设置30秒后过期</span><span style="color: #008080; line-height: 1.5 !important;">sleep</span>(6); <span style="color: #0000ff; line-height: 1.5 !important;">echo</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->TTL('cache_page').'<br>'; <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 查看给定key的剩余生存时间 //(integer) 24</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->EXPIRE('cache_page', 3000); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 更新生存时间,3000秒</span><span style="color: #008080; line-height: 1.5 !important;">sleep</span>(4); <span style="color: #0000ff; line-height: 1.5 !important;">echo</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->TTL('cache_page').'<br>'; <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">(integer) 2996</span>
로그인 후 복사
复制代码

?

?

EXPIREAT?EXPIREAT key timestamp

EXPIREAT的作用和EXPIRE一样,都用于为key设置生存时间。

不同在于EXPIREAT命令接受的时间参数是UNIX时间戳(unix timestamp)。

时间复杂度:O(1)返回值:
如果生存时间设置成功,返回1
key不存在或没办法设置生存时间,返回0

?

<span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">EXPIREAT</span><span style="color: #0000ff; line-height: 1.5 !important;">echo</span> '<br><br>EXPIREAT<br>'; <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SET('cache','www.google.com'); <span style="color: #0000ff; line-height: 1.5 !important;">echo</span> <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->EXPIREAT('cache','1355292000'); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #008000; line-height: 1.5 !important;"> 这个key将在2012.12.12过期</span> <span style="color: #0000ff; line-height: 1.5 !important;">echo</span> (<span style="color: #800080; line-height: 1.5 !important;">$redis</span>->TTL('cache')); <span style="color: #008000; line-height: 1.5 !important;">//</span><span style="color: #008000; line-height: 1.5 !important;">return 124345085</span>
로그인 후 복사

?

OBJECT?OBJECT subcommand [arguments [arguments]]

OBJECT命令允许从内部察看给定key的Redis对象。

它通常用在除错(debugging)或者了解为了节省空间而对key使用特殊编码的情况。
当将Redis用作缓存程序时,你也可以通过OBJECT命令中的信息,决定key的驱逐策略(eviction policies)。

OBJECT命令有多个子命令:

  • OBJECT?REFCOUNT?返回给定key引用所储存的值的次数。此命令主要用于除错。
  • OBJECT?ENCODING?返回给定key锁储存的值所使用的内部表示(representation)。
  • OBJECT?IDLETIME?返回给定key自储存以来的空转时间(idle, 没有被读取也没有被写入),以秒为单位。
对象可以以多种方式编码:
  • 字符串可以被编码为raw(一般字符串)或int(用字符串表示64位数字是为了节约空间)。
  • 列表可以被编码为ziplistlinkedlistziplist是为节约大小较小的列表空间而作的特殊表示。
  • 集合可以被编码为intset或者hashtableintset是只储存数字的小集合的特殊表示。
  • 哈希表可以编码为zipmap或者hashtablezipmap是小哈希表的特殊表示。
  • 有序集合可以被编码为ziplist或者skiplist格式。ziplist用于表示小的有序集合,而skiplist则用于表示任何大小的有序集合。
假如你做了什么让Redis没办法再使用节省空间的编码时(比如将一个只有1个元素的集合扩展为一个有100万个元素的集合),特殊编码类型(specially encoded types)会自动转换成通用类型(general type)。时间复杂度:O(1)返回值:
REFCOUNTIDLETIME返回数字。
ENCODING返回相应的编码类型。
复制代码
<span   style="max-width:90%">//</span><span style="color: #008000; line-height: 1.5 !important;">OBJECT</span><span style="color: #800080; line-height: 1.5 !important;">$redis</span>->select(8); <span style="color: #0000ff; line-height: 1.5 !important;">echo</span> '<br><br>OBJECT<br>'; <span style="color: #800080; line-height: 1.5 !important;">$redis</span>->SET('game',"WOW"); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #00"></span>
로그인 후 복사
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 채팅 명령 및 사용 방법
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Redis 클러스터 모드를 구축하는 방법 Redis 클러스터 모드를 구축하는 방법 Apr 10, 2025 pm 10:15 PM

Redis Cluster Mode는 Sharding을 통해 Redis 인스턴스를 여러 서버에 배포하여 확장 성 및 가용성을 향상시킵니다. 시공 단계는 다음과 같습니다. 포트가 다른 홀수 redis 인스턴스를 만듭니다. 3 개의 센티넬 인스턴스를 만들고, Redis 인스턴스 및 장애 조치를 모니터링합니다. Sentinel 구성 파일 구성, Redis 인스턴스 정보 및 장애 조치 설정 모니터링 추가; Redis 인스턴스 구성 파일 구성, 클러스터 모드 활성화 및 클러스터 정보 파일 경로를 지정합니다. 각 redis 인스턴스의 정보를 포함하는 Nodes.conf 파일을 작성합니다. 클러스터를 시작하고 Create 명령을 실행하여 클러스터를 작성하고 복제본 수를 지정하십시오. 클러스터에 로그인하여 클러스터 정보 명령을 실행하여 클러스터 상태를 확인하십시오. 만들다

Redis 명령을 사용하는 방법 Redis 명령을 사용하는 방법 Apr 10, 2025 pm 08:45 PM

Redis 지시 사항을 사용하려면 다음 단계가 필요합니다. Redis 클라이언트를 엽니 다. 명령 (동사 키 값)을 입력하십시오. 필요한 매개 변수를 제공합니다 (명령어마다 다름). 명령을 실행하려면 Enter를 누르십시오. Redis는 작업 결과를 나타내는 응답을 반환합니다 (일반적으로 OK 또는 -err).

Redis의 소스 코드를 읽는 방법 Redis의 소스 코드를 읽는 방법 Apr 10, 2025 pm 08:27 PM

Redis 소스 코드를 이해하는 가장 좋은 방법은 단계별로 이동하는 것입니다. Redis의 기본 사항에 익숙해집니다. 특정 모듈을 선택하거나 시작점으로 기능합니다. 모듈 또는 함수의 진입 점으로 시작하여 코드를 한 줄씩 봅니다. 함수 호출 체인을 통해 코드를 봅니다. Redis가 사용하는 기본 데이터 구조에 익숙해 지십시오. Redis가 사용하는 알고리즘을 식별하십시오.

단일 스레드 레 디스를 사용하는 방법 단일 스레드 레 디스를 사용하는 방법 Apr 10, 2025 pm 07:12 PM

Redis는 단일 스레드 아키텍처를 사용하여 고성능, 단순성 및 일관성을 제공합니다. 동시성을 향상시키기 위해 I/O 멀티플렉싱, 이벤트 루프, 비 블로킹 I/O 및 공유 메모리를 사용하지만 동시성 제한 제한, 단일 고장 지점 및 쓰기 집약적 인 워크로드에 부적합한 제한이 있습니다.

Redis 데이터를 지우는 방법 Redis 데이터를 지우는 방법 Apr 10, 2025 pm 10:06 PM

Redis 데이터를 지우는 방법 : Flushall 명령을 사용하여 모든 키 값을 지우십시오. FlushDB 명령을 사용하여 현재 선택한 데이터베이스의 키 값을 지우십시오. 선택을 사용하여 데이터베이스를 전환 한 다음 FlushDB를 사용하여 여러 데이터베이스를 지우십시오. del 명령을 사용하여 특정 키를 삭제하십시오. Redis-Cli 도구를 사용하여 데이터를 지우십시오.

Redis에서 모든 키를 보는 방법 Redis에서 모든 키를 보는 방법 Apr 10, 2025 pm 07:15 PM

Redis에서 모든 키를 보려면 세 가지 방법이 있습니다. 키 명령을 사용하여 지정된 패턴과 일치하는 모든 키를 반환하십시오. 스캔 명령을 사용하여 키를 반복하고 키 세트를 반환하십시오. 정보 명령을 사용하여 총 키 수를 얻으십시오.

Redis로 서버를 시작하는 방법 Redis로 서버를 시작하는 방법 Apr 10, 2025 pm 08:12 PM

Redis 서버를 시작하는 단계에는 다음이 포함됩니다. 운영 체제에 따라 Redis 설치. Redis-Server (Linux/MacOS) 또는 Redis-Server.exe (Windows)를 통해 Redis 서비스를 시작하십시오. Redis-Cli Ping (Linux/MacOS) 또는 Redis-Cli.exe Ping (Windows) 명령을 사용하여 서비스 상태를 확인하십시오. Redis-Cli, Python 또는 Node.js와 같은 Redis 클라이언트를 사용하여 서버에 액세스하십시오.

Redis 대기열을 읽는 방법 Redis 대기열을 읽는 방법 Apr 10, 2025 pm 10:12 PM

Redis의 대기열을 읽으려면 대기열 이름을 얻고 LPOP 명령을 사용하여 요소를 읽고 빈 큐를 처리해야합니다. 특정 단계는 다음과 같습니다. 대기열 이름 가져 오기 : "큐 :"와 같은 "대기열 : my-queue"의 접두사로 이름을 지정하십시오. LPOP 명령을 사용하십시오. 빈 대기열 처리 : 대기열이 비어 있으면 LPOP이 NIL을 반환하고 요소를 읽기 전에 대기열이 존재하는지 확인할 수 있습니다.

See all articles