feat: KVStorage에서 BackedEnum 지원
This commit is contained in:
+46
-15
@@ -11,6 +11,30 @@ class KVStorage{
|
|||||||
|
|
||||||
static private $storageList = [];
|
static private $storageList = [];
|
||||||
|
|
||||||
|
static private function convBackedEnum(string|int|\BackedEnum $key): string|int{
|
||||||
|
if($key instanceof \BackedEnum){
|
||||||
|
return $key->value;
|
||||||
|
}
|
||||||
|
return $key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
$convKeys[] = $key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $convKeys;
|
||||||
|
}
|
||||||
|
|
||||||
static public function getStorage(\MeekroDB $db, $storNamespace, string $tableName='storage'):self{
|
static public function getStorage(\MeekroDB $db, $storNamespace, string $tableName='storage'):self{
|
||||||
$obj_id = spl_object_hash($db);
|
$obj_id = spl_object_hash($db);
|
||||||
$fullKey = $obj_id.','.$storNamespace.','.$tableName;
|
$fullKey = $obj_id.','.$storNamespace.','.$tableName;
|
||||||
@@ -29,8 +53,9 @@ class KVStorage{
|
|||||||
$this->turnOnCache();
|
$this->turnOnCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getValuesFromInterNamespace(\MeekroDB $db, string $tableName, $key):array{
|
public static function getValuesFromInterNamespace(\MeekroDB $db, string $tableName, string|int|\BackedEnum $key):array{
|
||||||
$result = [];
|
$result = [];
|
||||||
|
$key = self::convBackedEnum($key);
|
||||||
foreach($db->queryAllLists(
|
foreach($db->queryAllLists(
|
||||||
'SELECT `namespace`, `value` FROM %b WHERE `key`=%s', $tableName, $key
|
'SELECT `namespace`, `value` FROM %b WHERE `key`=%s', $tableName, $key
|
||||||
) as [$namespaceName, $value])
|
) as [$namespaceName, $value])
|
||||||
@@ -40,15 +65,15 @@ class KVStorage{
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __get($key) {
|
public function __get(string|int|\BackedEnum $key) {
|
||||||
return $this->getValue($key);
|
return $this->getValue($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __set($key, $value) {
|
public function __set(string|int|\BackedEnum $key, $value) {
|
||||||
$this->setValue($key, $value);
|
$this->setValue($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __unset($key){
|
public function __unset(string|int|\BackedEnum $key){
|
||||||
$this->deleteValue($key);
|
$this->deleteValue($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,10 +108,11 @@ class KVStorage{
|
|||||||
return $this->resetDBNamespace();
|
return $this->resetDBNamespace();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function invalidateCacheValue($key):self{
|
public function invalidateCacheValue(string|int|\BackedEnum $key):self{
|
||||||
if($this->cacheData === null){
|
if($this->cacheData === null){
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
$key = static::convBackedEnum($key);
|
||||||
if(key_exists($key, $this->cacheData)){
|
if(key_exists($key, $this->cacheData)){
|
||||||
unset($this->cacheData[$key]);
|
unset($this->cacheData[$key]);
|
||||||
}
|
}
|
||||||
@@ -97,6 +123,7 @@ class KVStorage{
|
|||||||
if($this->cacheData === null){
|
if($this->cacheData === null){
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
$keys = static::convBackedEnums($keys);
|
||||||
|
|
||||||
foreach($keys as $key){
|
foreach($keys as $key){
|
||||||
if(key_exists($key, $this->cacheData)){
|
if(key_exists($key, $this->cacheData)){
|
||||||
@@ -119,6 +146,7 @@ class KVStorage{
|
|||||||
if($this->cacheData === null){
|
if($this->cacheData === null){
|
||||||
$this->cacheData = [];
|
$this->cacheData = [];
|
||||||
}
|
}
|
||||||
|
$keys = self::convBackedEnums($keys);
|
||||||
|
|
||||||
if($invalidateAll){
|
if($invalidateAll){
|
||||||
$notExists = $keys;
|
$notExists = $keys;
|
||||||
@@ -163,6 +191,8 @@ class KVStorage{
|
|||||||
if(!$keys){
|
if(!$keys){
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
$keys = static::convBackedEnums($keys);
|
||||||
|
|
||||||
$dictResult = $this->getValues($keys, $onlyCache);
|
$dictResult = $this->getValues($keys, $onlyCache);
|
||||||
$result = [];
|
$result = [];
|
||||||
foreach($keys as $key){
|
foreach($keys as $key){
|
||||||
@@ -175,6 +205,8 @@ class KVStorage{
|
|||||||
if(!$keys){
|
if(!$keys){
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
$keys = static::convBackedEnums($keys);
|
||||||
|
|
||||||
if ($this->cacheData === null) {
|
if ($this->cacheData === null) {
|
||||||
return $this->getDBValues($keys);
|
return $this->getDBValues($keys);
|
||||||
}
|
}
|
||||||
@@ -184,6 +216,7 @@ class KVStorage{
|
|||||||
|
|
||||||
//TODO: DB Select에서 as를 쓸 수 있으면 좋을 듯.
|
//TODO: DB Select에서 as를 쓸 수 있으면 좋을 듯.
|
||||||
foreach($keys as $key){
|
foreach($keys as $key){
|
||||||
|
|
||||||
if(!key_exists($key, $this->cacheData)){
|
if(!key_exists($key, $this->cacheData)){
|
||||||
$notExists[] = $key;
|
$notExists[] = $key;
|
||||||
continue;
|
continue;
|
||||||
@@ -208,12 +241,8 @@ class KVStorage{
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getValue(string|int|\BackedEnum $key, bool $onlyCache=false){
|
||||||
* @param string|int $key
|
$key = static::convBackedEnum($key);
|
||||||
* @param bool $onlyCache
|
|
||||||
* @return mixed|null
|
|
||||||
*/
|
|
||||||
public function getValue($key, bool $onlyCache=false){
|
|
||||||
if($this->cacheData !== null && ($onlyCache || key_exists($key, $this->cacheData))){
|
if($this->cacheData !== null && ($onlyCache || key_exists($key, $this->cacheData))){
|
||||||
return $this->cacheData[$key] ?? null;
|
return $this->cacheData[$key] ?? null;
|
||||||
}
|
}
|
||||||
@@ -226,13 +255,13 @@ class KVStorage{
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param string|int $key
|
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return KVStorage
|
* @return KVStorage
|
||||||
* @throws MeekroDBException
|
* @throws MeekroDBException
|
||||||
*/
|
*/
|
||||||
public function setValue($key, $value):self{
|
public function setValue(string|int|\BackedEnum $key, $value):self{
|
||||||
|
$key = static::convBackedEnum($key);
|
||||||
|
|
||||||
if($value === null){
|
if($value === null){
|
||||||
return $this->deleteValue($key);
|
return $this->deleteValue($key);
|
||||||
}
|
}
|
||||||
@@ -250,7 +279,9 @@ class KVStorage{
|
|||||||
* @return KVStorage
|
* @return KVStorage
|
||||||
* @throws MeekroDBException
|
* @throws MeekroDBException
|
||||||
*/
|
*/
|
||||||
public function deleteValue($key):self{
|
public function deleteValue(string|int|\BackedEnum $key):self{
|
||||||
|
$key = static::convBackedEnum($key);
|
||||||
|
|
||||||
if(isset($this->cacheData[$key])){
|
if(isset($this->cacheData[$key])){
|
||||||
unset($this->cacheData[$key]);
|
unset($this->cacheData[$key]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user