perf: 优化初始化数据

This commit is contained in:
kuaifan
2024-11-11 22:44:18 +08:00
parent 6fda0bd548
commit 0c70613865
13 changed files with 487 additions and 222 deletions

View File

@@ -0,0 +1,86 @@
<?php
namespace App\Module\Table;
use ReflectionClass;
use Swoole\Table;
abstract class AbstractData
{
/** @var self */
protected static $instance = null;
/** @var Table */
protected $table;
protected function getTableName(): string
{
$className = (new ReflectionClass(static::class))->getShortName();
return lcfirst($className) . 'Table';
}
private function __clone() {}
private function __wakeup() {}
protected function __construct()
{
$this->table = app('swoole')->{$this->getTableName()};
}
public function getTable()
{
return $this->table;
}
public static function instance()
{
if (static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
}
public static function set($key, $value)
{
return self::instance()->table->set($key, ['value' => $value]);
}
public static function get($key, $default = null)
{
$data = self::instance()->table->get($key);
return $data ? $data['value'] : $default;
}
public static function del($key)
{
return self::instance()->table->del($key);
}
public static function exist($key)
{
return self::instance()->table->exist($key);
}
public static function setMultiple(array $items)
{
foreach ($items as $key => $value) {
self::set($key, $value);
}
}
public static function clear()
{
foreach (self::instance()->table as $key => $row) {
self::del($key);
}
}
public static function getAll()
{
$result = [];
foreach (self::instance()->table as $key => $row) {
$result[$key] = $row['value'];
}
return $result;
}
}