Skip to content

Commit e565e17

Browse files
committed
Cache: added events WIP
1 parent 51fcc1b commit e565e17

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

src/Caching/Cache.php

Lines changed: 22 additions & 7 deletions
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

@@ -87,8 +96,9 @@ public function load($key, callable $generator = null)
8796
{
8897
$storageKey = $this->generateKey($key);
8998
$data = $this->storage->read($storageKey);
99+
$this->onEvent($this, $data === null ? self::EVENT_MISS : self::EVENT_HIT, $key);
90100
if ($data === null && $generator) {
91-
$this->storage->lock($storageKey);
101+
$this->storage->lock($storageKey);
92102
try {
93103
$data = $generator(...[&$dependencies]);
94104
} catch (\Throwable $e) {
@@ -135,12 +145,14 @@ public function bulkLoad(array $keys, callable $generator = null): array
135145
foreach ($keys as $i => $key) {
136146
$storageKey = $storageKeys[$i];
137147
if (isset($cacheData[$storageKey])) {
148+
$this->onEvent($this, self::EVENT_HIT, $key);
138149
$result[$key] = $cacheData[$storageKey];
139150
} elseif ($generator) {
140151
$result[$key] = $this->load($key, function (&$dependencies) use ($key, $generator) {
141152
return $generator(...[$key, &$dependencies]);
142153
});
143154
} else {
155+
$this->onEvent($this, self::EVENT_MISS, $key);
144156
$result[$key] = null;
145157
}
146158
}
@@ -166,27 +178,30 @@ public function bulkLoad(array $keys, callable $generator = null): array
166178
*/
167179
public function save($key, $data, array $dependencies = null)
168180
{
169-
$key = $this->generateKey($key);
181+
$storageKey = $this->generateKey($key);
170182

171183
if ($data instanceof \Closure) {
172184
trigger_error(__METHOD__ . '() closure argument is deprecated.', E_USER_WARNING);
173-
$this->storage->lock($key);
185+
$this->storage->lock($storageKey);
174186
try {
175187
$data = $data(...[&$dependencies]);
176188
} catch (\Throwable $e) {
177-
$this->storage->remove($key);
189+
$this->storage->remove($storageKey);
178190
throw $e;
179191
}
180192
}
181193

182194
if ($data === null) {
183-
$this->storage->remove($key);
195+
$this->storage->remove($storageKey);
196+
$this->onEvent($this, self::EVENT_REMOVE, $key);
184197
} else {
185198
$dependencies = $this->completeDependencies($dependencies);
186199
if (isset($dependencies[self::EXPIRATION]) && $dependencies[self::EXPIRATION] <= 0) {
187-
$this->storage->remove($key);
200+
$this->storage->remove($storageKey);
201+
$this->onEvent($this, self::EVENT_REMOVE, $key);
188202
} else {
189-
$this->storage->write($key, $data, $dependencies);
203+
$this->storage->write($storageKey, $data, $dependencies);
204+
$this->onEvent($this, self::EVENT_SAVE, $key);
190205
}
191206
return $data;
192207
}

tests/Caching/Cache.bulkLoad.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,53 @@ require __DIR__ . '/Cache.php';
1717
test('storage without bulk load support', function () {
1818
$storage = new TestStorage;
1919
$cache = new Cache($storage, 'ns');
20+
$cache->onEvent[] = function (...$args) use (&$event) {
21+
$event[] = $args;
22+
};
23+
2024
Assert::same([1 => null, 2 => null], $cache->bulkLoad([1, 2]), 'data');
25+
Assert::same([[$cache, $cache::EVENT_MISS, 1], [$cache, $cache::EVENT_MISS, 2]], $event);
2126

27+
$event = [];
2228
Assert::same([1 => 1, 2 => 2], $cache->bulkLoad([1, 2], function ($key) {
2329
return $key;
2430
}));
31+
Assert::same([
32+
[$cache, $cache::EVENT_MISS, 1], [$cache, $cache::EVENT_SAVE, 1],
33+
[$cache, $cache::EVENT_MISS, 2], [$cache, $cache::EVENT_SAVE, 2],
34+
], $event);
2535

36+
$event = [];
2637
$data = $cache->bulkLoad([1, 2]);
2738
Assert::same(1, $data[1]['data']);
2839
Assert::same(2, $data[2]['data']);
40+
Assert::same([[$cache, $cache::EVENT_HIT, 1], [$cache, $cache::EVENT_HIT, 2]], $event);
2941
});
3042

3143
test('storage with bulk load support', function () {
3244
$storage = new BulkReadTestStorage;
3345
$cache = new Cache($storage, 'ns');
46+
$cache->onEvent[] = function (...$args) use (&$event) {
47+
$event[] = $args;
48+
};
49+
3450
Assert::same([1 => null, 2 => null], $cache->bulkLoad([1, 2]));
51+
Assert::same([[$cache, $cache::EVENT_MISS, 1], [$cache, $cache::EVENT_MISS, 2]], $event);
3552

53+
$event = [];
3654
Assert::same([1 => 1, 2 => 2], $cache->bulkLoad([1, 2], function ($key) {
3755
return $key;
3856
}));
57+
Assert::same([
58+
[$cache, $cache::EVENT_MISS, 1], [$cache, $cache::EVENT_SAVE, 1],
59+
[$cache, $cache::EVENT_MISS, 2], [$cache, $cache::EVENT_SAVE, 2],
60+
], $event);
3961

62+
$event = [];
4063
$data = $cache->bulkLoad([1, 2]);
4164
Assert::same(1, $data[1]['data']);
4265
Assert::same(2, $data[2]['data']);
66+
Assert::same([[$cache, $cache::EVENT_HIT, 1], [$cache, $cache::EVENT_HIT, 2]], $event);
4367
});
4468

4569
test('dependencies', function () {

tests/Caching/Cache.load.phpt

Lines changed: 9 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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)