Skip to content

Commit

Permalink
Don't hit the reader if cache is fresh (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored May 16, 2021
1 parent 426dcad commit e6e7b7d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Doctrine/Common/Annotations/PsrCachedReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private function fetchFromCache(
$cacheKey = rawurlencode($cacheKey);

$item = $this->cache->getItem($cacheKey);
if (! $item->isHit() || ($this->debug && ! $this->refresh($cacheKey, $class))) {
if (($this->debug && ! $this->refresh($cacheKey, $class)) || ! $item->isHit()) {
$this->cache->save($item->set($this->delegate->{$method}($reflector)));
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Doctrine/Tests/Common/Annotations/PsrCachedReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\Common\Annotations\Reader;
use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\Route;
use Doctrine\Tests\Common\Annotations\Fixtures\ClassThatUsesTraitThatUsesAnotherTraitWithMethods;
use Doctrine\Tests\Common\Annotations\Fixtures\ClassWithClassAnnotationOnly;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use ReflectionClass;
Expand Down Expand Up @@ -202,6 +203,23 @@ public function testAvoidCallingFilemtimeTooMuch(): void
$this->assertEquals([$route2], $reader->getMethodAnnotations(new ReflectionMethod($className, 'method2')));
}

public function testReaderIsNotHitIfCacheIsFresh(): void
{
$cache = new ArrayAdapter();

$readAnnotations = (new PsrCachedReader(new AnnotationReader(), $cache, true))
->getClassAnnotations(new ReflectionClass(ClassWithClassAnnotationOnly::class));

$throwingReader = $this->createMock(Reader::class);
$throwingReader->expects(self::never())->method(self::anything());

self::assertEquals(
$readAnnotations,
(new PsrCachedReader($throwingReader, $cache, true))
->getClassAnnotations(new ReflectionClass(ClassWithClassAnnotationOnly::class))
);
}

protected function doTestCacheStale(string $className, int $lastCacheModification): PsrCachedReader
{
$cacheKey = rawurlencode($className);
Expand Down

0 comments on commit e6e7b7d

Please sign in to comment.