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

162 lines
3.1 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;
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
*/
class Redis extends Component
{
2020-10-29 18:17:25 +08:00
public string $host = '127.0.0.1';
public string $auth = 'xl.2005113426';
public int $port = 6973;
public int $databases = 0;
public int $timeout = -1;
public string $prefix = 'idd';
2020-08-31 01:27:08 +08:00
/**
* @throws Exception
*/
public function init()
{
2020-09-03 11:39:20 +08:00
$event = Snowflake::app()->event;
2020-08-31 01:27:08 +08:00
$event->on(Event::RELEASE_ALL, [$this, 'destroy']);
$event->on(Event::EVENT_AFTER_REQUEST, [$this, 'release']);
2021-01-04 16:03:10 +08:00
$event->on(Event::SERVER_WORKER_START, [$this, 'createPool']);
2020-08-31 01:27:08 +08:00
}
2021-01-04 16:03:10 +08:00
/**
* @throws ConfigException
*/
public function createPool()
{
$connections = Snowflake::app()->pool->redis;
$config = $this->get_config();
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$connections->initConnections('redis:' . $name, true);
$connections->setLength(env('REDIS.POOL_LENGTH', 100));
}
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
{
2020-09-03 18:10:28 +08:00
if (method_exists($this, $name)) {
$data = $this->{$name}(...$arguments);
} else {
$data = $this->proxy()->{$name}(...$arguments);
2020-08-31 01:27:08 +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
* @return bool
* @throws Exception
*/
public function lock($key, $timeout = 5)
{
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;
return $this->proxy()->eval($script, [$key, $timeout], 1);
2020-09-08 15:19:17 +08:00
}
/**
* @param $key
* @return int
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function unlock($key): int
2020-09-08 15:19:17 +08:00
{
$redis = $this->proxy();
return $redis->del($key);
}
2020-08-31 01:27:08 +08:00
/**
* 释放连接池
2020-09-03 18:04:03 +08:00
* @throws ConfigException
2020-08-31 01:27:08 +08:00
*/
public function release()
{
2020-09-03 11:39:20 +08:00
$connections = Snowflake::app()->pool->redis;
2020-08-31 01:27:08 +08:00
$connections->release($this->get_config(), true);
}
/**
* 销毁连接池
2020-09-03 18:04:03 +08:00
* @throws ConfigException
2020-08-31 01:27:08 +08:00
*/
public function destroy()
{
2020-09-03 11:39:20 +08:00
$connections = Snowflake::app()->pool->redis;
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
{
2020-09-03 11:39:20 +08:00
$connections = Snowflake::app()->pool->redis;
2020-08-31 01:27:08 +08:00
2021-01-04 16:03:10 +08:00
$client = $connections->getConnection($this->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
{
2020-09-17 13:56:57 +08:00
return Config::get('cache.redis', false, [
2020-08-31 13:49:40 +08:00
'host' => '127.0.0.1',
'port' => '6379',
'prefix' => Config::get('id'),
'auth' => '',
'databases' => '0',
'read_timeout' => -1,
2020-09-17 13:56:57 +08:00
'timeout' => -1,
2020-08-31 13:49:40 +08:00
]);
2020-08-31 01:27:08 +08:00
}
}