Skip to content

Commit 27ff114

Browse files
[Cache] Don't enable cache tracing unless the profiler is enabled
1 parent 904df78 commit 27ff114

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/Symfony/Component/Cache/Adapter/TraceableAdapter.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class TraceableAdapter implements AdapterInterface, CacheInterface, NamespacedPo
3434

3535
public function __construct(
3636
protected AdapterInterface $pool,
37+
public bool $enabled = true,
3738
) {
3839
}
3940

@@ -45,6 +46,9 @@ public function get(string $key, callable $callback, ?float $beta = null, ?array
4546
if (!$this->pool instanceof CacheInterface) {
4647
throw new BadMethodCallException(\sprintf('Cannot call "%s::get()": this class doesn\'t implement "%s".', get_debug_type($this->pool), CacheInterface::class));
4748
}
49+
if (!$this->enabled) {
50+
return $this->pool->get($key, $callback, $beta, $metadata);
51+
}
4852

4953
$isHit = true;
5054
$callback = function (CacheItem $item, bool &$save) use ($callback, &$isHit) {
@@ -71,6 +75,9 @@ public function get(string $key, callable $callback, ?float $beta = null, ?array
7175

7276
public function getItem(mixed $key): CacheItem
7377
{
78+
if (!$this->enabled) {
79+
return $this->pool->getItem($key);
80+
}
7481
$event = $this->start(__FUNCTION__);
7582
try {
7683
$item = $this->pool->getItem($key);
@@ -88,6 +95,9 @@ public function getItem(mixed $key): CacheItem
8895

8996
public function hasItem(mixed $key): bool
9097
{
98+
if (!$this->enabled) {
99+
return $this->pool->hasItem($key);
100+
}
91101
$event = $this->start(__FUNCTION__);
92102
try {
93103
return $event->result[$key] = $this->pool->hasItem($key);
@@ -98,6 +108,9 @@ public function hasItem(mixed $key): bool
98108

99109
public function deleteItem(mixed $key): bool
100110
{
111+
if (!$this->enabled) {
112+
return $this->pool->deleteItem($key);
113+
}
101114
$event = $this->start(__FUNCTION__);
102115
try {
103116
return $event->result[$key] = $this->pool->deleteItem($key);
@@ -108,6 +121,9 @@ public function deleteItem(mixed $key): bool
108121

109122
public function save(CacheItemInterface $item): bool
110123
{
124+
if (!$this->enabled) {
125+
return $this->pool->save($item);
126+
}
111127
$event = $this->start(__FUNCTION__);
112128
try {
113129
return $event->result[$item->getKey()] = $this->pool->save($item);
@@ -118,6 +134,9 @@ public function save(CacheItemInterface $item): bool
118134

119135
public function saveDeferred(CacheItemInterface $item): bool
120136
{
137+
if (!$this->enabled) {
138+
return $this->pool->saveDeferred($item);
139+
}
121140
$event = $this->start(__FUNCTION__);
122141
try {
123142
return $event->result[$item->getKey()] = $this->pool->saveDeferred($item);
@@ -128,6 +147,9 @@ public function saveDeferred(CacheItemInterface $item): bool
128147

129148
public function getItems(array $keys = []): iterable
130149
{
150+
if (!$this->enabled) {
151+
return $this->pool->getItems($keys);
152+
}
131153
$event = $this->start(__FUNCTION__);
132154
try {
133155
$result = $this->pool->getItems($keys);
@@ -151,6 +173,9 @@ public function getItems(array $keys = []): iterable
151173

152174
public function clear(string $prefix = ''): bool
153175
{
176+
if (!$this->enabled) {
177+
return $this->pool->clear($prefix);
178+
}
154179
$event = $this->start(__FUNCTION__);
155180
try {
156181
if ($this->pool instanceof AdapterInterface) {
@@ -165,6 +190,9 @@ public function clear(string $prefix = ''): bool
165190

166191
public function deleteItems(array $keys): bool
167192
{
193+
if (!$this->enabled) {
194+
return $this->pool->deleteItems($keys);
195+
}
168196
$event = $this->start(__FUNCTION__);
169197
$event->result['keys'] = $keys;
170198
try {
@@ -176,6 +204,9 @@ public function deleteItems(array $keys): bool
176204

177205
public function commit(): bool
178206
{
207+
if (!$this->enabled) {
208+
return $this->pool->commit();
209+
}
179210
$event = $this->start(__FUNCTION__);
180211
try {
181212
return $event->result = $this->pool->commit();
@@ -189,6 +220,9 @@ public function prune(): bool
189220
if (!$this->pool instanceof PruneableInterface) {
190221
return false;
191222
}
223+
if (!$this->enabled) {
224+
return $this->pool->prune();
225+
}
192226
$event = $this->start(__FUNCTION__);
193227
try {
194228
return $event->result = $this->pool->prune();
@@ -208,6 +242,9 @@ public function reset(): void
208242

209243
public function delete(string $key): bool
210244
{
245+
if (!$this->enabled) {
246+
return $this->pool->deleteItem($key);
247+
}
211248
$event = $this->start(__FUNCTION__);
212249
try {
213250
return $event->result[$key] = $this->pool->deleteItem($key);

src/Symfony/Component/Cache/DataCollector/CacheDataCollector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class CacheDataCollector extends DataCollector implements LateDataCollectorInter
3434
public function addInstance(string $name, TraceableAdapter $instance): void
3535
{
3636
$this->instances[$name] = $instance;
37+
$instance->enabled = true;
3738
}
3839

3940
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void

src/Symfony/Component/Cache/DependencyInjection/CacheCollectorPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private function addToCollector(string $id, string $name, ContainerBuilder $cont
5252
if (!$definition->isPublic() || !$definition->isPrivate()) {
5353
$recorder->setPublic($definition->isPublic());
5454
}
55-
$recorder->setArguments([new Reference($innerId = $id.'.recorder_inner')]);
55+
$recorder->setArguments([new Reference($innerId = $id.'.recorder_inner'), false]);
5656

5757
foreach ($definition->getMethodCalls() as [$method, $args]) {
5858
if ('setCallbackWrapper' !== $method || !$args[0] instanceof Definition || !($args[0]->getArguments()[2] ?? null) instanceof Definition) {

0 commit comments

Comments
 (0)