Lumen 基於 Laravel 打造,專爲構建微服務和 APIs 而生。Lumen與Redis服務端通訊可經過Predis(PHP庫)或者PhpRedis(PHP的C擴展)來實現,建議使用PhpRedis,其性能更高。Lumen下使用Predis和PhpRedis都需引入illuminate/redis(PHP庫),illuminate/redis(PHP庫)都對Predis和PhpRedis(Laravel 5.3以上)進行了很好的封裝,但illuminate/redis(PHP庫)又依賴predis/predis(PHP庫),故安裝 illuminate/redis時會自動引入predis/predis(PHP庫)。php
Redis 與 Memcached 均爲經常使用的 key-value 分佈式內存對象緩存系統,可提供數據緩存和數據共享能力,Redis 支持持久化,而 memcached 不支持持久化,發生重啓後數據不會自動恢復。mysql
關於Memcached:laravel
A 64bit "CAS" value, which is kept unique.
下邊這段是關於文本協議「noreply」的描述,同時建議使用二進制協議:git
Most ASCII commands allow a "noreply" version. One should not normally use this with the ASCII protocol, as it is impossible to align errors with requests. The intent is to avoid having to wait for a return packet after executing a mutation command (such as a set or add). The binary protocol properly implements noreply (quiet) statements. If you have a client which supports or uses the binary protocol, odds are good you may take advantage of this.
下邊這段是關於「A Well Designed Binary Protocol Client」的描述:github
With the binary protocol, it(A Well Designed Binary Protocol Client may take many application threads and use a single TCP connection back to memcached) is possible to pack requests from different client instances into the same TCP socket, then dole back results to the right owners.
關於Redis(REmote DIctionary Server, 遠程字典服務器):SSDB支持LevelDB,是Redis的替代品,且與其兼容。web
RDB:在save、shutdown、slave時觸發寫二進制文件,粒度大,若是這些操做未完成以前crash可能致使丟失一部分數據。
經過fork一個進程,copy-on-write把整個db保存下來,而主進程不會進行任何IO操做,保證了redis的高性能。
AOF:持續把寫操做命令格式化後追加到日誌文件的尾部,粒度較小,crash以後數據丟失小。AOF支持不一樣的fsync策略,
包括無fsync、每秒fsync、請求時fsync,默認爲每秒fsync策略。fsync是由後臺線程完成的,主線程繼續努
力地執行寫請求。AOF是文本文件,一般也比RDB文件大,恢復速度慢。
一. Redis的Docker部署redis
FROM ubuntu MAINTAINER cenze <272666745@qq.com> RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ADD conf/sources.list /etc/apt/ RUN apt-get update \ && apt-get install -y gcc make vim ENV PKGS="/usr/local/pkgs" ADD packages/redis-3.2.8.tar.gz $PKGS/ # install redis ENV PREFIX_REDIS="/usr/local/redis" WORKDIR $PKGS/redis-3.2.8 RUN make \ && make PREFIX=$PREFIX_REDIS install \ && cp redis.conf $PREFIX_REDIS/ \ && cp src/redis-trib.rb $PREFIX_REDIS/bin/ VOLUME ["/data"] ENV PATH $PREFIX_REDIS/bin:$PATH EXPOSE 6379 CMD ["redis-server","/usr/local/redis/redis.conf"]
注意事項:算法
sudo docker build -t cenze/redis -f Dockerfile-Redis .
sudo docker run -d --name redis cenze/redis
redis-cli或netcat(nc)或telnet測試部署:redis-cli下輸入 help <Tab> 可按組查詢命令列表sql
root@60c9de8c01a0:/usr/local/pkgs/redis-3.2.8# redis-cli -h 127.0.0.1 127.0.0.1:6379> set cache redis OK 127.0.0.1:6379> get cache "redis" 127.0.0.1:6379>
二.建立Lumen項目docker
composer建立Lumen:
composer
沒法以 root/super 用戶來運行,因此須要切換到其餘用戶環境,好比本人會運行以下命令
su - www-data export PATH=/usr/local/php/bin:$PATH (這一條最好寫進Home下的.profile, composer依賴PHP來運行) composer create-project --prefer-dist laravel/lumen lumen
.env參數配置:
APP_ENV=local APP_DEBUG=true APP_KEY=bcee22b233721b47c6043e6bf35ac4ee APP_TIMEZONE=Asia/Shanghai DB_CONNECTION=mysql DB_HOST=[myDbHost] DB_PORT=3306 DB_DATABASE=[myDataBase] DB_USERNAME=[myUser] DB_PASSWORD=[myPassword] CACHE_DRIVER=redis QUEUE_DRIVER=sync REDIS_HOST= 172.17.0.3 REDIS_PORT= 6379
三.Lumen中啓用Predis
cd /path/to/lumen
composer require illuminate/redis (predis/predis爲illuminate/redis所依賴,故將被自動安裝上)
'redis' => [ 'client' => 'predis', //'client' => 'phpredis', 'cluster' => env('REDIS_CLUSTER', false), 'default' => [ 'host' => env('REDIS_HOST', 'localhost'), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_DATABASE', 0), 'password' => env('REDIS_PASSWORD', null), ], ],
$app->register(Illuminate\Redis\RedisServiceProvider::class); $app->withFacades();//同時啓用Facades $app->withEloquent();//同時啓用Eloquent
$app->get('/', function () use ($app) { //return $app->version(); Cache::put('lumen', 'Hello, Lumen.', 5); return Cache::get('lumen'); });
頁面輸出:Hello, Lumen.
四.Lumen中啓用PhpRedis擴展
pecl install redis (有可能須要手動安裝 autoconf,phpize依賴該工具)
composer require illuminate/redis
注意事項:該C擴展安裝完後須要修改php.ini添加行extension=redis.so。若是php在cli模式下運行未發現Redis,多是由於你的php.ini文件沒有找到,該文件爲安裝配置項--with-config-file-path所指定,默認位於PREFIX/lib目錄下,因此應在啓動php時添加-c選項指定配置文件或php.ini所在目錄。
1)修改lumen/bootstrap/app.php,添加以下代碼:
$app->singleton('redis', function(){ $redis = new Redis; $redis->pconnect('172.17.0.3'); return $redis; }); unset($app->availableBindings['redis']);
$app->get('/', function () use ($app) { //return $app->version(); app('redis')->set('lumen', 'Hello, Lumen.'); return app('redis')->get("key"); });