1
|
|
|
<?php |
2
|
|
|
namespace Metaphore; |
3
|
|
|
|
4
|
|
|
use Metaphore\Value; |
5
|
|
|
use Metaphore\ForeignValue; |
6
|
|
|
use Metaphore\Store\ValueStoreInterface; |
7
|
|
|
use Metaphore\Store\LockStoreInterface; |
8
|
|
|
use Metaphore\LockManager; |
9
|
|
|
use Metaphore\Exception; |
10
|
|
|
use Metaphore\Ttl; |
11
|
|
|
use Metaphore\NoStaleCacheEvent; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Main class handling cache. |
15
|
|
|
*/ |
16
|
|
|
class Cache |
17
|
|
|
{ |
18
|
|
|
/*** @var \Metaphore\Store\ValueStoreInterface */ |
19
|
|
|
protected $valueStore; |
20
|
|
|
|
21
|
|
|
/*** @var \Metaphore\LockManager */ |
22
|
|
|
protected $lockManager; |
23
|
|
|
|
24
|
|
|
/*** @var callable */ |
25
|
|
|
protected $onNoStaleCacheCallable; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \Metaphore\Store\ValueStoreInterface |
29
|
|
|
* @param \Metaphore\Store\LockStoreInterface |
30
|
|
|
* @throws \Metaphore\Exception When value store cannot be used as lock store by default. |
31
|
|
|
*/ |
32
|
|
|
public function __construct(ValueStoreInterface $valueStore, LockManager $lockManager = null) |
33
|
|
|
{ |
34
|
|
|
$this->valueStore = $valueStore; |
35
|
|
|
|
36
|
|
|
if (!$lockManager) { |
37
|
|
|
if (!($valueStore instanceof LockStoreInterface)) { |
38
|
|
|
throw new Exception( |
39
|
|
|
sprintf('%s does not implement LockStoreInterface. ', get_class($valueStore)). |
40
|
|
|
'Please provide lock manager or value store that\'s compatible with lock store. ' |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$lockManager = new LockManager($valueStore); |
45
|
|
|
} |
46
|
|
|
$this->lockManager = $lockManager; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Caches specified closure/method/function for specified time. |
51
|
|
|
* |
52
|
|
|
* As a third argument - instead of integer - you can pass Ttt object to |
53
|
|
|
* define grace tll and lock ttl (both optional). |
54
|
|
|
* |
55
|
|
|
* @param string |
56
|
|
|
* @param callable |
57
|
|
|
* @param int|\Metaphore\Ttl |
58
|
|
|
* @param callable |
59
|
|
|
*/ |
60
|
|
|
public function cache($key, callable $cachedCallable, $ttl, callable $onNoStaleCacheCallable = null) |
61
|
|
|
{ |
62
|
|
|
$value = $this->getValue($key); |
63
|
|
|
|
64
|
|
|
if ($value->hasResult() && !$value->isStale()) { |
65
|
|
|
return $value->getResult(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!($ttl instanceof Ttl)) { |
69
|
|
|
$ttl = new Ttl($ttl); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$lock_acquired = $this->lockManager->acquire($key, $ttl->getLockTtl()); |
73
|
|
|
|
74
|
|
|
if (!$lock_acquired) { |
75
|
|
|
if ($value->hasResult()) { // serve stale if present |
76
|
|
|
return $value->getResult(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (!$onNoStaleCacheCallable) { |
80
|
|
|
$onNoStaleCacheCallable = $this->onNoStaleCacheCallable; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($onNoStaleCacheCallable !== null) { |
84
|
|
|
$event = new NoStaleCacheEvent($this, $key, $cachedCallable, $ttl); |
85
|
|
|
|
86
|
|
|
call_user_func($onNoStaleCacheCallable, $event); |
87
|
|
|
|
88
|
|
|
if ($event->hasResult()) { |
89
|
|
|
return $event->getResult(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$result = call_user_func($cachedCallable); |
95
|
|
|
|
96
|
|
|
$this->setResult($key, $result, $ttl); |
97
|
|
|
|
98
|
|
|
if ($lock_acquired) { |
99
|
|
|
$this->lockManager->release($key); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $result; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
public function delete($key) |
109
|
|
|
{ |
110
|
|
|
return $this->valueStore->delete($key); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return Value |
115
|
|
|
*/ |
116
|
|
|
public function getValue($key) |
117
|
|
|
{ |
118
|
|
|
$value = $this->valueStore->get($key); |
119
|
|
|
|
120
|
|
|
if (!($value instanceof Value)) { |
121
|
|
|
$value = new ForeignValue($value); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $value; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Sets result. Does not use anti-dogpile-effect mechanism. Use cache() instead for this. |
129
|
|
|
* |
130
|
|
|
* @param string |
131
|
|
|
* @param mixed |
132
|
|
|
* @param int|\Metaphore\Ttl |
133
|
|
|
*/ |
134
|
|
|
public function setResult($key, $result, $ttl) |
135
|
|
|
{ |
136
|
|
|
if (!($ttl instanceof Ttl)) { |
137
|
|
|
$ttl = new Ttl($ttl); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$expirationTimestamp = time() + $ttl->getTtl(); |
141
|
|
|
$value = new Value($result, $expirationTimestamp); |
142
|
|
|
|
143
|
|
|
$this->valueStore->set($key, $value, $ttl->getRealTtl()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param callable |
148
|
|
|
*/ |
149
|
|
|
public function onNoStaleCache(callable $onNoStaleCacheCallable) |
150
|
|
|
{ |
151
|
|
|
$this->onNoStaleCacheCallable = $onNoStaleCacheCallable; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return ValueStoreInterface |
156
|
|
|
*/ |
157
|
|
|
public function getValueStore() |
158
|
|
|
{ |
159
|
|
|
return $this->valueStore; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return LockManager |
164
|
|
|
*/ |
165
|
|
|
public function getLockManager() |
166
|
|
|
{ |
167
|
|
|
return $this->lockManager; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|