mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 15:45:36 +08:00
initial 2.0.0-a3 commit
This commit is contained in:
@@ -6,6 +6,7 @@ namespace ZM\Store;
|
||||
|
||||
use Exception;
|
||||
use Swoole\Table;
|
||||
use ZM\Console\Console;
|
||||
|
||||
class LightCache
|
||||
{
|
||||
@@ -14,13 +15,31 @@ class LightCache
|
||||
|
||||
private static $config = [];
|
||||
|
||||
public static $last_error = '';
|
||||
|
||||
public static function init($config) {
|
||||
self::$config = $config;
|
||||
self::$kv_table = new Table($config["size"], $config["hash_conflict_proportion"]);
|
||||
self::$kv_table->column("value", Table::TYPE_STRING, $config["max_strlen"]);
|
||||
self::$kv_table->column("expire", Table::TYPE_INT);
|
||||
self::$kv_table->column("data_type", Table::TYPE_STRING, 12);
|
||||
return self::$kv_table->create();
|
||||
$result = self::$kv_table->create();
|
||||
if ($result === true && isset($config["persistence_path"])) {
|
||||
$r = json_decode(file_get_contents($config["persistence_path"]), true);
|
||||
if ($r === null) $r = [];
|
||||
foreach ($r as $k => $v) {
|
||||
$write = self::set($k, $v);
|
||||
Console::debug("Writing LightCache: " . $k);
|
||||
if ($write === false) {
|
||||
self::$last_error = '可能是由于 Hash 冲突过多导致动态空间无法分配内存';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($result === false) {
|
||||
self::$last_error = '系统内存不足,申请失败';
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,12 +75,14 @@ class LightCache
|
||||
*/
|
||||
public static function set(string $key, $value, int $expire = -1) {
|
||||
if (self::$kv_table === null) throw new Exception("not initialized LightCache");
|
||||
if (is_array($value) || is_int($value)) {
|
||||
if (is_array($value)) {
|
||||
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
|
||||
if (strlen($value) >= self::$config["max_strlen"]) return false;
|
||||
$data_type = "json";
|
||||
} elseif (is_string($value)) {
|
||||
$data_type = "";
|
||||
} elseif (is_int($value)) {
|
||||
$data_type = "int";
|
||||
} else {
|
||||
throw new Exception("Only can set string, array and int");
|
||||
}
|
||||
@@ -97,20 +118,31 @@ class LightCache
|
||||
$r = [];
|
||||
$del = [];
|
||||
foreach (self::$kv_table as $k => $v) {
|
||||
if ($v["expire"] <= time()) {
|
||||
$del[]=$k;
|
||||
if ($v["expire"] <= time() && $v["expire"] >= 0) {
|
||||
$del[] = $k;
|
||||
continue;
|
||||
}
|
||||
$r[$k] = self::parseGet($v);
|
||||
}
|
||||
foreach($del as $v) {
|
||||
foreach ($del as $v) {
|
||||
self::unset($v);
|
||||
}
|
||||
return $r;
|
||||
}
|
||||
|
||||
public static function savePersistence() {
|
||||
$r = [];
|
||||
foreach (self::$kv_table as $k => $v) {
|
||||
if ($v["expire"] === -2) {
|
||||
$r[$k] = self::parseGet($v);
|
||||
}
|
||||
}
|
||||
$r = file_put_contents(self::$config["persistence_path"], json_encode($r, 128 | 256));
|
||||
if($r === false) Console::error("Not saved, please check your \"persistence_path\"!");
|
||||
}
|
||||
|
||||
private static function checkExpire($key) {
|
||||
if (($expire = self::$kv_table->get($key, "expire")) !== -1) {
|
||||
if (($expire = self::$kv_table->get($key, "expire")) >= 0) {
|
||||
if ($expire <= time()) {
|
||||
self::$kv_table->del($key);
|
||||
}
|
||||
@@ -120,6 +152,7 @@ class LightCache
|
||||
private static function parseGet($r) {
|
||||
switch ($r["data_type"]) {
|
||||
case "json":
|
||||
case "int":
|
||||
return json_decode($r["value"], true);
|
||||
case "":
|
||||
default:
|
||||
|
||||
9
src/ZM/Store/Redis/Redis.php
Normal file
9
src/ZM/Store/Redis/Redis.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Store\Redis;
|
||||
|
||||
|
||||
class Redis
|
||||
{
|
||||
}
|
||||
@@ -15,68 +15,20 @@ use ZM\Config\ZMConfig;
|
||||
class ZMBuf
|
||||
{
|
||||
//读写的缓存数据,需要在worker_num = 1下才能正常使用
|
||||
/** @var mixed[] ZMBuf的data */
|
||||
private static $cache = [];
|
||||
//Swoole SQL连接池,多进程下每个进程一个连接池
|
||||
/** @var PDOPool */
|
||||
static $sql_pool = null;//保存sql连接池的类
|
||||
|
||||
/** @var array Http请求uri路径根节点 */
|
||||
public static $req_mapping_node;
|
||||
/** @var array 事件注解的绑定对 */
|
||||
public static $events = [];
|
||||
|
||||
// 下面的有用,上面的没用了
|
||||
/** @var Atomic[] */
|
||||
public static $atomics;
|
||||
public static $instance = [];
|
||||
public static $context_class = [];
|
||||
public static $terminal = null;
|
||||
|
||||
static function get($name, $default = null) {
|
||||
return self::$cache[$name] ?? $default;
|
||||
}
|
||||
|
||||
static function set($name, $value) {
|
||||
self::$cache[$name] = $value;
|
||||
}
|
||||
|
||||
static function append($name, $value) {
|
||||
self::$cache[$name][] = $value;
|
||||
}
|
||||
|
||||
static function appendKey($name, $key, $value) {
|
||||
self::$cache[$name][$key] = $value;
|
||||
}
|
||||
|
||||
static function appendKeyInKey($name, $key, $value) {
|
||||
self::$cache[$name][$key][] = $value;
|
||||
}
|
||||
|
||||
static function unsetCache($name) {
|
||||
unset(self::$cache[$name]);
|
||||
}
|
||||
|
||||
static function unsetByValue($name, $vale) {
|
||||
$key = array_search($vale, self::$cache[$name]);
|
||||
array_splice(self::$cache[$name], $key, 1);
|
||||
}
|
||||
|
||||
static function isset($name) {
|
||||
return isset(self::$cache[$name]);
|
||||
}
|
||||
|
||||
static function array_key_exists($name, $key) {
|
||||
return isset(self::$cache[$name][$key]);
|
||||
}
|
||||
|
||||
static function in_array($name, $val) {
|
||||
return in_array($val, self::$cache[$name]);
|
||||
}
|
||||
|
||||
public static function resetCache() {
|
||||
self::$cache = [];
|
||||
self::$instance = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化atomic计数器
|
||||
*/
|
||||
@@ -86,6 +38,9 @@ class ZMBuf
|
||||
}
|
||||
self::$atomics["stop_signal"] = new Atomic(0);
|
||||
self::$atomics["wait_msg_id"] = new Atomic(0);
|
||||
for($i = 0; $i < 10; ++$i) {
|
||||
self::$atomics["_tmp_".$i] = new Atomic(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user