From ab6bb54eaea87b6fd4874285215697a9d594d7e2 Mon Sep 17 00:00:00 2001 From: hide_d Date: Sat, 12 May 2018 04:59:44 +0900 Subject: [PATCH] =?UTF-8?q?KVStorage=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/sammo/KVStorage.php | 218 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 hwe/sammo/KVStorage.php diff --git a/hwe/sammo/KVStorage.php b/hwe/sammo/KVStorage.php new file mode 100644 index 00000000..518e01de --- /dev/null +++ b/hwe/sammo/KVStorage.php @@ -0,0 +1,218 @@ +storNamespace = $storNamespace; + if($cacheMode){ + $this->cacheData = []; + } + } + + public function __get($key) { + return $this->getValue($key); + } + + public function __set($key, $value) { + return $this->setValue($key, $value); + } + + public function resetCache(bool $disableCache=true):self{ + if($disableCache){ + $this->cacheData = null; + } + else{ + $this->cacheData = []; + } + return $this; + } + + public function invalidateCacheValue($key):self{ + if($this->cacheData === null){ + return $this; + } + if(key_exists($key, $this->cacheData)){ + unset($this->cacheData[$key]); + } + return $this; + } + + public function invalidateCacheValues(array $keys):self{ + if($this->cacheData === null){ + return $this; + } + foreach($keys as $key){ + if(key_exists($key, $this->cacheData)){ + unset($this->cacheData[$key]); + } + } + + return $this; + } + + public function cacheAll():self{ + $this->cacheData = $this->getDBAll(); + return $this; + } + + public function cacheValues(array $keys):self{ + if($this->cacheData === null){ + $this->cacheData = []; + } + $values = $this->getDBValues($keys); + foreach($keys as $key){ + if(key_exists($key, $values)){ + $this->cacheData[$key] = $values[$key]; + } + else{ + $this->cacheData[$key] = null; + } + } + + return $this; + } + + public function getAll(bool $onlyCache=false): array{ + if($onlyCache && $this->cacheData !== null && count($this->cacheData) > 0){ + return $this->cacheData; + } + $result = $this->getAll(); + if($this->cacheData !== null){ + $this->cacheData = $result; + } + return $result; + } + + public function getValues(array $keys, bool $onlyCache=false): array{ + if ($this->cacheData === null) { + return $this->getDBValues($keys); + } + + $result = []; + $notExists = []; + + foreach($keys as $key){ + if(!key_exists($key, $this->cacheData)){ + $notExists[] = $key; + continue; + } + + $result[$key] = $this->cacheData[$key]; + } + + if($onlyCache){ + foreach($notExists as $emptyKey){ + $result[$emptyKey] = null; + } + + return $result; + } + + $dbResult = $this->getDBValues($notExists); + foreach($dbResult as $key=>$value){ + $result[$key] = $value; + $this->cacheData[$key] = $value; + } + return $result; + } + + public function getValue($key, bool $onlyCache=false){ + if($this->cacheData !== null && ($onlyCache || key_exists($key, $this->cacheData))){ + return $this->cacheData[$key] ?? null; + } + + $value = $this->getDBValue($key); + if($this->cacheData !== null){ + $this->cacheData[$key] = $value; + } + return $value; + } + + public function setValue($key, $value):self{ + if($value === null){ + return $this->deleteValue($key); + } + + if($this->cacheData){ + $this->cacheData[$key] = $value; + } + return $this->setDBValue($key, $value); + } + + public function deleteValue($key):self{ + if(isset($this->cacheData[$key])){ + unset($this->cacheData[$key]); + } + return $this->deleteDBValue($key); + } + + private function getDBAll(): array{ + $db = DB::db(); + $result = []; + foreach($db->queryAllLists( + 'SELECT `key`, `value` FROM storage WHERE `namespace`=%s', + $this->storNamespace + ) as list($key, $value)) + { + $result[$key] = Json::decode($value); + } + return $value; + } + + private function getDBValues(array $keys): array{ + $db = DB::db(); + $result = []; + foreach($db->queryAllLists( + 'SELECT `key`, `value` FROM storage WHERE `namespace`=%s AND `key` IN %ls', + $this->storNamespace, + $keys + ) as list($key, $value)) + { + $result[$key] = Json::decode($value); + } + foreach($keys as $key){ + if(!key_exists($key, $result)){ + $result[$key] = null; + } + } + return $value; + } + + private function getDBValue($key){ + $db = DB::db(); + $value = $db->queryFirstField( + 'SELECT `value` FROM storage WHERE `namespace`=%s AND `key`=%s', + $this->storNamespace, + $key + ); + if($value === null){ + return null; + } + return Json::decode($value); + } + + private function setDBValue($key, $value):self{ + if($value === null){ + return $this->deleteDBValue($key); + } + $db = DB::db(); + $db->insertUpdate('storage', [ + 'namespace'=>$this->storNamespace, + 'key'=>$key, + 'value'=>Json::encode($value) + ]); + return $this; + } + + private function deleteDBValue($key):self{ + $db = DB::db(); + $db->delete('storage', [ + 'namespace'=>$this->storNamespace, + 'key'=>$key + ]); + return $this; + } +} \ No newline at end of file