基于PhalApi的Redis拓展 (由@喵了個咪提供)

2018-11-21 21:33 更新

基于PhalApi的Redis拓展

前言

先在這里感謝phalapi框架創(chuàng)始人@dogstar,為我們提供了這樣一個優(yōu)秀的開源框架.

編寫本次拓展出于的目的是為了解決并不是非常熟悉redis的童鞋能夠方便的使用redis進行實際的運用
,對原生的phpredis進行的封裝優(yōu)化良好的注釋和例子希望能提供更好的幫助!

注:本拓展并沒有開發(fā)完成,也沒進行嚴格的測試,此版本為還處于開發(fā)階段的鑒賞版.

附上:

官網(wǎng)地址:http://www.phalapi.net/

開源中國Git地址:http://git.oschina.net/dogstar/PhalApi/tree/release

安裝配置redis以及phpredis

基于centos6.5

    //下redis解壓安裝
    wget http://download.redis.io/releases/redis-2.8.17.tar.gz
    tar zxvf redis-2.8.17.tar.gz
    cd redis-2.8.17
    make
    make test
    make install
    //生成6379端口以及配置文件
    cd utils
    ./install_server.sh
    Please select the redis port for this instance: [6379]
    Please select the redis config file name [/etc/redis/6379.conf]
    Please select the redis log file name [/var/log/redis_6379.log]
    Please select the data directory for this instance [/var/lib/redis/6379]
    Please select the redis executable path [/usr/local/bin/redis-server]
    //對配置文件進行配置
    vi /etc/redis/6379.conf
    databases 100                            #可以使用的庫的數(shù)量修改16為100
    masterauth xxxxxxxxxxxxx                 #連接 master 的認證密碼
    requirepass woyouwaimai76                #連接此redis的連接密碼
    :wq
    //修改關閉redis需要密碼
    vi /etc/rc.d/init.d/redis_6379
    $CLIEXEC -p $REDISPORT -a woyouwaimai76 shutdown    #stop redis需要密碼
    //重啟redis
    service redis_6379 restart
    //添加到系統(tǒng)啟動項
    chkconfig redis_6379 on

     //下載phpredis解壓安裝
     wget https://github.com/nicolasff/phpredis/archive/master.zip
     unzip master.zip -d phpredis
     cd phpredis/phpredis-master
     phpize
     ./configure
     make && make install
     //在php.ini中注冊phpredis
     extension = redis.so

     //測試
      <?php
         $auth     = 'xxxxxxxxx';
         $source   = '127.0.0.1';
         $host     = '6379';
         $redis    = new Redis();
         echo $redis->connect($host) ? "$host connect" : "$host fail";
         if($auth){
             echo $redis->auth($auth) ? " auth success" : " auth fail";
         }

注冊配置文件在Config.app文件下面

return array(
    //Redis配置項
    'redis' => array(
        //Redis緩存配置項
        'servers'  => array(
            'host'   => '127.0.0.1',        //Redis服務器地址
            'port'   => '6379',             //Redis端口號
            'prefix' => 'developers_',      //Redis-key前綴
            'auth'   => 'woyouwaimai76',    //Redis鏈接密碼
        ),
        // Redis分庫對應關系
        'DB'       => array(
            'developers' => 1,
            'user'       => 2,
            'code'       => 3,
        ),
        //使用阻塞式讀取隊列時的等待時間單位/秒
        'blocking' => 5,
    ),

);

在init入口文件注冊redis拓展

配置方式非常簡單只需要把拓展下載下來放入Library文件內(nèi)即可,然后就可以使用如下方法進行實例

//redis鏈接
DI()->redis = new Redis_Lite(DI()->config->get('app.redis.servers'));

開始使用

//存入永久的鍵值隊
DI()->redis->set_forever(鍵名,值,庫名);
//獲取永久的鍵值隊
DI()->redis->get_forever(鍵名, 庫名);

//存入一個有時效性的鍵值隊,默認600秒
DI()->redis->set_Time(鍵名,值,有效時間,庫名);
//獲取一個有時效性的鍵值隊
DI()->redis->get_Time(鍵名, 庫名);

//寫入隊列左邊
DI()->redis->set_Lpush(隊列鍵名,值, 庫名);
//讀取隊列右邊
DI()->redis->get_lpop(隊列鍵名, 庫名);
//讀取隊列右邊 如果沒有讀取到阻塞一定時間(阻塞時間或讀取配置文件blocking的值)
DI()->redis->get_Brpop(隊列鍵名,值, 庫名);

//刪除一個鍵值隊適用于所有
DI()->redis->del(鍵名, 庫名);
//自動增長
DI()->redis->get_incr(鍵名, 庫名);
//切換DB并且獲得操作實例
DI()->redis->get_redis(鍵名, 庫名);
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號