-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDoctrineCacheBridge.php
More file actions
168 lines (148 loc) · 4.31 KB
/
Copy pathDoctrineCacheBridge.php
File metadata and controls
168 lines (148 loc) · 4.31 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
* This file is part of php-cache organization.
*
* (c) 2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Cache\Bridge\Doctrine;
use Doctrine\Common\Cache\CacheProvider;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\InvalidArgumentException;
/**
* This is a bridge between a Doctrine cache and PSR6.
*
* @author Aaron Scherer <aequasi@gmail.com>
*/
class DoctrineCacheBridge extends CacheProvider
{
private CacheItemPoolInterface $cachePool;
/**
* DoctrineCacheBridge constructor.
*/
public function __construct(CacheItemPoolInterface $cachePool)
{
$this->cachePool = $cachePool;
}
public function getCachePool(): CacheItemPoolInterface
{
return $this->cachePool;
}
/**
* Fetches an entry from the cache.
*
* @param string $id the id of the cache entry to fetch
*
* @return mixed|false the cached data or FALSE, if no cache entry exists for the given id
*/
protected function doFetch($id): mixed
{
$item = $this->getItemWithPortableKeyFallback($id);
if ($item->isHit()) {
return $item->get();
}
return false;
}
/**
* Tests if an entry exists in the cache.
*
* @param string $id the cache id of the entry to check for
*
* @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise
*/
protected function doContains($id): bool
{
return $this->withPortableKeyFallback(
$id,
fn (string $key): bool => $this->cachePool->hasItem($key)
);
}
/**
* Puts data into the cache.
*
* @param string $id the cache id
* @param mixed $data the cache entry/data
* @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this
* cache entry (0 => infinite lifeTime).
*
* @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise
*/
protected function doSave($id, $data, $lifeTime = 0): bool
{
$item = $this->getItemWithPortableKeyFallback($id);
$item->set($data);
if (0 !== $lifeTime) {
$item->expiresAfter($lifeTime);
}
return $this->cachePool->save($item);
}
/**
* Deletes a cache entry.
*
* @param string $id the cache id
*
* @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise
*/
protected function doDelete($id): bool
{
return $this->withPortableKeyFallback(
$id,
fn (string $key): bool => $this->cachePool->deleteItem($key)
);
}
/**
* Flushes all cache entries.
*
* @return bool TRUE if the cache entries were successfully flushed, FALSE otherwise
*/
protected function doFlush(): bool
{
return $this->cachePool->clear();
}
/**
* Retrieves cached information from the data store.
*
* @since 2.2
*
* @return array<string, mixed>|null an associative array with server statistics if available
*/
protected function doGetStats(): ?array
{
return null;
}
/**
* We need to make sure we do not use any characters not supported.
*/
private function normalizeKey(string $key): string
{
if (preg_match('|[\{\}\(\)/\\\@\:]|', $key)) {
return preg_replace('|[\{\}\(\)/\\\@\:]|', '_', $key) ?? $key;
}
return $key;
}
private function getItemWithPortableKeyFallback(string $key): CacheItemInterface
{
return $this->withPortableKeyFallback(
$key,
fn (string $normalizedKey): CacheItemInterface => $this->cachePool->getItem($normalizedKey)
);
}
/**
* @template T
*
* @param callable(string): T $operation
*
* @return T
*/
private function withPortableKeyFallback(string $key, callable $operation): mixed
{
try {
return $operation($this->normalizeKey($key));
} catch (InvalidArgumentException) {
return $operation(hash('sha256', $key));
}
}
}