搜尋
首頁後端開發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 - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到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應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中