KVStorage가 사용할 db를 직접 선택할 수 있도록 변경.

KVStorage를 전역으로 이동
This commit is contained in:
2018-05-12 05:40:15 +09:00
parent d02a84e87e
commit 29ce47251a
@@ -2,10 +2,12 @@
namespace sammo; namespace sammo;
class KVStorage{ class KVStorage{
private $db = null;
private $storNamespace; private $storNamespace;
private $cacheData = null; private $cacheData = null;
public function __construct(string $storNamespace, bool $cacheMode=false){ public function __construct(\MeekroDB $db, string $storNamespace, bool $cacheMode=false){
$this->db = $db;
$this->storNamespace = $storNamespace; $this->storNamespace = $storNamespace;
if($cacheMode){ if($cacheMode){
$this->cacheData = []; $this->cacheData = [];
@@ -157,9 +159,8 @@ class KVStorage{
} }
private function getDBAll(): array{ private function getDBAll(): array{
$db = DB::db();
$result = []; $result = [];
foreach($db->queryAllLists( foreach($this->db->queryAllLists(
'SELECT `key`, `value` FROM storage WHERE `namespace`=%s', 'SELECT `key`, `value` FROM storage WHERE `namespace`=%s',
$this->storNamespace $this->storNamespace
) as list($key, $value)) ) as list($key, $value))
@@ -170,9 +171,8 @@ class KVStorage{
} }
private function getDBValues(array $keys): array{ private function getDBValues(array $keys): array{
$db = DB::db();
$result = []; $result = [];
foreach($db->queryAllLists( foreach($this->db->queryAllLists(
'SELECT `key`, `value` FROM storage WHERE `namespace`=%s AND `key` IN %ls', 'SELECT `key`, `value` FROM storage WHERE `namespace`=%s AND `key` IN %ls',
$this->storNamespace, $this->storNamespace,
$keys $keys
@@ -189,8 +189,7 @@ class KVStorage{
} }
private function getDBValue($key){ private function getDBValue($key){
$db = DB::db(); $value = $this->db->queryFirstField(
$value = $db->queryFirstField(
'SELECT `value` FROM storage WHERE `namespace`=%s AND `key`=%s', 'SELECT `value` FROM storage WHERE `namespace`=%s AND `key`=%s',
$this->storNamespace, $this->storNamespace,
$key $key
@@ -205,8 +204,7 @@ class KVStorage{
if($value === null){ if($value === null){
return $this->deleteDBValue($key); return $this->deleteDBValue($key);
} }
$db = DB::db(); $this->db->insertUpdate('storage', [
$db->insertUpdate('storage', [
'namespace'=>$this->storNamespace, 'namespace'=>$this->storNamespace,
'key'=>$key, 'key'=>$key,
'value'=>Json::encode($value) 'value'=>Json::encode($value)
@@ -215,8 +213,7 @@ class KVStorage{
} }
private function deleteDBValue($key):self{ private function deleteDBValue($key):self{
$db = DB::db(); $this->db->delete('storage', [
$db->delete('storage', [
'namespace'=>$this->storNamespace, 'namespace'=>$this->storNamespace,
'key'=>$key 'key'=>$key
]); ]);
@@ -224,8 +221,7 @@ class KVStorage{
} }
private function resetDBNamespace():self{ private function resetDBNamespace():self{
$db = DB::db(); $this->db->delete('storage', [
$db->delete('storage', [
'namespace'=>$this->storNamespace 'namespace'=>$this->storNamespace
]); ]);
return $this; return $this;