PHP Redis 對象方法手冊

redis(Remote Dictionary Server)是一種Nosql技術,它是一個開源的高級kv存儲和數據結構存儲系統。mysql

redis不單單是可以存儲key和value這種簡單的鍵值對,還能存儲例如集合、hash表、列表、字典等。redis

redis在整個運行過程當中,數據通通都是存儲在內存中的,所以它的速度很是快。sql

redis會週期性的將內存中的數據寫入在磁盤中,從而實現數據持久化的訪問能力。數據庫

redis還支持主從模式以及支持經過lua腳本去編寫擴展,而且支持高可用和分佈式集羣解決方案。數組

Redis 鏈接(CONNECT)

/**
 * 建立一個 Redis 客戶端
 *
 * Redis::__construct 構造函數
 */
$redis = new Redis();

/**
 * connect 鏈接redis服務
 *
 * @param   string  $host [服務地址]
 * @param   int     $port [端口號,默認6379]
 * @param   float   $timeout [鏈接時長,默認0.0表示不限制]
 * @return  bool    [成功返回true,失敗返回false]
 */
$redis->connect('127.0.0.1', 6379, 0.0);

/**
 * pconnect 不會主動關閉的鏈接
 *
 * @param   string  $host [服務地址]
 * @param   int     $port [端口號,默認6379]
 * @param   float   $timeout [鏈接時長,默認0.0]
 * @return  bool    [成功返回true,失敗返回false]
 */
$redis->pconnect('127.0.0.1', 6379, 0.0);

/**
 * auth 使用密碼進行鏈接驗證
 *
 * @param   string  $password
 * @return  bool [驗證成功返回true,不然返回false]
 */
$redis->auth('password');

/**
 * ping 檢查當前鏈接狀態
 *
 * @return  string  [成功返回+PONG,失敗拋出RedisException]
 */
$redis->ping();

/**
 * select 切換到指定的數據庫(0-15)
 * 新鏈接老是使用0號數據庫
 *
 * @param   int     $dbindex
 * @return  bool [成功返回true,失敗返回false]
 */
$redis->select(5);

/**
 * open 鏈接服務,與connect功能相同
 *
 * @param   string  $host [服務地址]
 * @param   int     $port [端口號,默認6379]
 * @param   float   $timeout [鏈接時長,默認0.0]
 */
$redis->open('127.0.0.1');

/**
 * close 關閉鏈接,除非鏈接正在使用
 */
$redis->close();
redis-connect

Redis 鍵(KEY)

/**
 * del 移除特定的key
 *
 * @param   int|array   $key1 [須要移除的key]
 * @param   string      $key2 ...
 * @param   string      $key3 ...
 * @return  int [被刪除key的數量]
 */
$redis->del('key1', 'key2', 'key3');
$redis->del(array('key1', 'key2', 'key3'));

/**
 * keys 查找符合特定模式的key
 * '*' 匹配數據庫中全部key
 *
 * @param   string  $pattern [匹配模式]
 * @return  array [符合條件的key數組]
 */
$redis->keys('key_id_*');

/**
 * randomkey 從數據庫中隨機返回一個key
 *
 * @return  string [一個隨機的key]
 */
$redis->randomKey();

/**
 * ttl 返回給定key的剩餘生存時間(單位秒)
 *
 * @param   string  $key
 * @return  int [key剩餘有效時間]
 */
$redis->ttl('key');

/**
 * exists 檢查給定key是否存在
 *
 * @param   string  $key
 * @return  bool [若是key存在返回true,不然返回false]
 */
$redis->exists('key');

/**
 * move 將當前數據庫(默認爲0)的key移動到給定的數據庫db當中
 *
 * @param   string  $key [待移動的key]
 * @param   int     $dbindex [目標數據庫編號]
 * @return  bool [成功返回true,失敗返回false]
 */
$redis->select(0); //選擇數據庫0
$redis->move('key', 1); //將key從db0移動至db1

/**
 * rename 修改key的名稱
 *
 * @param   string  $srcKey [修改以前的名稱]
 * @param   string  $dstKey [修改以後的名稱]
 * @return  bool [成功返回true,失敗返回false]
 */
$redis->rename('srcKey', 'dstKey');

/**
 * renameNx 僅當srcKey不存在時,將srcKey更名爲dstKey
 *
 * @param   string  $srcKey
 * @param   string  $dstKey
 * @return  bool [成功返回true,失敗返回false]
 */
$redis->renameNx('srcKey', 'dstKey');

/**
 * type 返回key所存儲的值的類型
 * none(0) string(1) set(2) list(3) zset(4) hash(5)
 *
 * @param   string  $key
 * @return  int [值類型]
 */
$redis->type('key');

/**
 * expire|setTimeout 爲給定key設置生存時間(單位秒)
 * 當key過時時,它會被自動刪除
 *
 * @param   string  $key [將失效的key]
 * @param   int     $ttl [剩餘生存時間]
 * @return  bool [設置成功返回true,失敗返回false]
 */
$redis->expire('key', 10);
$redis->setTimeout('key', 10);

/**
 * expireAt 爲給定key設置到期時間(時間戳)
 *
 * @param   string  $key [將失效的key]
 * @param   int     $timestamp [UNIX時間戳]
 * @return  bool [設置成功返回true,失敗返回false]
 */
$redis->expireAt('key', time() + 60);

/**
 * persist 移除key的過時時間,key將持久保持
 * 若是key不存在或沒有設置過時時間,則操做失敗
 *
 * @param   string  $key
 * @return  bool [移除成功返回true,失敗返回false]
 */
$redis->persist('key');

/**
 * sort 給列表、集合、有序集合排序(默認遞增)
 * [BY pattern] [GET patern] [LIMIT offset count]
 * [SORT ASC|DESC] [ALPHA] [STORE destination]
 *
 * @param   string  $key
 * @param   array   $option [可選,操做]
 * @return  array [排序結果]
 */
$redis->sort('key', array(
    'BY' => 'user_id_*',    //使用外部key做爲權重比較元素
    'GET' => 'user_name_*', //獲取外部關聯key值
    'LIMIT' => array(5, 3), //從5開始取3個元素
    'ALPHA' => true,        //以UTF-8規則對字符排序
    'SORT' => 'DESC',       //以遞減模式排序
    'STORE' => 'newKey',    //將排序結果保存至newKey中
));
redis-key

Redis 字符串(STRING)

/**
 * set 將字符串value關聯到key
 * set 無視類型覆蓋原值
 *
 * @param   string  $key
 * @param   string  $value
 * @param   int     $timeout [過時時間,默認爲0]
 * @return  bool [命令成功返回true]
 */
$redis->set('key', 'value');

/**
 * setnx 當key不存在時設置key的值。
 *
 * @param   string  $key
 * @param   string  $value
 * @return  bool [成功返回true,失敗返回false]
 */
$redis->setnx('key', 'value');

/**
 * setex 將值value關聯到key,並設置key的有效時間(單位秒)
 * setex 是一個原子性操做,關聯值和設置有效時間會同時完成
 *
 * @param   string  $key
 * @param   int     $ttl
 * @param   string  $value
 * @return  bool [命令成功返回true]
 */
$redis->setex('key', 60, 'value');

/**
 * setRange 用value覆寫key中存儲的字符串(從偏移量offset開始)
 * 若key中存儲的字符串長度小於偏移量,那麼偏移量之間的空白將用零比特填充
 *
 * @param   string  $key
 * @param   int     $offset
 * @param   string  $value
 * @return  int [返回覆寫後的字符串長度]
 */
$redis->setRange('key', 5, 'value');

/**
 * mset 同時設置多個key-value對(會覆蓋舊值)
 * mset 是一個原子性操做,全部key在同一時間被設置
 *
 * @param   array   $array [key-value數組]
 * @return  bool [成功返回true,失敗返回false]
 */
$redis->mset(array('key' => 'value'));

/**
 * msetnx 同時設置多個key-value(當且僅當key不存在時)
 * msetnx 是原子性的,全部key要麼都被設置,要麼都不被設置
 *
 * @param   array $array [key-value數組]
 * @return  int 1 [成功返回1,失敗返回0]
 */
$redis->msetnx(array('key' => 'value'));

/**
 * append 將value追加到key原來的值以後
 * 若是key不存在,則簡單的將key設爲value
 *
 * @param   string  $key
 * @param   string  $value [追加的字符串]
 * @return  int [返回追加後的字符串長度]
 */
$redis->append('key', 'value');

/**
 * get 返回key所關聯的字符串值
 *
 * @param   string  $key
 * @return  string|bool [成功返回value值,失敗返回false]
 */
$redis->get('key');

/**
 * mget 返回全部給定key的值
 *
 * @param   array $array [key數組]
 * @return  array [返回values數組]
 */
$redis->mget(array('key1', 'key2', 'key3'));

/**
 * getRange 返回key總字符串的子字符串
 * 子字符串的截取範圍由start和end兩個偏移量決定
 *
 * @param   string  $key
 * @param   int     $start [開始位置]
 * @param   int     $end [結束位置]
 * @return  string [返回截取後的子串]
 */
$redis->getRange('key', 0, -1);

/**
 * getSet 將給定key的值設爲value,並返回key的舊值
 *
 * @param   string  $key
 * @param   string  $value
 * @return  string  [返回以前的value]
 */
$redis->getSet('key', 'value');

/**
 * strlen 獲取key所存儲的字符串長度
 *
 * @param   string  $key
 * @return  int [返回字符串長度]
 */
$redis->strlen('key');

/**
 * incr 將key中儲存的數字值增長一
 *
 * @param   string $key
 * @return  int [返回增長後的值]
 */
$redis->incr('key');

/**
 * incrBy 將key所儲存的值加上給定的增量值
 *
 * @param   string  $key
 * @param   int     $increment [整形增量]
 * @return  int [返回增長後的值]
 */
$redis->incrBy('key', 10);

/**
 * incrByFloat 將key所儲存的值加上給定的浮點增量值
 *
 * @param   string  $key
 * @param   float   $increment [浮點型增量]
 * @return  float [返回增長後的值]
 */
$redis->incrByFloat('key', 3.14);


/**
 * decr 將key中儲存的數字值減小一
 *
 * @param   string $key
 * @return  int [返回減小後的值]
 */
$redis->decr('key');

/**
 * decrBy 將key所儲存的值減去給定的減量值
 *
 * @param   string  $key
 * @param   int     $decrement [整形減量]
 * @return  int [返回減小後的值]
 */
$redis->decrBy('key', 10);


/**
 * setBit 設置或清除字符串指定偏移量上的位(bit)
 * 位的設置或清除取決於value參數,能夠是0或1
 *
 * @param   string  $key
 * @param   int     $offset [偏移量]
 * @param   int     $value [位設置 0或1]
 * @return  int [返回偏移量上原來的比特值 0或1]
 */
$redis->setBit('key', 5, 0);

/**
 * getBit 獲取字符串指定偏移量上的位(bit)
 * 當offset大於字符串長度,或key不存在時,返回0
 *
 * @param   string  $key
 * @param   int     $offset [偏移量]
 * @return  int [返回偏移量上的比特值 0或1]
 */
$redis->getBit('key', 5);
redis-string

Redis 哈希表(HASH)

/**
 * hSet 將哈希表key中的域field的值設爲value
 * 若是field已經存在於哈希表中,舊值將被覆蓋
 *
 * @param   string  $key [key 鍵]
 * @param   string  $hashKey [field 字段]
 * @param   string  $value [value 值]
 * @return  int [新值返回1,覆蓋返回0,錯誤返回error]
 */
$redis->hSet('key', 'field', 'value');

/**
 * hSetNx 爲哈希表中添加一個新值
 * 若域field已存在,則該操做無效
 *
 * @param   string  $key
 * @param   string  $hashKey
 * @param   string  $value
 * @return  bool [設置成功返回true,字段已經存在返回false]
 */
$redis->hSetNx('key', 'field', 'value');

/**
 * hMset 爲哈希表同時設置多個field-value
 * 此命令會覆蓋哈希表中已存在的域
 *
 * @param   string  $key
 * @param   array   $hashKeys [field-value 數組]
 * @return  bool [成功返回true,失敗返回false]
 */
$redis->hMset('key', array('field' => 'value'));

/**
 * hGet 返回哈希表key中給定域field的值
 *
 * @param   string  $key
 * @param   string  $hashKey
 * @return  string|bool [返回字段值,若失敗則返回false]
 */
$redis->hGet('key', 'field');

/**
 * hMGet 獲取哈希表key中多個域的值
 *
 * @param   string  $key
 * @param   array   $hashKeys
 * @return  array [返回 field-value 數組]
 */
$redis->hMGet('key', array('field1', 'field2', 'field3'));

/**
 * hGetAll 獲取哈希表中全部的域和值
 *
 * @param   string $key
 * @return  array [返回 field-value 數組]
 */
$redis->hGetAll('key');

/**
 * hDel 刪除哈希表中的指定域
 * 不存在的域將被忽略
 *
 * @param   string $key
 * @param   string $hashKey1
 * @param   string $hashKey2
 * @param   string $hashKeyN
 * @return  int [返回被刪除字段的數量]
 */
$redis->hDel('key', 'field');

/**
 * hLen 獲取哈希表的長度
 * 若是key不存在,或不是hash,則返回false
 *
 * @param   string  $key
 * @return  int [返回字段數量]
 */
$redis->hLen('key');

/**
 * hExists 查看哈希表中給定字段是否存在
 *
 * @param   string  $key
 * @param   string  $hashKey
 * @return  bool [字段存在返回true,不然返回false]
 */
$redis->hExists('key', 'filed');

/**
 * hIncrBy 爲哈希表key中的指定字段的整數值加上增量
 *
 * @param   string  $key
 * @param   string  $hashKey
 * @param   int     $increment [整形增量]
 * @return  int [返回增長後的值]
 */
$redis->hIncrBy('key', 'field', 10);

/**
 * hIncrByFloat 爲哈希表key中的指定字段的浮點數值加上增量
 *
 * @param   string  $key
 * @param   string  $field
 * @param   float   $increment [浮點型增量]
 * @return  float [返回增長後的值]
 */
$redis->hIncrByFloat('key', 'field', 3.14);

/**
 * hKeys 獲取全部哈希表中的字段
 *
 * @param   string  $key
 * @return  array [返回field數組]
 */
$redis->hKeys('key');

/**
 * hVals 獲取哈希表中全部值
 *
 * @param   string  $key
 * @return  array [返回value數組]
 */
$redis->hVals('key');
redis-hash

Redis 列表(LIST)

/**
 * lPush 將一個或多個值插入到列表的表頭
 * 每一個value值將按照從左到右的順序依次插入到表頭
 *
 * @param   string  $key
 * @param   string  $value1  String [插入列表的值]
 * @param   string  $value2  Optional
 * @param   string  $valueN  Optional
 * @return  int|bool [成功返回列表長度,失敗返回false]
 */
$redis->lPush('key', 'value1', 'value2', 'value3');

/**
 * lPushx 將一個或多個值插入到已存在的列表頭部
 *
 * @param   string  $key
 * @param   string  $value [插入列表的值]
 * @return  int|bool [成功返回列表長度,失敗返回false]
 */
$redis->lPushx('key', 'value');

/**
 * rPush 將一個或多個值插入到列表的表尾
 * 每一個value值將按照從左到右的順序依次插入到表尾
 *
 * @param   string  $key
 * @param   string  $value1 String [插入列表的值]
 * @param   string  $value2 Optional
 * @param   string  $valueN Optional
 * @return  int|bool [成功返回列表長度,失敗返回false]
 */
$redis->rPush('key', 'value1', 'value2', 'value3');

/**
 * rPushx 將一個或多個值插入到已存在的列表尾部
 *
 * @param   string  $key
 * @param   string  $value String [插入列表的值]
 * @return  int|bool [成功返回列表長度,失敗返回false]
 */
$redis->rPushx('key', 'value');

/**
 * lPop 移出並獲取列表的第一個元素
 *
 * @param   string $key
 * @return  string [返回移除的元素]
 */
$redis->lPop('key');

/**
 * rPop 移除並獲取列表最後一個元素
 *
 * @param   string $key
 * @return  string [返回移除的元素]
 */
$redis->rPop('key');

/**
 * lLen 獲取列表的長度
 *
 * @param   string  $key
 * @return  int [key存在時返回列表長度]
 */
$redis->lLen('key');

/**
 * lRange 獲取列表指定範圍內的元素
 *
 * @param   string  $key
 * @param   int     $start
 * @param   int     $end
 * @return  array [返回特定範圍內的元素]
 */
$redis->lRange('key', 0, -1);

/**
 * lRem 根據參數count的值,移除列表中與參數value相等的元素
 * count > 0:從表頭開始向表尾搜索,移除與value相等的元素,數量爲count
 * count < 0: 從表尾開始向表頭搜索,移除與value相等的元素,數量爲count的絕對值
 * count = 0: 移除表中全部與value相等的值
 *
 * @param   string  $key
 * @param   string  $value
 * @param   int     $count
 * @return  int     [返回被移除元素的數量]
 */
$redis->lRem('key', 'value', 0);

/**
 * lSet 將列表key下標爲index的元素的值設置爲value
 * 當index超出長度範圍,返回false
 *
 * @param string    $key
 * @param int       $index
 * @param string    $value
 * @return BOOL [設置成功返回true,不然返回false]
 */
$redis->lSet('key', 0, 'value');

/**
 * lTrim 讓列表只保留指定區間內的元素
 *
 * @param string    $key
 * @param int       $start
 * @param int       $stop
 * @return array    Bool [當key不是列表類型時返回false]
 */
$redis->lTrim('key', 0, -1);

/**
 * lIndex 經過索引設置列表元素的值
 *
 * @param string    $key
 * @param int       $index
 * @return String [返回下標爲index的元素]
 */
$redis->lIndex('key', 0);

/**
 * lInsert 將值value插入到列表key當中,位於值pivot以前或以後
 * 若是沒有找到pivot,返回-1。若是key爲空列表,返回0
 *
 * @param   string  $key
 * @param   int     $position Redis::BEFORE | Redis::AFTER
 * @param   string  $pivot
 * @param   string  $value
 * @return  int [返回插入完成後的列表長度]
 */
$redis->lInsert('key', Redis::BEFORE, 'pivot', 'value');

/**
 * rpoplpush 移除列表的最後一個元素,並將該元素添加到另外一個列表並返回
 * rpoplpush 將在一個原子時間內同時執行移除和插入兩個操做
 *
 * @param   string  $srcKey
 * @param   string  $dstKey
 * @return  string  [返回移除插入成功的元素]
 */
$redis->rpoplpush('srcKey', 'dstKey');
redis-list

Redis 集合(SET)

/**
 * sAdd 將一個或多個member元素加入到集合key當中
 * 已經存在於集合的member元素將被忽略
 *
 * @param   string  $key [集合key]
 * @param   string  $value1 [待添加值]
 * @param   string  $value2 [可選值]
 * @param   string  $valueN [可選值]
 * @return  int [返回被添加到集合中新成員的數量]
 */
$redis->sAdd('key', 'value1', 'value2', 'value3');

/**
 * sRem 移除集合key中的一個或多個member元素
 * 不存在的member元素會被忽略
 *
 * @param   string  $key
 * @param   string  $member1
 * @param   string  $member2
 * @param   string  $memberN
 * @return  int [被成功移除的元素數量]
 */
$redis->sRem('key', 'member1', 'member2', 'member3');

/**
 * sMembers 返回集合key中的全部成員
 *
 * @param   string  $key
 * @return  array [返回集合中全部成員]
 */
$redis->sMembers('key');

/**
 * sIsMember 判斷member元素是不是集合key的成員
 *
 * @param   string  $key
 * @param   string  $value
 * @return  bool [成員存在返回true,不然返回false]
 */
$redis->sIsMember('key', 'value');

/**
 * sCard 獲取集合中成員的數量
 *
 * @param   string  $key
 * @return  int [返回集合的大小,key不存在時返回0]
 */
$redis->sCard('key');

/**
 * sMove 將member元素從source集合移動到destination集合
 * 若是source集合不存在或不包含指定的member元素,則不執行任何操做
 *
 * @param   string  $srcKey
 * @param   string  $dstKey
 * @param   string  $member
 * @return  bool [操做成功返回true,成員不存在返回false]
 */
$redis->sMove('srcKey', 'dstKey', 'member');

/**
 * sPop 移除並返回集合中的一個隨機元素
 * 若是集合不存在或爲空時,返回false
 *
 * @param   string  $key
 * @return  string [返回被彈出的元素]
 */
$redis->sPop('key');

/**
 * sRandMember 獲取集合中的任意一個成員
 *
 * @param   string  $key
 * @return  string [返回一個集合中的隨機元素]
 */
$redis->sRandMember('key');

/**
 * sInter 獲取全部給定集合的交集
 *
 * @param   string  $key1 [進行相交運算的集合]
 * @param   string  $key2  ...
 * @param   string  $keyN  ...
 * @return  array [返回全部交集成員]
 */
$redis->sInter('key1', 'key2', 'key3');

/**
 * sInterStore 將給定集合的交集保存到dstKey集合
 * 若是dstKey集合已存在,則將其覆蓋
 *
 * @param   string  $dstKey [存儲運算結果的集合]
 * @param   string  $key1 [進行相交運算的集合]
 * @param   string  $key2 ...
 * @param   string  $keyN ...
 * @return  int [返回結果集中成員的數量]
 */
$redis->sInterStore('dstKey', 'key1', 'key2', 'key3');

/**
 * sUnion 獲取全部給定集合的並集
 *
 * @param   string  $key1 [進行聯合運算的集合]
 * @param   string  $key2 ...
 * @param   string  $keyN ...
 * @return  array [返回全部並集成員]
 */
$redis->sUnion('key1', 'key2', 'key3');

/**
 * sUnionStore 將給定集合的並集保存到dstKey集合
 * 若是dstKey集合已存在,則將其覆蓋
 *
 * @param   string  $dstKey [存儲運算結果的集合]
 * @param   string  $key1 [進行聯合運算的集合]
 * @param   string  $key2    ...
 * @param   string  $keyN    ...
 * @return  int [返回結果集中成員的數量]
 */
$redis->sUnionStore('dstKey', 'key1', 'key2', 'key3');

/**
 * sDiff 獲取全部給定集合的差集
 *
 * @param   string  $key1 [進行相減運算的集合]
 * @param   string  $key2 ...
 * @param   string  $keyN ...
 * @return  array [返回全部差集成員]
 */
$redis->sDiff('key1', 'key2', 'key3');

/**
 * sDiffStore 將給定集合的差集保存到dstKey集合
 * 若是dstKey集合已存在,則將其覆蓋
 *
 * @param   string  $dstKey [存儲運算結果的集合]
 * @param   string  $key1 [進行相減運算的集合]
 * @param   string  $key2      ...
 * @param   string  $keyN      ...
 * @return  int [返回結果集中成員的數量]
 */
$redis->sDiffStore('dstKey', 'key1', 'key2', 'key3');
redis-set

Redis 有序集合(ZSET)

/**
 * zAdd 將一個或多個member元素及其score值加入到有序集key當中
 *
 * @param   string  $key
 * @param   float   $score1 [用於排序的分數]
 * @param   string  $value1 [須要添加的元素]
 * @return  int [返回被成功添加的新成員數量]
 */
$redis->zAdd('key', 1.5, 'value');

/**
 * zRem 移除有序集key中的一個或多個成員
 * 不存在的成員將被忽略
 *
 * @param   string  $key
 * @param   string  $member1
 * @param   string  $member2
 * @param   string  $memberN
 * @return  int [返回被成功移除的成員數量]
 */
$redis->zRem('key', 'member1', 'member2', 'member3');

/**
 * zCard 獲取有序集合中的成員數量
 *
 * @param   string  $key
 * @return  int [返回有序集的基數]
 */
$redis->zCard('key');

/**
 * zCount 計算在有序集合中指定區間分數的成員數
 *
 * @param   string  $key
 * @param   string  $start
 * @param   string  $end
 * @return  int [返回特定範圍內的成員數量]
 */
$redis->zCount('key', 0, -1);

/**
 * zScore 獲取有序集合中指定成員的分數值
 *
 * @param   string  $key
 * @param   string  $member
 * @return  float  [返回member成員的score值]
 */
$redis->zScore('key', 'member');

/**
 * zIncrBy 爲成員member的score值加上增量,增量能夠爲負值
 *
 * @param   string  $key
 * @param   float   $value [浮點型增量]
 * @param   string  $member
 * @return  float  [返回member成員的新score值]
 */
$redis->zIncrBy('key', 1.5, 'member');

/**
 * zRange 經過索引區間(score遞增)返回指定區間內的成員
 * 經過withscores選項,可讓member與score一併返回
 *
 * @param   string  $key
 * @param   int     $start
 * @param   int     $end
 * @param   bool    $withscores
 * @return  array [返回指定區間內的成員]
 */
$redis->zRange('key', 0, -1);

/**
 * zRevRange 經過索引區間(score遞減)返回指定區間內的成員
 * 經過withscores選項,可讓member與score一併返回
 *
 * @param   string  $key
 * @param   int     $start
 * @param   int     $end
 * @param   bool    $withscore
 * @return  array [返回指定區間內的成員]
 */
$redis->zRevRange('key', 0, -1);

/**
 * zRangeByScore 返回有序集合(score遞增)score處於[min,max]區間的成員
 *
 * @param   string  $key
 * @param   float   $min [分數最小值]
 * @param   float   $max [分數最大值]
 * @param   array   $options [可選,限制結果數量與形式]
 * @return  array [返回分數在指定區間內的成員]
 */
$redis->zRangeByScore('key', 1.0, 2.0, array(
    'limit' => array(5, 3), // offset 5,count 3
    'withscores' => true,
));

/**
 * zRevRangeByScore 返回有序集合(score遞減)score處於[min,max]區間的成員
 *
 * @param   string  $key
 * @param   float   $min [分數最小值]
 * @param   float   $max [分數最大值]
 * @param   array   $options [可選,限制結果數量與形式]
 * @return  array [返回分數在指定區間內的成員]
 */
$redis->zRevRangeByScore('key', 1.0, 2.0, array(
    'limit' => array(5, 3), // offset 5,count 3
    'withscores' => true,
));

/**
 * zRank 返回有序集合(score遞增)中指定成員的排名
 *
 * @param   string  $key
 * @param   string  $member
 * @return  int [返回member在有序集合中的索引]
 */
$redis->zRank('key', 'member');

/**
 * zRevRank 返回有序集合(score遞減)中指定成員的排名
 *
 * @param   string  $key
 * @param   string  $member
 * @return  int [返回member在有序集合中的索引]
 */
$redis->zRevRank('key', 'member');

/**
 * zRemRangeByRank 移除有序集合中給定的排名區間的全部成員
 *
 * @param   string  $key
 * @param   int     $start
 * @param   int     $end
 * @return  int [被成功移除的成員數量]
 */
$redis->zRemRangeByRank('key', 0, -1);

/**
 * zRemRangeByScore 移除有序集合中給定的分數區間的全部成員
 *
 * @param   string  $key
 * @param   float   $min [分數最小值]
 * @param   float   $max [分數最大值]
 * @return  int [被成功移除的成員數量]
 */
$redis->zRemRangeByScore('key', 1.0, 2.0);
redis-zset

Redis 服務(SERVER)

/**
 * bgrewriteaof 異步執行AOF文件重寫操做
 *
 * @return  bool [操做成功返回true,失敗返回false]
 */
$redis->bgrewriteaof();

/**
 * bgsave 在後臺異步保存當前數據庫的數據到磁盤
 *
 * @return  bool [操做成功返回true,失敗返回false]
 */
$redis->bgsave();

/**
 * save 同步保存當前數據庫的數據到磁盤
 *
 * @return  bool [操做成功返回true,失敗返回false]
 */
$redis->save();

/**
 * lastSave 返回最近一次將數據保存到磁盤上的時間
 *
 * @return  int [返回UNIX時間戳]
 */
$redis->lastSave();

/**
 * dbSize 獲取當前數據庫的大小
 *
 * @return int [返回當前數據庫的key的數量]
 */
$redis->dbSize();

/**
 * slaveof 將當前服務器轉變爲指定服務器的從屬服務器
 *
 * @param   string  $host
 * @param   int $port
 * @return  bool [操做成功返回true,失敗返回false]
 */
$redis->slaveof('10.0.1.7', 6379);

/**
 * flushAll 清空整個 Redis 服務器的數據
 *
 * @return  bool [老是返回true]
 */
$redis->flushAll();

/**
 * flushDB 清空當前數據庫中的全部數據
 *
 * @return  bool [老是返回true]
 */
$redis->flushDB();

/**
 * info 關於 Redis 服務器的各類信息和統計值
 *
 * @return string
 */
$redis->info();
redis-server

==================Redis使用Demo=======================緩存

/* 鏈接MySQL */
$mysqli = new mysqli();
$mysqli->connect('127.0.0.1', 'root', '', 'cms');

/* 鏈接Redis */
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

/* 查詢數據 */
$user_data = $mysqli->query('SELECT * FROM user')
    ->fetch_all(MYSQLI_ASSOC);

/* 緩存數據 */
foreach ($user_data as $data) {
    $redis->zAdd('user', $data['id'], $data['username']);
    $redis->hMset('user:' . $data['username'], $data);
}

/* 設置有效時間 */
$redis->expire('user', 60);
foreach ($user_data as $data) {
    $redis->expire('user:' . $data['username'], 60);
}

$user_data = null; //清空從mysql中查詢的數據

/* 取出緩存數據 */
$user_set = $redis->zRange('user', 0, -1);
foreach ($user_set as $username) {
    $user_data[] = $redis->hGetAll('user:' . $username);
}

print_r($user_data);
相關文章
相關標籤/搜索