Skip to content

Commit d206c00

Browse files
committedMar 31, 2020
Cache: added events WIP
1 parent a332f96 commit d206c00

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed
 

‎src/Caching/Cache.php

+21-6
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,18 @@ class Cache
3333
NAMESPACES = 'namespaces',
3434
ALL = 'all';
3535

36+
public const
37+
EVENT_HIT = 'hit',
38+
EVENT_MISS = 'miss',
39+
EVENT_SAVE = 'save',
40+
EVENT_REMOVE = 'remove';
41+
3642
/** @internal */
3743
public const NAMESPACE_SEPARATOR = "\x00";
3844

45+
/** @var array */
46+
public $onEvent;
47+
3948
/** @var Storage */
4049
private $storage;
4150

@@ -88,6 +97,7 @@ public function load($key, callable $generator = null)
8897
{
8998
$storageKey = $this->generateKey($key);
9099
$data = $this->storage->read($storageKey);
100+
$this->onEvent($this, $data === null ? self::EVENT_MISS : self::EVENT_HIT, $key);
91101
if ($data === null && $generator) {
92102
$this->storage->lock($storageKey);
93103
try {
@@ -134,12 +144,14 @@ public function bulkLoad(array $keys, callable $generator = null): array
134144
foreach ($keys as $i => $key) {
135145
$storageKey = $storageKeys[$i];
136146
if (isset($cacheData[$storageKey])) {
147+
$this->onEvent($this, self::EVENT_HIT, $key);
137148
$result[$key] = $cacheData[$storageKey];
138149
} elseif ($generator) {
139150
$result[$key] = $this->load($key, function (&$dependencies) use ($key, $generator) {
140151
return $generator(...[$key, &$dependencies]);
141152
});
142153
} else {
154+
$this->onEvent($this, self::EVENT_MISS, $key);
143155
$result[$key] = null;
144156
}
145157
}
@@ -165,27 +177,30 @@ public function bulkLoad(array $keys, callable $generator = null): array
165177
*/
166178
public function save($key, $data, array $dependencies = null)
167179
{
168-
$key = $this->generateKey($key);
180+
$storageKey = $this->generateKey($key);
169181

170182
if ($data instanceof \Closure) {
171183
trigger_error(__METHOD__ . '() closure argument is deprecated.', E_USER_WARNING);
172-
$this->storage->lock($key);
184+
$this->storage->lock($storageKey);
173185
try {
174186
$data = $data(...[&$dependencies]);
175187
} catch (\Throwable $e) {
176-
$this->storage->remove($key);
188+
$this->storage->remove($storageKey);
177189
throw $e;
178190
}
179191
}
180192

181193
if ($data === null) {
182-
$this->storage->remove($key);
194+
$this->storage->remove($storageKey);
195+
$this->onEvent($this, self::EVENT_REMOVE, $key);
183196
} else {
184197
$dependencies = $this->completeDependencies($dependencies);
185198
if (isset($dependencies[self::EXPIRATION]) && $dependencies[self::EXPIRATION] <= 0) {
186-
$this->storage->remove($key);
199+
$this->storage->remove($storageKey);
200+
$this->onEvent($this, self::EVENT_REMOVE, $key);
187201
} else {
188-
$this->storage->write($key, $data, $dependencies);
202+
$this->storage->write($storageKey, $data, $dependencies);
203+
$this->onEvent($this, self::EVENT_SAVE, $key);
189204
}
190205
return $data;
191206
}

‎tests/Caching/Cache.bulkLoad.phpt

+24
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,54 @@ require __DIR__ . '/Cache.php';
1818
test(function () {
1919
$storage = new TestStorage;
2020
$cache = new Cache($storage, 'ns');
21+
$cache->onEvent[] = function (...$args) use (&$event) {
22+
$event[] = $args;
23+
};
24+
2125
Assert::same([1 => null, 2 => null], $cache->bulkLoad([1, 2]), 'data');
26+
Assert::same([[$cache, $cache::EVENT_MISS, 1], [$cache, $cache::EVENT_MISS, 2]], $event);
2227

28+
$event = [];
2329
Assert::same([1 => 1, 2 => 2], $cache->bulkLoad([1, 2], function ($key) {
2430
return $key;
2531
}));
32+
Assert::same([
33+
[$cache, $cache::EVENT_MISS, 1], [$cache, $cache::EVENT_SAVE, 1],
34+
[$cache, $cache::EVENT_MISS, 2], [$cache, $cache::EVENT_SAVE, 2],
35+
], $event);
2636

37+
$event = [];
2738
$data = $cache->bulkLoad([1, 2]);
2839
Assert::same(1, $data[1]['data']);
2940
Assert::same(2, $data[2]['data']);
41+
Assert::same([[$cache, $cache::EVENT_HIT, 1], [$cache, $cache::EVENT_HIT, 2]], $event);
3042
});
3143

3244
// storage with bulk load support
3345
test(function () {
3446
$storage = new BulkReadTestStorage;
3547
$cache = new Cache($storage, 'ns');
48+
$cache->onEvent[] = function (...$args) use (&$event) {
49+
$event[] = $args;
50+
};
51+
3652
Assert::same([1 => null, 2 => null], $cache->bulkLoad([1, 2]));
53+
Assert::same([[$cache, $cache::EVENT_MISS, 1], [$cache, $cache::EVENT_MISS, 2]], $event);
3754

55+
$event = [];
3856
Assert::same([1 => 1, 2 => 2], $cache->bulkLoad([1, 2], function ($key) {
3957
return $key;
4058
}));
59+
Assert::same([
60+
[$cache, $cache::EVENT_MISS, 1], [$cache, $cache::EVENT_SAVE, 1],
61+
[$cache, $cache::EVENT_MISS, 2], [$cache, $cache::EVENT_SAVE, 2],
62+
], $event);
4163

64+
$event = [];
4265
$data = $cache->bulkLoad([1, 2]);
4366
Assert::same(1, $data[1]['data']);
4467
Assert::same(2, $data[2]['data']);
68+
Assert::same([[$cache, $cache::EVENT_HIT, 1], [$cache, $cache::EVENT_HIT, 2]], $event);
4569
});
4670

4771
// dependencies

‎tests/Caching/Cache.load.phpt

+9
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,25 @@ require __DIR__ . '/Cache.php';
1818
// load twice with fallback
1919
$storage = new TestStorage;
2020
$cache = new Cache($storage, 'ns');
21+
$cache->onEvent[] = function (...$args) use (&$event) {
22+
$event[] = $args;
23+
};
2124

2225
$value = $cache->load('key', function () {
2326
return 'value';
2427
});
2528
Assert::same('value', $value);
29+
Assert::same([
30+
[$cache, $cache::EVENT_MISS, 'key'],
31+
[$cache, $cache::EVENT_SAVE, 'key'],
32+
], $event);
2633

34+
$event = [];
2735
$data = $cache->load('key', function () {
2836
return "won't load this value"; // will read from storage
2937
});
3038
Assert::same('value', $data['data']);
39+
Assert::same([[$cache, $cache::EVENT_HIT, 'key']], $event);
3140

3241

3342
// load twice with closure fallback, pass dependencies

‎tests/Caching/Cache.save.phpt

+4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ require __DIR__ . '/Cache.php';
1818
// save value with dependencies
1919
$storage = new testStorage;
2020
$cache = new Cache($storage, 'ns');
21+
$cache->onEvent[] = function (...$args) use (&$event) {
22+
$event[] = $args;
23+
};
2124
$dependencies = [Cache::TAGS => ['tag']];
2225

2326
$cache->save('key', 'value', $dependencies);
27+
Assert::same([[$cache, $cache::EVENT_SAVE, 'key']], $event);
2428

2529
$res = $cache->load('key');
2630
Assert::same('value', $res['data']);

0 commit comments

Comments
 (0)
Failed to load comments.