redis 中国語マニュアル: http://readthedocs.org/docs/redis/en/latest/?
この記事は『redis 中国語マニュアル』を参照し、php-redis と redis_cli の違い(主に戻り値の型とパラメータの使い方)に注意して PHP を使用してサンプルコードを実装してください。
ディレクトリ (クイック検索コマンドには CTRL F を使用します):
|
|
|
|
|
- String(String)
- SET
- SETNX SETEX
- SETRANGE
- MSET
- MSETNX
- APPEND
- GET
- MGET
- ゲットレンジ
- ゲットセット
- ストラレン INCR
- INCRBY
- DECR
- デクラビー
- セットビット
- ゲットビット
- ハッシュ テーブル (ハッシュ)
- HSET
- HSETNX
- HMSET
- HGET
- HMGET
- HGETALL
- HDEL
- HLEN HEXISTS
- HINCRBY
- HKEYS
- HVALS
- テーブル (リスト)
- LPUSH
- LPUSHX
- RPUSH
- RPUSHX
- LPOP
- RPOP
- BLPOP
- BRPOP
- LLEN
- LRANGE LREM
- LSET
- LTRIM
- LINDEX
- LINSERT
- RPOPLPUSH
- BRPOPLPUSH
- 設定
- SADD
- SREM
- メンバー
- システムメンバー スカード
- スムーブ
- ストップ
- SRANDMEMBER
- SINTER
- SINTERSTORE
- SUNION
- SUNIONSTORE
- SDIFF
- SDIFFSTORE
|
|
|
|
|
phpredis は redis の php の拡張機能であり、非常に効率的で、メモリレベルのモジュールのビジネス関係を作成するのに非常に役立ちます。
; redis が公式に提供するコマンド使用上のヒント:
ダウンロード アドレスは次のとおりです:
https://github.com/owlient/phpredis (redis 2.0.4 をサポート)
Redis::__construct コンストラクター
$redis = new Redis();
connect、オープンリンク Redis サービス
パラメータ
ホスト: 文字列、サービス アドレス
port: int、ポート番号
timeout: float、リンク期間 (オプション、デフォルトは 0、リンク時間に制限なし)
注: redis.conf にも時間があります。デフォルトは 300
pconnect、popen 自動的に閉じられないリンク
上記を参照
redis モードを設定するには setOption
redis 設定を表示するには getOptionモード
ping? 接続ステータスの表示
?
KEY 関連の操作
DEL
指定されたキーを削除します。
キーが存在しない場合、コマンドは無視されます。
時間計算量:?
<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 パターン? 指定されたパターンに一致する key を検索します。特殊符号用"\"隔开
时间复杂度: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)返回值:?
<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>
?
TTLTTL key
返回给定key的剩余生存时间(time to live)(以秒为单位)。
时间复杂度:O(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)返回值:<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)返回值:?
<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)。
?
?
时间复杂度:O(1)返回值:<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 timestampEXPIREAT的作用和EXPIRE一样,都用于为key设置生存时间。
不同在于EXPIREAT命令接受的时间参数是UNIX时间戳(unix timestamp)。
时间复杂度:O(1)返回值:?
<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对象。
OBJECT命令有多个子命令:
-
OBJECT?REFCOUNT?
返回给定key引用所储存的值的次数。此命令主要用于除错。 -
OBJECT?ENCODING?
返回给定key锁储存的值所使用的内部表示(representation)。 -
OBJECT?IDLETIME?
返回给定key自储存以来的空转时间(idle, 没有被读取也没有被写入),以秒为单位。
- 字符串可以被编码为raw(一般字符串)或int(用字符串表示64位数字是为了节约空间)。
- 列表可以被编码为ziplist或linkedlist。ziplist是为节约大小较小的列表空间而作的特殊表示。
- 集合可以被编码为intset或者hashtable。intset是只储存数字的小集合的特殊表示。
- 哈希表可以编码为zipmap或者hashtable。zipmap是小哈希表的特殊表示。
- 有序集合可以被编码为ziplist或者skiplist格式。ziplist用于表示小的有序集合,而skiplist则用于表示任何大小的有序集合。
<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('ゲーム',"すごい"); <span style="color: #008000; line-height: 1.5 !important;">#</span><span style="color: #00"></span>