-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheBridge.php
More file actions
81 lines (71 loc) · 2.29 KB
/
CacheBridge.php
File metadata and controls
81 lines (71 loc) · 2.29 KB
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
79
80
81
<?php
declare(strict_types=1);
namespace KaririCode\ClassDiscovery\Integration;
use KaririCode\ClassDiscovery\Contract\CacheStrategy;
use KaririCode\ClassDiscovery\Contract\DiscoveryResult;
/**
* Bridge to KaririCode\Cache PSR-16 simple cache.
*
* Adapts PSR-16 SimpleCache to CacheStrategy contract,
* enabling transparent use of Redis, Memcached, or any
* PSR-16 compliant cache with the discovery system.
*
* Optional dependency: kariricode/cache
*
* @package KaririCode\ClassDiscovery\Integration
* @author Walmir Silva <walmir.silva@kariricode.org>
* @license MIT
* @since 3.1.0
*/
final readonly class CacheBridge implements CacheStrategy
{
/**
* @param object $simpleCache PSR-16 CacheInterface instance
* (typed as object to avoid hard dependency on psr/simple-cache)
* @param string $prefix Key prefix to avoid collisions
*/
public function __construct(
/** @psalm-suppress PropertyNotSetInConstructor */
private readonly object $simpleCache,
private readonly string $prefix = 'kc_discovery_',
) {
}
#[\Override]
public function get(string $key): ?DiscoveryResult
{
/** @var DiscoveryResult|null $result */
// @phpstan-ignore method.notFound
$result = $this->simpleCache->get($this->prefix . $key);
return $result instanceof DiscoveryResult ? $result : null;
}
#[\Override]
public function set(string $key, DiscoveryResult $result, int $ttl = 3600): void
{
// @phpstan-ignore method.notFound
$this->simpleCache->set(
$this->prefix . $key,
$result,
$ttl > 0 ? $ttl : null,
);
}
#[\Override]
public function delete(string $key): void
{
// @phpstan-ignore method.notFound
$this->simpleCache->delete($this->prefix . $key);
}
#[\Override]
public function clear(): void
{
// PSR-16 clear() clears everything — not ideal.
// Prefer iterating known keys if the cache supports it.
// @phpstan-ignore method.notFound
$this->simpleCache->clear();
}
#[\Override]
public function has(string $key): bool
{
// @phpstan-ignore method.notFound
return $this->simpleCache->has($this->prefix . $key);
}
}