-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoltDbCache.php
78 lines (47 loc) · 1.52 KB
/
BoltDbCache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Tilwa\Adapters\Cache;
use Tilwa\Contracts\IO\{CacheManager, EnvAccessor};
use Spiral\Goridge\RPC\RPC;
use Spiral\RoadRunner\KeyValue\Factory;
use Psr\SimpleCache\CacheInterface;
class BoltDbCache implements CacheManager {
const TAG_KEY = "_reserved_key_tags";
private $client, $envAccessor, $activeNodeName;
public function __construct (EnvAccessor $envAccessor) {
$this->envAccessor = $envAccessor;
}
public function setupClient ():void {
$this->client = new Factory(RPC::create(
$this->envAccessor->getField("RR_RPC")
));
}
/**
* @param {storageName} Must match one of the storages defined in the key-value section of rr.yaml
*/
public function openDataNode (string $storageName):self {
$this->activeNodeName = $storageName;
return $this;
}
public function getItem (string $key) {
return $this->getDataNode()->get($key);
}
public function saveItem (string $key, $data):void {
$this->getDataNode()->set($key, $data);
}
public function getDataNode ():CacheInterface {
return $this->client->select($this->activeNodeName);
}
public function tagItem (string $key, $data):void {
$dataNode = $this->getDataNode();
$allTags = $dataNode->get(self::TAG_KEY);
if (is_null($allTags)) $allTags = [];
if (!array_key_exists($key, $allTags))
$allTags[$key] = [];
$allTags[$key][] = $data;
$dataNode->set(self::TAG_KEY, $allTags);
}
public function deleteItem (string $key) {
$this->getDataNode()->set($key, null);
}
}
?>