mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 15:45:36 +08:00
add LightCache support
This commit is contained in:
@@ -20,6 +20,7 @@ use ZM\Process\ProcessStateManager;
|
||||
use ZM\Store\Database\DBException;
|
||||
use ZM\Store\Database\DBPool;
|
||||
use ZM\Store\FileSystem;
|
||||
use ZM\Store\KV\LightCache;
|
||||
use ZM\Utils\ZMUtil;
|
||||
|
||||
class WorkerEventListener
|
||||
@@ -70,6 +71,11 @@ class WorkerEventListener
|
||||
logger()->info('WORKER#' . $i . ":\t" . ProcessStateManager::getProcessState(ZM_PROCESS_WORKER, $i));
|
||||
}
|
||||
|
||||
// 如果使用的是 LightCache,注册下自动保存的监听器
|
||||
if (is_a(config('global.kv.use', \LightCache::class), LightCache::class, true)) {
|
||||
Framework::getInstance()->getDriver()->getEventLoop()->addTimer(config('global.kv.light_cache_autosave_time', 600) * 1000, [LightCache::class, 'saveAll'], 0);
|
||||
}
|
||||
|
||||
// 注册 Worker 进程遇到退出时的回调,安全退出
|
||||
register_shutdown_function(function () {
|
||||
$error = error_get_last();
|
||||
@@ -104,9 +110,13 @@ class WorkerEventListener
|
||||
|
||||
/**
|
||||
* @throws ZMKnownException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function onWorkerStop999(): void
|
||||
{
|
||||
if (is_a(config('global.kv.use', \LightCache::class), LightCache::class, true)) {
|
||||
LightCache::saveAll();
|
||||
}
|
||||
logger()->debug('Worker #' . ProcessManager::getProcessId() . ' stopping');
|
||||
if (DIRECTORY_SEPARATOR !== '\\') {
|
||||
ProcessStateManager::removeProcessState(ZM_PROCESS_WORKER, ProcessManager::getProcessId());
|
||||
|
||||
47
src/ZM/Store/KV/KVInterface.php
Normal file
47
src/ZM/Store/KV/KVInterface.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Store\KV;
|
||||
|
||||
interface KVInterface
|
||||
{
|
||||
/**
|
||||
* 打开一个 KV 库
|
||||
*
|
||||
* @param string $name KV 的库名称
|
||||
*/
|
||||
public static function open(string $name = ''): KVInterface;
|
||||
|
||||
/**
|
||||
* 返回一个 KV 键值对的数据
|
||||
*
|
||||
* @param string $key 键名
|
||||
* @param null|mixed $default 如果不存在时返回的默认值
|
||||
*/
|
||||
public function get(string $key, mixed $default = null): mixed;
|
||||
|
||||
/**
|
||||
* 设置一个 KV 键值对的数据
|
||||
*
|
||||
* @param string $key 键名
|
||||
* @param mixed $value 键值
|
||||
* @param int $ttl 超时秒数(如果等于 0 代表永不超时)
|
||||
*/
|
||||
public function set(string $key, mixed $value, int $ttl = 0): bool;
|
||||
|
||||
/**
|
||||
* 强制删除一个 KV 键值对数据
|
||||
*
|
||||
* @param string $key 键名
|
||||
* @return bool 当键存在并被删除时返回 true
|
||||
*/
|
||||
public function unset(string $key): bool;
|
||||
|
||||
/**
|
||||
* 键值对数据是否存在
|
||||
*
|
||||
* @param string $key 键名
|
||||
*/
|
||||
public function isset(string $key): bool;
|
||||
}
|
||||
164
src/ZM/Store/KV/LightCache.php
Normal file
164
src/ZM/Store/KV/LightCache.php
Normal file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Store\KV;
|
||||
|
||||
use ZM\Exception\InvalidArgumentException;
|
||||
use ZM\Process\ProcessStateManager;
|
||||
use ZM\Store\FileSystem;
|
||||
|
||||
/**
|
||||
* 轻量、基于本地 JSON 文件的 KV 键值对缓存
|
||||
*/
|
||||
class LightCache implements KVInterface
|
||||
{
|
||||
/** @var array 存放库对象的列表 */
|
||||
private static array $objs = [];
|
||||
|
||||
/** @var array 存放缓存数据的列表 */
|
||||
private static array $caches = [];
|
||||
|
||||
/** @var array 存放超时数据的列表 */
|
||||
private static array $ttys = [];
|
||||
|
||||
/** @var string 查找库的目录地址 */
|
||||
private string $find_dir;
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function __construct(private string $name = '', string $find_str = '')
|
||||
{
|
||||
if ((ProcessStateManager::$process_mode['worker'] ?? 0) > 1) {
|
||||
logger()->error('LightCache 不支持多进程模式,如需在多进程下使用,请使用 ZMRedis 作为 KV 引擎!');
|
||||
return;
|
||||
}
|
||||
$this->find_dir = empty($find_str) ? config('global.kv.light_cache_dir', '/tmp/zm_light_cache') : $find_str;
|
||||
FileSystem::createDir($this->find_dir);
|
||||
$this->validateKey($name);
|
||||
if (file_exists($this->find_dir . '/' . $name . '.json')) {
|
||||
$data = json_decode(file_get_contents($this->find_dir . '/' . $name . '.json'), true);
|
||||
if (is_array($data)) {
|
||||
self::$caches[$name] = $data['data'];
|
||||
self::$ttys[$name] = $data['expire'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function open(string $name = ''): KVInterface
|
||||
{
|
||||
if (!isset(self::$objs[$name])) {
|
||||
self::$objs[$name] = new LightCache($name);
|
||||
}
|
||||
return self::$objs[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public static function saveAll()
|
||||
{
|
||||
/** @var LightCache $obj */
|
||||
foreach (self::$objs as $obj) {
|
||||
$obj->save();
|
||||
}
|
||||
logger()->debug('Saved all light caches');
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存 KV 库的数据到文件
|
||||
*
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function save(): void
|
||||
{
|
||||
file_put_contents(zm_dir($this->find_dir . '/' . $this->name . '.json'), json_encode([
|
||||
'data' => self::$caches[$this->name] ?? [],
|
||||
'expire' => self::$ttys[$this->name] ?? [],
|
||||
], JSON_THROW_ON_ERROR));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除该 KV 库的所有数据,并且永远无法恢复
|
||||
*/
|
||||
public function removeSelf(): bool
|
||||
{
|
||||
if (file_exists($this->find_dir . '/' . $this->name . '.json')) {
|
||||
unlink($this->find_dir . '/' . $this->name . '.json');
|
||||
}
|
||||
unset(self::$caches[$this->name], self::$ttys[$this->name], self::$objs[$this->name]);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function get(string $key, mixed $default = null): mixed
|
||||
{
|
||||
// 首先判断在不在缓存变量里
|
||||
if (!isset(self::$caches[$this->name][$key])) {
|
||||
return $default;
|
||||
}
|
||||
// 然后判断是否有延迟
|
||||
if (isset(self::$ttys[$this->name][$key])) {
|
||||
if (self::$ttys[$this->name][$key] > time()) {
|
||||
return self::$caches[$this->name][$key];
|
||||
}
|
||||
unset(self::$ttys[$this->name][$key], self::$caches[$this->name][$key]);
|
||||
|
||||
return $default;
|
||||
}
|
||||
return self::$caches[$this->name][$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function set(string $key, mixed $value, int $ttl = 0): bool
|
||||
{
|
||||
$this->validateKey($key);
|
||||
self::$caches[$this->name][$key] = $value;
|
||||
if ($ttl > 0) {
|
||||
self::$ttys[$this->name][$key] = time() + $ttl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function unset(string $key): bool
|
||||
{
|
||||
unset(self::$caches[$this->name][$key], self::$ttys[$this->name][$key]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isset(string $key): bool
|
||||
{
|
||||
if (!isset(self::$caches[$this->name][$key])) {
|
||||
return false;
|
||||
}
|
||||
if (isset(self::$ttys[$this->name][$key])) {
|
||||
if (self::$ttys[$this->name][$key] > time()) {
|
||||
return true;
|
||||
}
|
||||
unset(self::$ttys[$this->name][$key], self::$caches[$this->name][$key]);
|
||||
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private function validateKey(string $key): void
|
||||
{
|
||||
if ($key === '') {
|
||||
return;
|
||||
}
|
||||
if (strlen($key) >= 128) {
|
||||
throw new InvalidArgumentException('LightCache 键名长度不能超过 128 字节!');
|
||||
}
|
||||
// 只能包含数字、大小写字母、下划线、短横线、点、中文
|
||||
if (!preg_match('/^[\w\-.\x{4e00}-\x{9fa5}]+$/u', $key)) {
|
||||
throw new InvalidArgumentException('LightCache 键名只能包含数字、大小写字母、下划线、短横线、点、中文!');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user