1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\Asana\Api; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use DateInterval; |
7
|
|
|
use Helix\Asana\Api; |
8
|
|
|
use Helix\Asana\Base\AbstractEntity; |
9
|
|
|
use Helix\Asana\Base\Data; |
10
|
|
|
use Psr\SimpleCache\CacheInterface as PSR16; |
11
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Adapts a `PSR-16` cache to the runtime entity pool. |
15
|
|
|
* |
16
|
|
|
* Concurrency locks can be added by extending this class. |
17
|
|
|
* |
18
|
|
|
* @see https://www.php-fig.org/psr/psr-16/ |
19
|
|
|
*/ |
20
|
|
|
class SimpleCache extends Cache { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var PSR16 |
24
|
|
|
*/ |
25
|
|
|
protected $psr; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Defaults to 1 hour. |
29
|
|
|
* |
30
|
|
|
* @var DateInterval |
31
|
|
|
*/ |
32
|
|
|
protected $ttl; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param PSR16 $psr |
36
|
|
|
*/ |
37
|
|
|
public function __construct (PSR16 $psr) { |
38
|
|
|
$this->psr = $psr; |
39
|
|
|
$this->ttl = new DateInterval('PT1H'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $key |
44
|
|
|
* @throws InvalidArgumentException |
45
|
|
|
*/ |
46
|
|
|
protected function _delete (string $key) { |
47
|
|
|
$this->psr->delete('asana:' . $key); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $key |
52
|
|
|
* @return null|string|AbstractEntity |
53
|
|
|
* @throws InvalidArgumentException |
54
|
|
|
*/ |
55
|
|
|
protected function _get (string $key) { |
56
|
|
|
return $this->psr->get('asana:' . $key); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $key |
61
|
|
|
* @param mixed $value |
62
|
|
|
* @throws InvalidArgumentException |
63
|
|
|
*/ |
64
|
|
|
protected function _set (string $key, $value) { |
65
|
|
|
$this->psr->set('asana:' . $key, $value, $this->ttl); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param AbstractEntity $entity |
70
|
|
|
* @return bool |
71
|
|
|
* @throws InvalidArgumentException |
72
|
|
|
*/ |
73
|
|
|
public function add (AbstractEntity $entity): bool { |
74
|
|
|
if (parent::add($entity)) { |
75
|
|
|
$this->_set($entity->getGid(), $entity); |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $gid |
83
|
|
|
* @param string[] $keys |
84
|
|
|
* @throws InvalidArgumentException |
85
|
|
|
*/ |
86
|
|
|
protected function addKeys (string $gid, array $keys): void { |
87
|
|
|
parent::addKeys($gid, $keys); |
88
|
|
|
// stash gid refs |
89
|
|
|
foreach ($keys as $key) { |
90
|
|
|
if ($key != $gid) { // loose, key may actually be int |
91
|
|
|
$this->_set($key, $gid); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $key |
98
|
|
|
* @param Api|Data $caller |
99
|
|
|
* @param Closure $factory |
100
|
|
|
* @return null|AbstractEntity |
101
|
|
|
* @throws InvalidArgumentException |
102
|
|
|
*/ |
103
|
|
|
public function get (string $key, $caller, Closure $factory) { |
104
|
|
|
// POOL MISS && CACHE HIT |
105
|
|
|
if (!isset($this->gids[$key]) and $entity = $this->_get($key)) { |
106
|
|
|
// dereference gid? |
107
|
|
|
if (is_string($entity)) { |
108
|
|
|
// recurse (once) for the entity |
109
|
|
|
if ($entity = $this->get($entity, $caller, $factory)) { |
110
|
|
|
return $entity; |
111
|
|
|
} |
112
|
|
|
// remove bad ref |
113
|
|
|
$this->_delete($key); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
// POOL HIT || CACHE MISS |
117
|
|
|
return parent::get($key, $caller, $factory); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return DateInterval |
122
|
|
|
*/ |
123
|
|
|
public function getTtl () { |
124
|
|
|
return $this->ttl; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param AbstractEntity $entity |
129
|
|
|
* @throws InvalidArgumentException |
130
|
|
|
*/ |
131
|
|
|
public function remove (AbstractEntity $entity): void { |
132
|
|
|
parent::remove($entity); |
133
|
|
|
foreach ($entity->getCacheKeys() as $key) { |
134
|
|
|
$this->_delete($key); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param DateInterval $ttl |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function setTtl (DateInterval $ttl) { |
143
|
|
|
$this->ttl = $ttl; |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |