摘要:發(fā)布,新增隊(duì)列驅(qū)動,緩存驅(qū)動移動至,使用老版本的需要修改中緩存驅(qū)動加載位置。目前支持根據(jù)獲取配置的基本讀寫等。和可以繼續(xù)使用自帶的驅(qū)動,兩者互不影響。下一步如有需要可以繼續(xù)完善這兩部分的驅(qū)動。
歡迎關(guān)注我的博客 http://targetliu.com
Lumen的確是一款適合做API,速度很快的框架。但是在項(xiàng)目中使用Redis時發(fā)現(xiàn)Lumen默認(rèn)使用的 predis/predis 會拖慢整體速度,特別是在高并發(fā)的情況下,所以尋思著使用 PhpRedis 代替,畢竟 PhpRedis 是C語言寫的模塊,性能上肯定優(yōu)于 predis
文中例子已經(jīng)整理成一個 composer 包,文末有簡單介紹。
[TargetLiu/PHPRedis]
2016.7.29:v1.1.0發(fā)布,新增隊(duì)列驅(qū)動,緩存驅(qū)動移動至 TargetLiuPHPRedisCache ,使用老版本的需要修改 bootstrap/app.php 中緩存驅(qū)動加載位置。
https://github.com/TargetLiu/PHPRedis
https://packagist.org/packages/targetliu/phpredis
編譯安裝PhpRedis由于 PhpRedis 是C語言寫的模塊,需要編譯安裝。安裝方法網(wǎng)上一搜一大把,請根據(jù)自己的環(huán)境選擇相應(yīng)的方法安裝即可。
兩個可能用得上的鏈接:
PECL - PhpRedis
GitHub - PhpRedis
Lumen中使用PhpRedis很簡單,只需要在 bootstrap/app.php 中添加下列代碼將PhpRedis注入容器即可:
$app->singleton("phpredis", function(){ $redis = new Redis; $redis->pconnect("127.0.0.1"); //建立連接 $redis->select(1); //選擇庫 $redis->auth("xxxx"); //認(rèn)證 return $redis; }); unset($app->availableBindings["redis"]);
綁定后即可通過 app("phpredis") 直接使用 PhpRedis 了,具體使用方法可以看相應(yīng)的官方文檔。
Lumen中為PhpRedis增加Cache驅(qū)動由于實(shí)際使用中更多的將Redis用于緩存,Lumen自帶的Redis緩存驅(qū)動是基于 predis/predis 實(shí)現(xiàn),我們現(xiàn)在新建一個驅(qū)動以支持 Phpredis
新增Cache驅(qū)動詳細(xì)方法可以查看 官方文檔,這里指羅列一些關(guān)鍵的點(diǎn)。更多的內(nèi)容也可以查看 TargetLiu/PHPRedis
我們首先創(chuàng)建一個 ServiceProvider :
make("phpredis"), $app->config["cache.prefix"])); }); } /** * Register bindings in the container. * * @return void */ public function register() { // } }
這樣就建立一個名為 phpreids 的驅(qū)動。再創(chuàng)建一個基于 IlluminateContractsCacheStore 契約的緩存操作類用以操作 PhpRedis
redis = $redis; $this->setPrefix($prefix); } /** * Retrieve an item from the cache by key. * * @param string|array $key * @return mixed */ public function get($key) { if (!is_null($value = $this->connection()->get($this->prefix . $key))) { return is_numeric($value) ? $value : unserialize($value); } } /** * Retrieve multiple items from the cache by key. * * Items not found in the cache will have a null value. * * @param array $keys * @return array */ public function many(array $keys) { $return = []; $prefixedKeys = array_map(function ($key) { return $this->prefix . $key; }, $keys); $values = $this->connection()->mGet($prefixedKeys); foreach ($values as $index => $value) { $return[$keys[$index]] = is_numeric($value) ? $value : unserialize($value); } return $return; } /** * Store an item in the cache for a given number of minutes. * * @param string $key * @param mixed $value * @param int $minutes * @return void */ public function put($key, $value, $minutes) { $value = is_numeric($value) ? $value : serialize($value); $this->connection()->set($this->prefix . $key, $value, (int) max(1, $minutes * 60)); } /** * Store multiple items in the cache for a given number of minutes. * * @param array $values * @param int $minutes * @return void */ public function putMany(array $values, $minutes) { foreach ($values as $key => $value) { $this->put($key, $value, $minutes); } } /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function increment($key, $value = 1) { return $this->connection()->incrBy($this->prefix . $key, $value); } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function decrement($key, $value = 1) { return $this->connection()->decrBy($this->prefix . $key, $value); } /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return void */ public function forever($key, $value) { $value = is_numeric($value) ? $value : serialize($value); $this->connection()->set($this->prefix . $key, $value); } /** * Remove an item from the cache. * * @param string $key * @return bool */ public function forget($key) { return (bool) $this->connection()->delete($this->prefix . $key); } /** * Remove all items from the cache. * * @return void */ public function flush() { $this->connection()->flushDb(); } /** * Get the Redis connection instance. * * @return PredisClientInterface */ public function connection() { return $this->redis; } /** * Get the Redis database instance. * * @return IlluminateRedisDatabase */ public function getRedis() { return $this->redis; } /** * Get the cache key prefix. * * @return string */ public function getPrefix() { return $this->prefix; } /** * Set the cache key prefix. * * @param string $prefix * @return void */ public function setPrefix($prefix) { $this->prefix = !empty($prefix) ? $prefix . ":" : ""; } }
通過以上兩個步驟基本上就完成了Cache驅(qū)動的創(chuàng)建,現(xiàn)在只需要在 bootstrap/app.php 中注入新建的Cache驅(qū)動然后配置 .env 中 CACHE_DRIVER = phpredis ,最后再在 config/cache.php 中加入相應(yīng)的驅(qū)動代碼即可
"phpredis" => [ "driver" => "phpredis" ],
Cache的使用請查看Lumen官方文檔
一個基于PhpRedis的Lumen擴(kuò)展包[TargetLiu/PHPRedis]
https://github.com/TargetLiu/PHPRedis
https://packagist.org/packages/targetliu/phpredis
安裝:composer require targetliu/phpredis
安裝及使用方法請看 README
這個包只是我做的一個簡單示例,引入了 PhpRedis 并做了最簡單的緩存驅(qū)動。目前支持根據(jù) .env 獲取Redis配置、Cache的基本讀寫等。
Session和Queue可以繼續(xù)使用Lumen自帶的Redis驅(qū)動,兩者互不影響。下一步如有需要可以繼續(xù)完善這兩部分的驅(qū)動。
這個包將根據(jù)自己工作需求以及大家的已經(jīng)進(jìn)一步完善。
歡迎大家提出意見共同完善。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/21789.html
摘要:配置項(xiàng)用于配置失敗隊(duì)列任務(wù)存放的數(shù)據(jù)庫及數(shù)據(jù)表。要使用隊(duì)列驅(qū)動,需要在配置文件中配置數(shù)據(jù)庫連接。如果應(yīng)用使用了,那么可以使用時間或并發(fā)來控制隊(duì)列任務(wù)。你可以使用命令運(yùn)行這個隊(duì)列進(jìn)程。如果隊(duì)列進(jìn)程意外關(guān)閉,它會自動重啟啟動隊(duì)列進(jìn)程。 一、概述 在Web開發(fā)中,我們經(jīng)常會遇到需要批量處理任務(wù)的場景,比如群發(fā)郵件、秒殺資格獲取等,我們將這些耗時或者高并發(fā)的操作放到隊(duì)列中異步執(zhí)行可以有效緩解系...
摘要:什么是官網(wǎng)是一個由組件搭建而成的微框架是當(dāng)前最快的框架之一在什么時候使用專為微服務(wù)或者設(shè)計舉個例子如果你的應(yīng)用里面有部分業(yè)務(wù)邏輯的請求頻率比較高就可以單獨(dú)把這部分業(yè)務(wù)邏輯拿出來使用來構(gòu)建一個小因?yàn)槭菍?yōu)化了框架的加載機(jī)制所以對資源的要求少很 什么是 Lumen?官網(wǎng) lumen 是一個由 Laravel 組件搭建而成的微框架,是當(dāng)前最快的 PHP 框架之一! 在什么時候使用 Lume...
摘要:的現(xiàn)狀目前是版本,是基于開發(fā)。入口文件啟動文件和配置文件框架的入口文件是。在路由中指定控制器類必須寫全命名空間,不然會提示找不到類。目前支持四種數(shù)據(jù)庫系統(tǒng)以及。使用時發(fā)生錯誤,因?yàn)樵谖募?,的默認(rèn)驅(qū)動是。 最近使用 Lumen 做了 2 個業(yè)余項(xiàng)目,特此記錄和分享一下。 Lumen 的介紹 在使用一項(xiàng)新的技術(shù)時,了解其應(yīng)用場景是首要的事情。 Lumen 的口號:為速度而生的 La...
閱讀 2664·2021-11-25 09:43
閱讀 684·2021-11-12 10:36
閱讀 4654·2021-11-08 13:18
閱讀 2194·2021-09-06 15:00
閱讀 3127·2019-08-30 15:56
閱讀 946·2019-08-30 13:57
閱讀 2002·2019-08-30 13:48
閱讀 1426·2019-08-30 11:13