首页  >  文章  >  后端开发  >  phpredis汉语言手册——《redis中文手册》 php版

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

WBOY
WBOY原创
2016-06-13 12:19:56835浏览

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