Files
kiri-core/System/Cache/Redis.php
T

161 lines
3.0 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/27 0027
* Time: 11:00
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace Snowflake\Cache;
use Exception;
use Snowflake\Abstracts\Component;
2020-09-04 01:05:33 +08:00
use Snowflake\Abstracts\Config;
2021-06-29 12:01:06 +08:00
use Snowflake\Core\Json;
2020-08-31 01:27:08 +08:00
use Snowflake\Event;
2020-08-31 13:49:40 +08:00
use Snowflake\Exception\ConfigException;
2020-08-31 01:27:08 +08:00
use Snowflake\Snowflake;
/**
* Class Redis
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Cache
2020-08-31 01:27:08 +08:00
* @see \Redis
2021-06-29 12:01:06 +08:00
*
2020-08-31 01:27:08 +08:00
*/
class Redis extends Component
{
2021-08-02 16:38:50 +08:00
2020-08-31 01:27:08 +08:00
/**
* @throws Exception
*/
2021-03-29 17:00:53 +08:00
public function init()
2020-08-31 01:27:08 +08:00
{
2021-06-07 11:43:01 +08:00
Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'destroy']);
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'release']);
Event::on(Event::SERVER_WORKER_START, [$this, 'createPool']);
Event::on(Event::SERVER_TASK_START, [$this, 'createPool']);
2020-08-31 01:27:08 +08:00
}
2021-01-04 16:03:10 +08:00
/**
2021-03-08 18:52:25 +08:00
* @throws ConfigException
2021-04-01 18:35:16 +08:00
* @throws Exception
2021-01-04 16:03:10 +08:00
*/
2021-06-07 11:43:01 +08:00
public function createPool()
2021-01-04 16:03:10 +08:00
{
2021-03-08 18:52:25 +08:00
$connections = Snowflake::app()->getRedisFromPool();
2021-01-04 16:03:10 +08:00
2021-01-04 16:21:31 +08:00
$config = $this->get_config();
2021-01-04 16:03:10 +08:00
2021-08-02 16:38:50 +08:00
$length = Config::get('connections.pool.max',10);
2021-01-04 16:11:43 +08:00
2021-07-05 15:49:37 +08:00
$connections->initConnections('Redis:' . $config['host'], true, $length);
2021-01-04 16:03:10 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @param $name
* @param $arguments
* @return mixed
* @throws
*/
2020-12-17 14:09:14 +08:00
public function __call($name, $arguments): mixed
2020-08-31 01:27:08 +08:00
{
2021-06-29 12:01:06 +08:00
$time = microtime(true);
2021-06-07 11:43:01 +08:00
if (method_exists($this, $name)) {
$data = $this->{$name}(...$arguments);
2020-09-03 18:10:28 +08:00
} else {
2021-06-04 18:33:42 +08:00
$data = $this->proxy()->{$name}(...$arguments);
2020-08-31 01:27:08 +08:00
}
2021-06-29 12:01:06 +08:00
if (microtime(true) - $time >= 0.02) {
2021-06-29 14:40:12 +08:00
$this->warning('Redis:' . Json::encode([$name, $arguments]) . (microtime(true) - $time));
2021-06-29 12:01:06 +08:00
}
2020-09-03 18:10:28 +08:00
return $data;
2020-08-31 01:27:08 +08:00
}
2020-09-08 15:19:17 +08:00
/**
* @param $key
* @param int $timeout
2021-04-01 18:35:16 +08:00
* @return bool|int
2020-09-08 15:19:17 +08:00
* @throws Exception
*/
2021-06-29 12:01:06 +08:00
public function lock($key, int $timeout = 5): bool|int
2020-09-08 15:19:17 +08:00
{
2020-09-08 17:58:54 +08:00
$script = <<<SCRIPT
local _nx = redis.call('setnx',KEYS[1], ARGV[1])
if (_nx ~= 0) then
redis.call('expire',KEYS[1], ARGV[1])
return 1
end
return 0
SCRIPT;
2021-05-28 18:23:58 +08:00
return $this->proxy()->eval($script, ['{lock}:' . $key, $timeout], 1);
2020-09-08 15:19:17 +08:00
}
/**
* @param $key
* @return int
* @throws Exception
*/
2021-06-07 11:43:01 +08:00
public function unlock($key): int
2020-09-08 15:19:17 +08:00
{
$redis = $this->proxy();
2021-05-28 18:23:58 +08:00
return $redis->del('{lock}:' . $key);
2020-09-08 15:19:17 +08:00
}
2020-08-31 01:27:08 +08:00
/**
2021-03-08 18:52:25 +08:00
* @throws ConfigException
2021-04-01 18:35:16 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2021-06-07 11:43:01 +08:00
public function release()
2020-08-31 01:27:08 +08:00
{
2021-03-08 18:52:25 +08:00
$connections = Snowflake::app()->getRedisFromPool();
2020-08-31 01:27:08 +08:00
$connections->release($this->get_config(), true);
}
/**
* 销毁连接池
2020-09-03 18:04:03 +08:00
* @throws ConfigException
2021-02-20 13:20:37 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2021-06-07 11:43:01 +08:00
public function destroy()
2020-08-31 01:27:08 +08:00
{
2021-03-08 18:52:25 +08:00
$connections = Snowflake::app()->getRedisFromPool();
2020-08-31 01:27:08 +08:00
$connections->destroy($this->get_config(), true);
}
/**
* @return \Redis
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function proxy(): \Redis
2020-08-31 01:27:08 +08:00
{
2021-03-08 18:52:25 +08:00
$connections = Snowflake::app()->getRedisFromPool();
2020-08-31 01:27:08 +08:00
2021-06-04 18:11:58 +08:00
$config = $this->get_config();
$client = $connections->get($config, true);
2020-08-31 01:27:08 +08:00
if (!($client instanceof \Redis)) {
throw new Exception('Redis connections more.');
}
return $client;
}
/**
* @return array
2020-08-31 13:49:40 +08:00
* @throws ConfigException
2020-08-31 01:27:08 +08:00
*/
public function get_config(): array
{
2021-06-04 18:11:58 +08:00
return Config::get('cache.redis', null, true);
2020-08-31 01:27:08 +08:00
}
}