refac: KVStorage의 내부구조를 Ds\Map으로 변경

This commit is contained in:
2022-05-08 20:10:04 +09:00
parent 703bf9395d
commit bf2268ee4a
7 changed files with 166 additions and 145 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ function processSpring() {
function processGoldIncome() {
$db = DB::db();
$gameStor = new KVStorage($db, 'game_env');
$gameStor = KVStorage::getStorage($db, 'game_env');
[$year, $month] = $gameStor->getValuesAsArray(['year', 'month']);
$adminLog = [];
@@ -52,7 +52,7 @@ class ResetTurnTime extends \sammo\BaseAPI
return '충분한 유산 포인트를 가지고 있지 않습니다.';
}
$gameStor = new KVStorage($db, 'game_env');
$gameStor = KVStorage::getStorage($db, 'game_env');
[$turnTerm, $serverTurnTime] = $gameStor->getValuesAsArray(['turnterm', 'turntime']);
$currTurnTime = new DateTimeImmutable($general->getTurnTime());
+1 -1
View File
@@ -47,7 +47,7 @@ class SetBlockScout extends \sammo\BaseAPI
return "권한이 부족합니다.";
}
$gameStor = new KVStorage($db, 'game_env');
$gameStor = KVStorage::getStorage($db, 'game_env');
$blockChangeScout = $gameStor->getValue('block_change_scout')??false;
if ($blockChangeScout){
return "임관 설정을 바꿀 수 없도록 설정되어 있습니다.";
+1 -1
View File
@@ -15,7 +15,7 @@ class BlockScoutAction extends \sammo\Event\Action{
'scout'=>1
], true);
if($this->blockChangeScout !== null){
$gameStor = new KVStorage($db, 'game_env');
$gameStor = KVStorage::getStorage($db, 'game_env');
$gameStor->setValue('block_change_scout', $this->blockChangeScout);
}
@@ -16,7 +16,7 @@ class UnblockScoutAction extends \sammo\Event\Action{
]);
if($this->blockChangeScout !== null){
$gameStor = new KVStorage($db, 'game_env');
$gameStor = KVStorage::getStorage($db, 'game_env');
$gameStor->setValue('block_change_scout', $this->blockChangeScout);
}
+98 -102
View File
@@ -1,140 +1,133 @@
<?php
namespace sammo;
use MeekroDBException;
use Ds\Map;
class KVStorage{
class KVStorage
{
private $db;
private $tableName;
private $storNamespace;
private $cacheData = null;
static private $storageList = [];
private ?Map $cacheData = null;
static private function convBackedEnum(string|int|\BackedEnum $key): string|int{
if($key instanceof \BackedEnum){
return $key->value;
}
return $key;
}
/** @var null|Map<string, self> */
static private ?Map $storageList = null;
/**
* @param (string|int|\BackedEnum)[] $keys
* @return (string|int)[]
*/
static private function convBackedEnums(array $keys): array{
$convKeys = [];
foreach($keys as $key){
if($key instanceof \BackedEnum){
$convKeys[] = $key->value;
static public function getStorage(\MeekroDB $db, $storNamespace, string $tableName = 'storage'): self
{
if (self::$storageList === null) {
self::$storageList = new Map();
}
else{
$convKeys[] = $key;
}
}
return $convKeys;
}
static public function getStorage(\MeekroDB $db, $storNamespace, string $tableName='storage'):self{
$obj_id = spl_object_hash($db);
$fullKey = $obj_id . ',' . $storNamespace . ',' . $tableName;
if(key_exists($fullKey, static::$storageList)){
return static::$storageList[$fullKey];
if (self::$storageList->hasKey($fullKey)) {
return self::$storageList[$fullKey];
}
$obj = new static($db, $storNamespace, $tableName);
static::$storageList[$fullKey] = $obj;
self::$storageList[$fullKey] = $obj;
return $obj;
}
public function __construct(\MeekroDB $db, $storNamespace, string $tableName='storage'){
protected function __construct(\MeekroDB $db, $storNamespace, string $tableName = 'storage')
{
$this->db = $db;
$this->storNamespace = $storNamespace;
$this->tableName = $tableName;
$this->turnOnCache();
}
public static function getValuesFromInterNamespace(\MeekroDB $db, string $tableName, string|int|\BackedEnum $key):array{
$result = [];
$key = self::convBackedEnum($key);
foreach($db->queryAllLists(
'SELECT `namespace`, `value` FROM %b WHERE `key`=%s', $tableName, $key
) as [$namespaceName, $value])
public static function getValuesFromInterNamespace(\MeekroDB $db, string $tableName, string|int|\BackedEnum $key): array
{
$result = [];
$key = Util::valueFromEnum($key);
foreach ($db->queryAllLists(
'SELECT `namespace`, `value` FROM %b WHERE `key`=%s',
$tableName,
$key
) as [$namespaceName, $value]) {
$result[$namespaceName] = Json::decode($value);
}
return $result;
}
public function __get(string|int|\BackedEnum $key) {
public function __get(string|int|\BackedEnum $key)
{
return $this->getValue($key);
}
public function __set(string|int|\BackedEnum $key, $value) {
public function __set(string|int|\BackedEnum $key, $value)
{
$this->setValue($key, $value);
}
public function __unset(string|int|\BackedEnum $key){
public function __unset(string|int|\BackedEnum $key)
{
$this->deleteValue($key);
}
public function turnOnCache(): self{
public function turnOnCache(): self
{
if ($this->cacheData === null) {
$this->cacheData = [];
$this->cacheData = new Map();
}
return $this;
}
public function turnOffCache(): self{
public function turnOffCache(): self
{
if ($this->cacheData !== null) {
$this->cacheData = null;
}
return $this;
}
public function resetCache(bool $disableCache=true):self{
public function resetCache(bool $disableCache = true): self
{
if ($disableCache) {
$this->cacheData = null;
}
else{
} else {
$this->cacheData = [];
}
return $this;
}
public function resetValues():self{
public function resetValues(): self
{
if ($this->cacheData !== null) {
$this->cacheData = [];
}
return $this->resetDBNamespace();
}
public function invalidateCacheValue(string|int|\BackedEnum $key):self{
public function invalidateCacheValue(string|int|\BackedEnum $key): self
{
if ($this->cacheData === null) {
return $this;
}
$key = static::convBackedEnum($key);
if(key_exists($key, $this->cacheData)){
unset($this->cacheData[$key]);
}
$key = Util::valueFromEnum($key);
$this->cacheData->remove($key, null);
return $this;
}
public function invalidateCacheValues(array $keys):self{
public function invalidateCacheValues(array $keys): self
{
if ($this->cacheData === null) {
return $this;
}
$keys = static::convBackedEnums($keys);
$keys = Util::valuesFromEnumArray($keys);
foreach ($keys as $key) {
if(key_exists($key, $this->cacheData)){
unset($this->cacheData[$key]);
}
$this->cacheData->remove($key, null);
}
return $this;
}
public function cacheAll(bool $invalidateAll=true):self{
public function cacheAll(bool $invalidateAll = true): self
{
if (!$invalidateAll && $this->cacheData !== null && count($this->cacheData) > 0) {
return $this;
}
@@ -142,19 +135,19 @@ class KVStorage{
return $this;
}
public function cacheValues(array $keys, bool $invalidateAll=false):self{
public function cacheValues(array $keys, bool $invalidateAll = false): self
{
if ($this->cacheData === null) {
$this->cacheData = [];
$this->cacheData = new Map();
}
$keys = self::convBackedEnums($keys);
$keys = Util::valuesFromEnumArray($keys);
if ($invalidateAll) {
$notExists = $keys;
}
else{
} else {
$notExists = [];
foreach ($keys as $key) {
if(!key_exists($key, $this->cacheData)){
if (!$this->cacheData->hasKey($key)) {
$notExists[] = $key;
}
}
@@ -165,33 +158,30 @@ class KVStorage{
$values = $this->getDBValues($notExists);
foreach ($notExists as $key) {
if(key_exists($key, $values)){
$this->cacheData[$key] = $values[$key];
}
else{
$this->cacheData[$key] = null;
}
$this->cacheData[$key] = $values->get($key, null);
}
return $this;
}
public function getAll(bool $onlyCache=false): array{
public function getAll(bool $onlyCache = false): array
{
if ($onlyCache && $this->cacheData !== null && count($this->cacheData) > 0) {
return $this->cacheData;
return $this->cacheData->toArray();
}
$result = $this->getDBAll();
if ($this->cacheData !== null) {
$this->cacheData = $result;
}
return $result;
return $result->toArray();
}
public function getValuesAsArray(array $keys, bool $onlyCache=false): array{
public function getValuesAsArray(array $keys, bool $onlyCache = false): array
{
if (!$keys) {
return [];
}
$keys = static::convBackedEnums($keys);
$keys = Util::valuesFromEnumArray($keys);
$dictResult = $this->getValues($keys, $onlyCache);
$result = [];
@@ -201,11 +191,12 @@ class KVStorage{
return $result;
}
public function getValues(array $keys, bool $onlyCache=false): array{
public function getValues(array $keys, bool $onlyCache = false): array
{
if (!$keys) {
return [];
}
$keys = static::convBackedEnums($keys);
$keys = Util::valuesFromEnumArray($keys);
if ($this->cacheData === null) {
return $this->getDBValues($keys);
@@ -217,7 +208,7 @@ class KVStorage{
//TODO: DB Select에서 as를 쓸 수 있으면 좋을 듯.
foreach ($keys as $key) {
if(!key_exists($key, $this->cacheData)){
if (!$this->cacheData->hasKey($key)) {
$notExists[] = $key;
continue;
}
@@ -241,10 +232,11 @@ class KVStorage{
return $result;
}
public function getValue(string|int|\BackedEnum $key, bool $onlyCache=false){
$key = static::convBackedEnum($key);
if($this->cacheData !== null && ($onlyCache || key_exists($key, $this->cacheData))){
return $this->cacheData[$key] ?? null;
public function getValue(string|int|\BackedEnum $key, bool $onlyCache = false)
{
$key = Util::valueFromEnum($key);
if ($this->cacheData !== null && ($onlyCache || $this->cacheData->hasKey($key))) {
return $this->cacheData->get($key, null);
}
$value = $this->getDBValue($key);
@@ -259,8 +251,9 @@ class KVStorage{
* @return KVStorage
* @throws MeekroDBException
*/
public function setValue(string|int|\BackedEnum $key, $value):self{
$key = static::convBackedEnum($key);
public function setValue(string|int|\BackedEnum $key, $value): self
{
$key = Util::valueFromEnum($key);
if ($value === null) {
return $this->deleteValue($key);
@@ -279,51 +272,51 @@ class KVStorage{
* @return KVStorage
* @throws MeekroDBException
*/
public function deleteValue(string|int|\BackedEnum $key):self{
$key = static::convBackedEnum($key);
public function deleteValue(string|int|\BackedEnum $key): self
{
$key = Util::valueFromEnum($key);
if(isset($this->cacheData[$key])){
unset($this->cacheData[$key]);
}
$this->cacheData->remove($key, null);
return $this->deleteDBValue($key);
}
private function getDBAll(): array{
$result = [];
private function getDBAll(): Map
{
$result = new Map();
foreach ($this->db->queryAllLists(
'SELECT `key`, `value` FROM %b WHERE `namespace`=%s',
$this->tableName,
$this->storNamespace
) as list($key, $value))
{
) as list($key, $value)) {
$result[$key] = Json::decode($value);
}
return $result;
}
private function getDBValues(array $keys): array{
private function getDBValues(array $keys): Map
{
if (!$keys) {
return [];
return new Map();
}
$result = [];
$result = new Map();
foreach ($this->db->queryAllLists(
'SELECT `key`, `value` FROM %b WHERE `namespace`=%s AND `key` IN %ls',
$this->tableName,
$this->storNamespace,
$keys
) as list($key, $value))
{
) as list($key, $value)) {
$result[$key] = Json::decode($value);
}
foreach ($keys as $key) {
if(!key_exists($key, $result)){
if (!$result->hasKey($key)) {
$result[$key] = null;
}
}
return $result;
}
private function getDBValue(string $key){
private function getDBValue(string $key)
{
$value = $this->db->queryFirstField(
'SELECT `value` FROM %b WHERE `namespace`=%s AND `key`=%s',
$this->tableName,
@@ -336,7 +329,8 @@ class KVStorage{
return Json::decode($value);
}
private function setDBValue(string $key, $value):self{
private function setDBValue(string $key, $value): self
{
if ($value === null) {
return $this->deleteDBValue($key);
}
@@ -348,7 +342,8 @@ class KVStorage{
return $this;
}
private function deleteDBValue(string $key):self{
private function deleteDBValue(string $key): self
{
$this->db->delete(
$this->tableName,
'`namespace`=%s AND `key`=%s',
@@ -358,7 +353,8 @@ class KVStorage{
return $this;
}
private function resetDBNamespace():self{
private function resetDBNamespace(): self
{
$this->db->delete($this->tableName, 'namespace=%s', $this->storNamespace);
return $this;
}
+25
View File
@@ -809,4 +809,29 @@ class Util extends \utilphp\util
}
}
}
public static function valueFromEnum(\BackedEnum|string|int $value):string|int{
if($value instanceof \BackedEnum){
return $value->value;
}
return $value;
}
/**
* @param (int|string|\BackedEnum)[] $values
* @return (int|string)[]
*/
public static function valuesFromEnumArray(array $values):array{
$result = [];
foreach($values as $value){
if($value instanceof \BackedEnum){
$result[] = $value->value;
}
else{
$result[] = $value;
}
}
return $result;
}
};