update to 2.3.1 version (build 388)

cleanup code and fix a bug
This commit is contained in:
2021-03-18 14:56:35 +08:00
parent 456b102c15
commit e77b9d4970
57 changed files with 179 additions and 508 deletions

View File

@@ -27,6 +27,7 @@ class LightCache
* @param $config
* @return bool|mixed
* @throws Exception
* @noinspection PhpMissingReturnTypeInspection
*/
public static function init($config) {
self::$config = $config;
@@ -87,6 +88,7 @@ class LightCache
* @param int $expire
* @return mixed
* @throws ZMException
* @noinspection PhpMissingReturnTypeInspection
*/
public static function set(string $key, $value, int $expire = -1) {
if (self::$kv_table === null) throw new ZMException("not initialized LightCache");
@@ -120,6 +122,7 @@ class LightCache
* @param $value
* @return bool|mixed
* @throws ZMException
* @noinspection PhpMissingReturnTypeInspection
*/
public static function update(string $key, $value) {
if (self::$kv_table === null) throw new ZMException("not initialized LightCache.");
@@ -154,7 +157,7 @@ class LightCache
* @return bool
* @throws Exception
*/
public static function isset(string $key) {
public static function isset(string $key): bool {
return self::get($key) !== null;
}
@@ -162,7 +165,7 @@ class LightCache
return self::$kv_table->del($key);
}
public static function getAll() {
public static function getAll(): array {
$r = [];
$del = [];
foreach (self::$kv_table as $k => $v) {
@@ -178,8 +181,11 @@ class LightCache
return $r;
}
/**
* @param false $only_worker
* @throws Exception
*/
public static function savePersistence($only_worker = false) {
// 下面将OnSave激活一下
if (server()->worker_id == (ZMConfig::get("global", "worker_cache")["worker"] ?? 0)) {
$dispatcher = new EventDispatcher(OnSave::class);

View File

@@ -13,12 +13,14 @@ class LightCacheInside
/** @var Table[]|null */
private static $kv_table = [];
public static $last_error = '';
public static function init() {
self::createTable("wait_api", 3, 65536); //用于存协程等待的状态内容
self::createTable("connect", 3, 64); //用于存单机器人模式下的机器人fd的
self::createTable("static_route", 64, 256);//用于存储
public static function init(): bool {
try {
self::createTable("wait_api", 3, 65536);
self::createTable("connect", 3, 64); //用于存单机器人模式下的机器人fd
self::createTable("static_route", 64, 256);//用于存储
} catch (ZMException $e) {
return false;
} //用于存协程等待的状态内容的
//self::createTable("worker_start", 2, 1024);//用于存启动服务器时的状态的
return true;
}
@@ -27,10 +29,8 @@ class LightCacheInside
* @param string $table
* @param string $key
* @return mixed|null
* @throws ZMException
*/
public static function get(string $table, string $key) {
if (!isset(self::$kv_table[$table])) throw new ZMException("not initialized LightCache");
$r = self::$kv_table[$table]->get($key);
return $r === false ? null : json_decode($r["value"], true);
}
@@ -40,10 +40,8 @@ class LightCacheInside
* @param string $key
* @param string|array|int $value
* @return mixed
* @throws ZMException
*/
public static function set(string $table, string $key, $value) {
if (self::$kv_table === null) throw new ZMException("not initialized LightCache");
public static function set(string $table, string $key, $value): bool {
try {
return self::$kv_table[$table]->set($key, [
"value" => json_encode($value, 256)

View File

@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection PhpUnused */
namespace ZM\Store\Lock;
@@ -29,7 +29,7 @@ class SpinLock
}
}
public static function tryLock(string $key) {
public static function tryLock(string $key): bool {
if (($r = self::$kv_lock->incr($key, 'lock_num')) > 1) {
return false;
}

View File

@@ -1,4 +1,6 @@
<?php /** @noinspection PhpComposerExtensionStubsInspection */
<?php /** @noinspection PhpUnused */
/** @noinspection PhpComposerExtensionStubsInspection */
namespace ZM\Store\Redis;
@@ -36,7 +38,7 @@ class ZMRedis
/**
* @return Redis
*/
public function get() {
public function get(): Redis {
return $this->conn;
}

View File

@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection PhpMissingReturnTypeInspection */
namespace ZM\Store;

View File

@@ -16,7 +16,7 @@ class ZMAtomic
* @param $name
* @return Atomic|null
*/
public static function get($name) {
public static function get($name): ?Atomic {
return self::$atomics[$name] ?? null;
}