8000 bug #44669 [Cache] disable lock on CLI (nicolas-grekas) · symfony/cache@630d333 · GitHub
[go: up one dir, main page]

Skip to content

Commit 630d333

Browse files
bug #44669 [Cache] disable lock on CLI (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [Cache] disable lock on CLI | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | | Tickets | Fix #41130 | License | MIT | Doc PR | - I thought using semaphores would fix the linked issue but I'm proposing to revert them in #44667 Instead, I think we should disable the lock on CLI processes. The purpose of having locks enabled by default is to prevent cache stamped at deploy time. Disabling the lock on CLI still provides this protection on eg FPM. For ppl that really want a lock between eg crons and FPM, the lock component should be used instead. Commits ------- b968514286 [Cache] disable lock on CLI
2 parents 89130c1 + 5dd8aa5 commit 630d333

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

Tests/Adapter/TagAwareAdapterTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414
use PHPUnit\Framework\MockObject\MockObject;
1515
use Psr\Cache\CacheItemInterface;
1616
use Psr\Cache\CacheItemPoolInterface;
17-
use Psr\Log\LoggerInterface;
1817
use Symfony\Component\Cache\Adapter\AdapterInterface;
1918
use Symfony\Component\Cache\Adapter\ArrayAdapter;
2019
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
2120
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
22-
use Symfony\Component\Cache\LockRegistry;
2321
use Symfony\Component\Cache\Tests\Fixtures\PrunableAdapter;
2422

2523
/**
@@ -198,24 +196,6 @@ public function testGetItemReturnsCacheMissWhenPoolDoesNotHaveItemAndOnlyHasTags
198196
$this->assertFalse($item->isHit());
199197
}
200198

201-
public function testLog()
202-
{
203-
$lockFiles = LockRegistry::setFiles([__FILE__]);
204-
205-
$logger = $this->createMock(LoggerInterface::class);
206-
$logger
207-
->expects($this->atLeastOnce())
208-
->method($this->anything());
209-
210-
$cache = new TagAwareAdapter(new ArrayAdapter());
211-
$cache->setLogger($logger);
212-
213-
// Computing will produce at least one log
214-
$cache->get('foo', static function (): string { return 'ccc'; });
215-
216-
LockRegistry::setFiles($lockFiles);
217-
}
218-
219199
/**
220200
* @return MockObject&PruneableCacheInterface
221201
*/

Traits/ContractsTrait.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ trait ContractsTrait
3131
doGet as private contractsGet;
3232
}
3333

34-
private $callbackWrapper = [LockRegistry::class, 'compute'];
34+
private $callbackWrapper;
3535
private $computing = [];
3636

3737
/**
@@ -41,8 +41,16 @@ trait ContractsTrait
4141
*/
4242
public function setCallbackWrapper(?callable $callbackWrapper): callable
4343
{
44+
if (!isset($this->callbackWrapper)) {
45+
$this->callbackWrapper = \Closure::fromCallable([LockRegistry::class, 'compute']);
46+
47+
if (\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
48+
$this->setCallbackWrapper(null);
49+
}
50+
}
51+
4452
$previousWrapper = $this->callbackWrapper;
45-
$this->callbackWrapper = $callbackWrapper ?? function (callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata, ?LoggerInterface $logger) {
53+
$this->callbackWrapper = $callbackWrapper ?? static function (callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata, ?LoggerInterface $logger) {
4654
return $callback($item, $save);
4755
};
4856

@@ -82,6 +90,10 @@ static function (CacheItem $item, float $startTime, ?array &$metadata) {
8290
$this->computing[$key] = $key;
8391
$startTime = microtime(true);
8492

93+
if (!isset($this->callbackWrapper)) {
94+
$this->setCallbackWrapper($this->setCallbackWrapper(null));
95+
}
96+
8597
try {
8698
$value = ($this->callbackWrapper)($callback, $item, $save, $pool, function (CacheItem $item) use ($setMetadata, $startTime, &$metadata) {
8799
$setMetadata($item, $startTime, $metadata);

0 commit comments

Comments
 (0)
0