搜索
首页后端开发php教程phpredis汉语言手册——《redis中文手册》 php版

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
PHP的目的:构建动态网站PHP的目的:构建动态网站Apr 15, 2025 am 12:18 AM

PHP用于构建动态网站,其核心功能包括:1.生成动态内容,通过与数据库对接实时生成网页;2.处理用户交互和表单提交,验证输入并响应操作;3.管理会话和用户认证,提供个性化体验;4.优化性能和遵循最佳实践,提升网站效率和安全性。

PHP:处理数据库和服务器端逻辑PHP:处理数据库和服务器端逻辑Apr 15, 2025 am 12:15 AM

PHP在数据库操作和服务器端逻辑处理中使用MySQLi和PDO扩展进行数据库交互,并通过会话管理等功能处理服务器端逻辑。1)使用MySQLi或PDO连接数据库,执行SQL查询。2)通过会话管理等功能处理HTTP请求和用户状态。3)使用事务确保数据库操作的原子性。4)防止SQL注入,使用异常处理和关闭连接来调试。5)通过索引和缓存优化性能,编写可读性高的代码并进行错误处理。

您如何防止PHP中的SQL注入? (准备的陈述,PDO)您如何防止PHP中的SQL注入? (准备的陈述,PDO)Apr 15, 2025 am 12:15 AM

在PHP中使用预处理语句和PDO可以有效防范SQL注入攻击。1)使用PDO连接数据库并设置错误模式。2)通过prepare方法创建预处理语句,使用占位符和execute方法传递数据。3)处理查询结果并确保代码的安全性和性能。

PHP和Python:代码示例和比较PHP和Python:代码示例和比较Apr 15, 2025 am 12:07 AM

PHP和Python各有优劣,选择取决于项目需求和个人偏好。1.PHP适合快速开发和维护大型Web应用。2.Python在数据科学和机器学习领域占据主导地位。

PHP行动:现实世界中的示例和应用程序PHP行动:现实世界中的示例和应用程序Apr 14, 2025 am 12:19 AM

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

PHP:轻松创建交互式Web内容PHP:轻松创建交互式Web内容Apr 14, 2025 am 12:15 AM

PHP可以轻松创建互动网页内容。1)通过嵌入HTML动态生成内容,根据用户输入或数据库数据实时展示。2)处理表单提交并生成动态输出,确保使用htmlspecialchars防XSS。3)结合MySQL创建用户注册系统,使用password_hash和预处理语句增强安全性。掌握这些技巧将提升Web开发效率。

PHP和Python:比较两种流行的编程语言PHP和Python:比较两种流行的编程语言Apr 14, 2025 am 12:13 AM

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

PHP的持久相关性:它还活着吗?PHP的持久相关性:它还活着吗?Apr 14, 2025 am 12:12 AM

PHP仍然具有活力,其在现代编程领域中依然占据重要地位。1)PHP的简单易学和强大社区支持使其在Web开发中广泛应用;2)其灵活性和稳定性使其在处理Web表单、数据库操作和文件处理等方面表现出色;3)PHP不断进化和优化,适用于初学者和经验丰富的开发者。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中