laravel利用swoole扩展封装redis连接池以及一键协程化

  • redisPool 封装目录结构

├─redis_pool --------- 主目录
│ ├─redis
│ │ ├─RedisOp.php ------ redis操作类
│ │ ├─RedisBase.php ------ redis操作基类
│ ├─pool
│ │ ├─RedisPool.php ------- Redis连接池封装类
│ ├─Redis.php ---------------- swoole一键协程封装

一、封装redis操作类

  • RedisBase.php

/**
* redis操作基类
*/
namespace App\redis_pool\redis;
Class RedisBase{
	//连接池
    protected $pool;
    //Redis连接
    protected $redis;
    public function __construct($pool)
    {
        $this->pool = $pool;
    }
    public function connection()
    {
        return $this->pool->get();
    }
    /**
     * 从池中或得连接给到Redis属性
     */
    public function getRedis()
    {
        return $this->redis = $this->connection();
    }
    public function __call($name, $arguments)
    {
        if (!method_exists($this,$name)){
            throw new \RuntimeException("{$name} Method doesn't exist!");
        }
        call_user_func("getRedis");
        call_user_func_array([$this,$name],$arguments);
    }

}
  • RedisOp.php

/**
 * redis 操作
 */
namespace App\redis_pool\redis;

class RedisOp extends RedisBase
{
    public function hGet($key, $field)
    {
        return $this->redis->hGet($key, $field);
    }
    public function hSet($key, $field, $value)
    {
        return $this->redis->hSet($key,$field,$value);
    }
    .......
    //添加更多redis 操作类
}

二、Redis连接池封装类

  • RedisPool.php

namespace App\redis_pool\pool;

use Swoole\Database\RedisPool as Pool;
use Swoole\Database\RedisConfig;
use Illuminate\Foundation\Application;

class RedisPool
{
    /**
     * laravel的应用
     */
    protected $app;

    /*
     *获取Redis的配置
     */
    protected $config;

    /**
     * 池
     */
    protected $pool;
    public function  __construct(Application $app)
    {
        $this->app = $app;
        $this->config = $app->make("config");
        $this->init();
    }
    public function init()
    {
        $config = (new RedisConfig())
            ->withHost($this->config->get('database.redis.default.host'))
            ->withPort($this->config->get('database.redis.default.port'))
            ->withAuth($this->config->get('database.redis.default.password'));
        $this->pool = new Pool($config,$this->config->get('database.redis.default.pool.size'));
    }
    public function get()
    {
        return $this->pool->get();
    }
    public function put($redis)
    {
        return $this->pool->put($redis);
    }
}

三、在laravel容器中注册redis单例

  • /app/Providers/AppServiceProvider.php

namespace App\Providers;
use App\pool\redis_pool\RedisOp;
use App\pool\redis_pool\RedisPool;
use Illuminate\Support\ServiceProvider;
use Elasticsearch\ClientBuilder as ESClientBuilder;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        //在laravel的容器中注册一个redis 的服务
        $this->app->singleton('redis',function (){
            return new RedisOp(new RedisPool($this->app));
        });
    }
}

四、一键协程化

  • Redis.php

namespace App\redis_pool;
use Swoole\Runtime;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;

class Redis
{
	//获取redis已注册的服务
    public static function getDriver()
    {
        return app(app('config')->get("database.redis.default.driver"));
    }
    /**
     * @param $name redis操作方法
     * @param $arguments 参数
     */
    public static function __callStatic($name, $arguments)
    {
        Runtime::enableCoroutine();//woole在最新版本中4.1.0新增了Runtime::enableCoroutine特性,可以将基于php_stream的代码和客户端一键协程化
        $chan = new Channel(1);//设置一个容量为1的通道
        Coroutine::create(function () use ($chan,$name,$arguments){ //创建一个新的协程
            $redis = self::getDriver();
            $return = $redis->{$name}(...$arguments);//执行redis相关 操作
            $chan->push($return);
            return $chan->pop();
        });
    }
}

五、调用示例


namespace App\Http\Controllers\Api;
use App\redis_pool\Redis;

class TestController extends Controller
{
    public function index()
    {
    	//使用redis的set方法
     	$re1 = Redis::set("test",1234567);
     	$res = Redis::get("test");
     	echo $res; //输出 1234567
	}

你可能感兴趣的:(Swoole,Redis,Laravel,redis,swoole)