[TOC]
開啟 redis 擴(kuò)展
- php.ini
[redis]
extension=redis
redis.pconnect.pooling_enabled = 1
相關(guān)命令
redis 類
$redis = new Redis();
$redis = new Redis([
'host' => '127.0.0.1',
'port' => 6379,
'connectTimeout' => 2.5,
'auth' => ['root', 'password'],
'ssl' => ['verify_peer' => false],
]);
$key = 'key1';
連接
ini_set('default_socket_timeout', -1); // 設(shè)置php不斷開連接
$redis->connect('127.0.0.1', 6379);
$redis->pconnect('127.0.0.1', 6379);
$redis->pconnect('/tmp/redis.sock'); // 持久性連接斤寂,php-fpm進(jìn)程結(jié)束之前一直復(fù)用
賬號(hào)密碼
/* Authenticate with the password 'foobared' */
$redis->auth('foobared');
/* Authenticate with the username 'phpredis', and password 'haxx00r' */
$redis->auth(['phpredis', 'haxx00r']);
/* Authenticate with the password 'foobared' */
$redis->auth(['foobared']);
/* You can also use an associative array specifying user and pass */
$redis->auth(['user' => 'phpredis', 'pass' => 'phpredis']);
$redis->auth(['pass' => 'phpredis']);
數(shù)據(jù)庫選擇
$redis->select(0);
設(shè)置統(tǒng)一前綴
$redis->setOption(Redis::OPT_PREFIX, 'myAppName:'); // use custom prefix on all keys
序列化
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // Don't serialize data
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // Use built-in serialize/unserialize
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // Use igBinary serialize/unserialize
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_MSGPACK); // Use msgpack serialize/unserialize
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_JSON);
keys 數(shù)量
$redis->dbSize();
清空所有數(shù)據(jù)庫
$redis->flushAll();
清空當(dāng)前數(shù)據(jù)庫
$redis->flushDb();
同步持久化
$redis->save();
獲取 key 類型
所有類型
string: Redis::REDIS_STRING
set: Redis::REDIS_SET
list: Redis::REDIS_LIST
zset: Redis::REDIS_ZSET
hash: Redis::REDIS_HASH
other: Redis::REDIS_NOT_FOUND
$redis->type('key');
獲取過期時(shí)間
ps: If the key has no ttl, -1 will be returned, and -2 if the key doesn't exist
$redis->ttl('key'); // seconds
$redis->pttl('key'); // milliseconds
事務(wù)
mulit
命令塊作為單個(gè)事務(wù)運(yùn)行
$ret = $redis->multi()
->set('key1', 'val1')
->get('key1')
->set('key2', 'val2')
->get('key2')
->exec();
PIPELINE
只是將更快地傳輸?shù)椒?wù)器豁跑,但沒有任何原子性保證
$ret = $redis->pipeline()
->set('key1', 'val1')
->get('key1')
->set('key2', 'val2')
->get('key2')
->exec();
執(zhí)行或取消
$redis->exec(); // 執(zhí)行
$redis->discard(); // 取消
watch
在 watch 和 exec 執(zhí)行之間化戳,如果發(fā)現(xiàn)監(jiān)聽的值發(fā)生變化本橙,則執(zhí)行失敗
$redis->watch('x');
$ret = $redis->multi()
->incr('x')
->exec(); // return true or false
腳本
script
$redis->script('load', $script);
$redis->script('flush');
$redis->script('kill');
$redis->script('exists', $script1, [$script2, $script3, ...]);
eval
Parameters
- script string.
- args array, optional.
- num_keys int, optional. KEYS參數(shù)的個(gè)數(shù) args.length - key 為 ARGV 參數(shù)的個(gè)數(shù)
$redis->eval("return 1");
- eval 參數(shù)的格式
EVAL script numkeys key [key ...] arg [arg ...]
eval "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 key2 arg1 arg2 arg3
- "key1"
- "key2"
- "arg1"
- "arg2"
- "arg3"
lua 腳本
$script = <<<LUA
local interval_milliseconds = tonumber(ARGV[1])
redis.call('set', KEYS[1], 2)
redis.call('pexpire', KEYS[1], interval_milliseconds)
redis.call('decr', KEYS[1])
local current = redis.call('get', KEYS[1])
local ttl = redis.call('ttl', KEYS[1])
return {current,ttl}
LUA;
$redis->script('load', $script);
$ret = $redis->eval($script, [$key, 1], 1);
string
添加
$redis->set($key, 20); // 設(shè)置
// Will set the key, if it doesn't exist, with a ttl of 10 seconds
$redis->set($key, 'value', ['nx', 'ex'=>10]);
// Will set a key, if it does exist, with a ttl of 1000 milliseconds
$redis->set('key', 'value', ['nx', 'px'=>1000]);
// Set the string value in argument as value of the key if the key doesn't already exist in the database
$redis->setNx($key, 'value');
$redis->incr($key, 2); // 自增2
$redis->decr($key, 2); // 自減2
$redis->incr('key1', 1); /* key1 didn't exists, set to 0 before the increment, ex = -1 */
// 追加內(nèi)容
$redis->set('key', 'value1');
$redis->append('key', 'value2'); /* 12 */
$redis->get('key'); /* 'value1value2' */
獲取
$redis->get($key); // 獲取
$redis->mGet(['key1', 'key2', 'key3']); // ['value1', 'value2', 'value3'];
// 獲取部分內(nèi)容
$redis->set('key', 'string value');
$redis->getRange('key', 0, 5); /* 'string' */
$redis->getRange('key', -5, -1); /* 'value' */
// 獲取并設(shè)置
$redis->set('x', '42');
$exValue = $redis->getSet('x', 'lol'); // return '42', replaces x by 'lol'
刪除
// 刪除
$redis->del($key, 'key2'); /* return 2 */
$redis->del([$key, 'key4']); /* return 2 */
其它
// 重命名
$redis->set('x', '42');
$redis->rename('x', 'y');
$redis->get('y'); // → 42
// 存在
$redis->exists('key');
過期時(shí)間
$redis->expire($key, 3); // x will disappear in 3 seconds.
$now = time(NULL); // current timestamp
$redis->expireAt('x', $now + 3);
hash
新增
$redis->hSet('key', 'field', 'value');
$redis->hMSet('key', ['field2' => 'value2', 'field3' => 'value3']);
獲取
$redis->hGet('key', 'field'); /* returns "value" */
$redis->hMGet('key', ['field', 'field2']);
$redis->hKeys('key') // ['field', 'filed2', 'field3']
$redis->hVals('key') // ['value', 'value2', 'value3']
$redis->hGetAll('key') // ['field' => 'value', 'field2' => 'value2', 'field3' => 'value3']
刪除
$redis->hDel('key', 'field');
$redis->hDel('key', 'field', 'filed2');
其它
$redis->hExists('key', 'field');
list 列表
新增
$redis->lPush('key1', 'A'); // left push
$redis->rPush('key1', 'B'); // right push
獲取
$redis->lIndex('key1', 0); // 獲取 list 索引
$redis->lGet('key1', 0); // 獲取 list 的值
$redis->lLen('key1'); // 獲取 list 長度
彈出
$redis->lPop('key1'); // left pop
$redis->rPop('key1'); // right pop
Sets 集合(value唯一)
新增
$redis->sAdd('key1', 'member1', 'member2', 'member3'); /* 3, 'key1' => {'member1', 'member2', 'member3'}*/
獲取
$redis->sCard('key1'); /*獲取 set 長度*/
$redis->sDiff('s0', 's1', 's2') // 取差集
$redis->sMembers('key1') // ['member1', 'member2', 'member3']
刪除
$redis->sPop('key1', 2); // 隨機(jī)彈出2個(gè)元素
$redis->sRem('key1', 'member2', 'member3'); // 刪除
Sorted sets(有序集合)
每個(gè)元素都會(huì)關(guān)聯(lián)一個(gè) double 類型的分?jǐn)?shù)。redis 正是通過分?jǐn)?shù)來為集合中的成員進(jìn)行從小到大的排序
新增
$redis->zAdd('key1', 1, 'member1', 'member2', 'member3'); /* 'key1' => {'member1', 'member2', 'member3'}*/
$redis->zAdd('key1', 2, 'member4'); /* 3, 'key1' => {'member1', 'member2', 'member3', 'member4'}*/
獲取
$redis->zCard('key1'); /*獲取 set 長度*/
$redis->zScore('key', 'val2'); // 獲取元素的 score
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zRange('key1', 0, -1); // ['val0', 'val2', 'val10'] Parameters index start end
$redis->zRangeByScore('key', 0, 3); /* 根據(jù) score 獲取 ['val0', 'val2'] */ Parameters index start_score end_score
$redis->zRangeByScore('key', '-inf', '+inf', ['withscores' => TRUE]); /* ['val0' => 0, 'val2' => 2, 'val10' => 10] */
彈出或刪除
$redis->zPopMin('key1', 2); // 彈出最低分?jǐn)?shù)的2個(gè)元素
$redis->zPopMax('key1', 2); // 彈出最高分?jǐn)?shù)的2個(gè)元素
$redis->zRem('key1', 'member2', 'member3'); // 刪除
Streams
主要用于消息隊(duì)列(MQ锅减,Message Queue)
分組
$obj_redis->xGroup('HELP');
$obj_redis->xGroup('CREATE', $str_key, $str_group, $str_msg_id, [$boo_mkstream]);
$obj_redis->xGroup('SETID', $str_key, $str_group, $str_msg_id);
$obj_redis->xGroup('DESTROY', $str_key, $str_group);
$obj_redis->xGroup('DELCONSUMER', $str_key, $str_group, $str_consumer_name);
$obj_redis->xGroup('CREATE', 'mystream', 'mygroup', 0);
新增消息
消息 message ID糖儡,我們使用
*
表示由 redis 生成,可以自定義怔匣,但是要自己保證遞增性
$obj_redis->xAdd('mystream', "*", ['field' => 'value']); // return The added message ID, example: '1530063064286-0'
標(biāo)記為已處理
$obj_redis->xAck($stream, $group, $arr_messages);
$obj_redis->xAck('stream', 'group1', ['1530063064286-0', '1530063064286-1']);
刪除消息
$obj_redis->xDel($str_key, $arr_ids);
$obj_redis->xDel('mystream', ['1530115304877-0', '1530115305731-0']);
獲取消息
$obj_redis->xRead($arr_streams [, $i_count, $i_block]);
$obj_redis->xRead(['stream1' => '1535222584555-0', 'stream2' => '1535222584555-0']);
$obj_redis->xReadGroup($str_group, $str_consumer, $arr_streams [, $i_count, $i_block]);
$obj_redis->xReadGroup('mygroup', 'consumer1', ['s1' => 0, 's2' => 0]);
獲取消息列表
/* Get everything in this stream */
$obj_redis->xRange('mystream', '-', '+');
/* Only the first two messages */
$obj_redis->xRange('mystream', '-', '+', 2);
Pub/sub
subscribe 訂閱
$redis = new Redis();
$redis->connect('192.168.1.62', 6379);
$redis->setOption(Redis::OPT_READ_TIMEOUT, -1); // redis方式設(shè)置不超時(shí)
$redis->subscribe(['chan', 'chan2'], function($redis, $chan, $msg) {
echo $msg . PHP_EOL;
});
psubscribe 批量訂閱
模糊匹配
$redis->psubscribe(['my*'], function($redis, $pattern, $chan, $msg) {
echo "Pattern: $pattern\n";
echo "Channel: $chan\n";
echo "Payload: $msg\n";
});
publish 發(fā)布
$redis = new Redis();
$redis->connect('192.168.1.62', 6379);
$redis->publish('chan', 'hello, world!'); // send message.