Files
douba_shop/crmeb/services/RedisService.php
Karlcarlo c9a98f342c chore: 添加静态资源文件和版本配置文件
添加大量图片、字体、音频等静态资源文件,包括用户界面图片、模板文件和安装界面素材
添加系统版本配置文件(.version)和常量定义文件(.constant)
更新供应商依赖文件,包括Symfony VarDumper组件和相关许可证文件
添加验证码配置文件(captcha.php)和.gitignore规则
2026-03-06 20:14:23 +08:00

167 lines
4.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
namespace crmeb\services;
use think\facade\Config;
use Swoole\Coroutine\Channel;
use Swoole\Coroutine\Redis;
/**
* Class RedisService
* @author 等风来
* @email 136327134@qq.com
* @date 2022/10/29
* @package crmeb\services
*/
class RedisService
{
/**
* @var array
*/
protected $config;
/**
* 获取进程池最大时间
* @var int
*/
protected $maxWaitTime = 3;
/**
* 最大连接池数量
* @var int
*/
protected $maxActive;
/**
* @var Channel
*/
protected $pool;
/**
* RedisService constructor.
* @param array $config
* @param array $pool
*/
public function __construct(array $config = [], array $pool = [])
{
$this->config = $config ?: Config::get('cache.stores.redis');
$this->maxActive = $pool['maxActive'] ?? Config::get('swoole.pool.redis.cache.max_active', 50);
$this->maxWaitTime = $pool['maxWaitTime'] ?? Config::get('swoole.pool.redis.cache.max_wait_time', 5);
}
/**
* 初始化连接池
* @author 等风来
* @email 136327134@qq.com
* @date 2022/10/29
*/
public function init()
{
$this->pool = new Channel($this->maxActive);
go(function () {
for ($i = 0; $i < $this->maxActive; $i++) {
$redis = $this->createConnect($this->config);
$this->pool->push($redis);
}
});
return $this;
}
/**
* 创建连接
* @param array $config
* @return mixed
* @author 等风来
* @email 136327134@qq.com
* @date 2022/10/29
*/
public function createConnect(array $config)
{
$connection = new Redis();
$res = $connection->connect($config['host'], $config['port'], \Redis::SERIALIZER_PHP);
if ($config['password']) {
$connection->auth($config['password']);
}
$connection->select($config['select']);
if ($res === false) {
throw new \RuntimeException("Failed to connect redis server");
}
return $connection;
}
/**
* 销毁连接
* @param $connection
* @author 等风来
* @email 136327134@qq.com
* @date 2022/10/29
*/
public function removeConnect($connection)
{
\Swoole\Coroutine\defer(function () use ($connection) { //释放
$connection->close();
});
}
/**
* 获取redis连接句柄
* @return Redis
* @author 等风来
* @email 136327134@qq.com
* @date 2022/10/29
*/
public function getRedis()
{
if ($this->pool->isEmpty()) {
$redis = $this->createConnect($this->config);
$this->removeConnect($redis);
return $redis;
}
$redis = $this->pool->pop($this->maxWaitTime);
if (false === $redis) {
$redis = $this->createConnect($this->config);
$this->removeConnect($redis);
} else {
\Swoole\Coroutine\defer(function () use ($redis) { //释放
$this->pool->push($redis);
});
}
return $redis;
}
/**
* @param $name
* @param $arguments
* @return Redis
* @author 等风来
* @email 136327134@qq.com
* @date 2022/10/29
*/
public function __call($name, $arguments)
{
return call_user_func_array([$this->getRedis(), $name], $arguments);
}
}