欢迎关注个人博客 http://targetliu.comphp
Lumen的确是一款适合作API,速度很快的框架。可是在项目中使用Redis时发现Lumen默认使用的
predis/predis
会拖慢总体速度,特别是在高并发的状况下,因此寻思着使用PhpRedis
代替,毕竟PhpRedis
是C语言写的模块,性能上确定优于predis
laravel
文中例子已经整理成一个 composer
包,文末有简单介绍。git
[TargetLiu/PHPRedis]github
2016.7.29:v1.1.0发布,新增队列驱动,缓存驱动移动至 TargetLiu\PHPRedis\Cache
,使用老版本的须要修改 bootstrap/app.php
中缓存驱动加载位置。redis
因为 PhpRedis
是C语言写的模块,须要编译安装。安装方法网上一搜一大把,请根据本身的环境选择相应的方法安装便可。并发
两个可能用得上的连接:app
PECL - PhpRediscomposer
很简单,只须要在 bootstrap/app.php
中添加下列代码将PhpRedis注入容器便可:
$app->singleton('phpredis', function(){ $redis = new Redis; $redis->pconnect('127.0.0.1'); //创建链接 $redis->select(1); //选择库 $redis->auth('xxxx'); //认证 return $redis; }); unset($app->availableBindings['redis']);
绑定后便可经过 app('phpredis')
直接使用 PhpRedis
了,具体使用方法能够看相应的官方文档。
因为实际使用中更多的将Redis用于缓存,Lumen自带的Redis缓存驱动是基于 predis/predis
实现,咱们如今新建一个驱动以支持 Phpredis
新增Cache驱动详细方法能够查看 官方文档,这里指罗列一些关键的点。更多的内容也能够查看 TargetLiu/PHPRedis
咱们首先建立一个 ServiceProvider
:
<?php namespace App\Providers; use Illuminate\Support\Facades\Cache; use Illuminate\Support\ServiceProvider; use TargetLiu\PHPRedis\PHPRedisStore; class CacheServiceProvider extends ServiceProvider { /** * Perform post-registration booting of services. * * @return void */ public function boot() { Cache::extend('phpredis', function ($app) { return Cache::repository(new PHPRedisStore($app->make('phpredis'), $app->config['cache.prefix'])); }); } /** * Register bindings in the container. * * @return void */ public function register() { // } }
这样就创建一个名为 phpreids
的驱动。再建立一个基于 Illuminate\Contracts\Cache\Store
契约的缓存操做类用以操做 PhpRedis
<?php namespace TargetLiu\PHPRedis; use Illuminate\Contracts\Cache\Store; class PHPRedisStore implements Store { /** * The Redis database connection. * * @var \Illuminate\Redis\Database */ protected $redis; /** * A string that should be prepended to keys. * * @var string */ protected $prefix; /** * Create a new Redis store. * * @param \Illuminate\Redis\Database $redis * @param string $prefix * @return void */ public function __construct($redis, $prefix = '') { $this->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 \Predis\ClientInterface */ public function connection() { return $this->redis; } /** * Get the Redis database instance. * * @return \Illuminate\Redis\Database */ 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驱动的建立,如今只须要在 bootstrap/app.php
中注入新建的Cache驱动而后配置 .env
中 CACHE_DRIVER = phpredis
,最后再在 config/cache.php
中加入相应的驱动代码便可
'phpredis' => [ 'driver' => 'phpredis' ],
Cache的使用请查看Lumen官方文档
[TargetLiu/PHPRedis]
安装:composer require targetliu/phpredis
安装及使用方法请看 README
这个包只是我作的一个简单示例,引入了 PhpRedis
并作了最简单的缓存驱动。目前支持根据 .env
获取Redis配置、Cache的基本读写等。
Session和Queue能够继续使用Lumen自带的Redis驱动,二者互不影响。下一步若有须要能够继续完善这两部分的驱动。
这个包将根据本身工做需求以及你们的已经进一步完善。
欢迎你们提出意见共同完善。