|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Uxmp\Core\Component\Cache; |
|
6
|
|
|
|
|
7
|
|
|
use Predis\ClientInterface; |
|
8
|
|
|
use Predis\Connection\ConnectionException; |
|
9
|
|
|
use Psr\Cache\CacheException; |
|
10
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
11
|
|
|
use Psr\Container\ContainerInterface; |
|
12
|
|
|
use Psr\Log\LoggerInterface; |
|
13
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
|
14
|
|
|
use Symfony\Component\Cache\Adapter\PhpFilesAdapter; |
|
15
|
|
|
use Symfony\Component\Cache\Adapter\RedisAdapter; |
|
16
|
|
|
use Uxmp\Core\Component\Config\ConfigProviderInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Accessor methods to cache implementations for every cache aspect |
|
20
|
|
|
*/ |
|
21
|
|
|
final class CacheProvider implements CacheProviderInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var array<string, CacheItemPoolInterface> */ |
|
24
|
|
|
private array $cacheHandler = []; |
|
25
|
|
|
|
|
26
|
4 |
|
public function __construct( |
|
27
|
|
|
private readonly ConfigProviderInterface $configProvider, |
|
28
|
|
|
private readonly ContainerInterface $dic, |
|
29
|
|
|
private readonly LoggerInterface $logger, |
|
30
|
|
|
) { |
|
31
|
4 |
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
public function getOrmMetadataCache(): CacheItemPoolInterface |
|
34
|
|
|
{ |
|
35
|
1 |
|
return $this->getCacheImplementation( |
|
36
|
1 |
|
CacheDescriptorEnum::ORM_METADATA, |
|
37
|
1 |
|
'uxmp_metadata' |
|
38
|
1 |
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function getOrmQueryCache(): CacheItemPoolInterface |
|
42
|
|
|
{ |
|
43
|
1 |
|
return $this->getCacheImplementation( |
|
44
|
1 |
|
CacheDescriptorEnum::ORM_QUERY, |
|
45
|
1 |
|
'uxmp_query' |
|
46
|
1 |
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
public function getOrmResultCache(): CacheItemPoolInterface |
|
50
|
|
|
{ |
|
51
|
2 |
|
return $this->getCacheImplementation( |
|
52
|
2 |
|
CacheDescriptorEnum::ORM_RESULT, |
|
53
|
2 |
|
'uxmp_result' |
|
54
|
2 |
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
4 |
|
private function getCacheImplementation( |
|
58
|
|
|
CacheDescriptorEnum $cacheDescriptor, |
|
59
|
|
|
string $cacheName |
|
60
|
|
|
): CacheItemPoolInterface { |
|
61
|
4 |
|
$cacheHandler = $this->cacheHandler[$cacheDescriptor->value] ?? null; |
|
62
|
|
|
|
|
63
|
4 |
|
$this->logger->info( |
|
64
|
4 |
|
sprintf( |
|
65
|
4 |
|
'Lookup cache `%s`', |
|
66
|
4 |
|
$cacheDescriptor->value |
|
67
|
4 |
|
), |
|
68
|
4 |
|
[$this->cacheHandler] |
|
69
|
4 |
|
); |
|
70
|
|
|
|
|
71
|
4 |
|
if ($cacheHandler === null) { |
|
72
|
4 |
|
$cacheType = $this->configProvider->getCacheConfig()[$cacheDescriptor->value]; |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
4 |
|
$cacheHandler = match ($cacheType) { |
|
76
|
4 |
|
CacheTypeEnum::FILE => new PhpFilesAdapter($cacheName), |
|
77
|
4 |
|
CacheTypeEnum::REDIS => new RedisAdapter( |
|
78
|
4 |
|
$this->dic->get(ClientInterface::class), |
|
79
|
4 |
|
$cacheName, |
|
80
|
4 |
|
), |
|
81
|
4 |
|
default => new ArrayAdapter(), |
|
82
|
4 |
|
}; |
|
83
|
1 |
|
} catch (CacheException|ConnectionException $e) { |
|
84
|
1 |
|
$this->logger->error('Cache initialization failed', [$e->getMessage()]); |
|
85
|
|
|
|
|
86
|
1 |
|
$cacheHandler = new ArrayAdapter(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
4 |
|
$this->logger->info( |
|
90
|
4 |
|
sprintf( |
|
91
|
4 |
|
'Using cache adapter `%s` for `%s`', |
|
92
|
4 |
|
$cacheHandler::class, |
|
93
|
4 |
|
$cacheDescriptor->value |
|
94
|
4 |
|
) |
|
95
|
4 |
|
); |
|
96
|
|
|
|
|
97
|
4 |
|
$this->cacheHandler[$cacheDescriptor->value] = $cacheHandler; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
4 |
|
return $cacheHandler; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|