Skip to content

Commit

Permalink
Merge pull request #97 from gam6itko/cache_clear
Browse files Browse the repository at this point in the history
add ClearableCacheInterface
  • Loading branch information
goetas committed Mar 7, 2021
2 parents f410b75 + 8a08ea2 commit b5c5254
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/Cache/ClearableCacheInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Metadata\Cache;

/**
* @author Alexander Strizhak <gam6itko@gmail.com>
*/
interface ClearableCacheInterface
{
/**
* Clear all classes metadata from the cache.
*/
public function clear(): bool;
}
11 changes: 10 additions & 1 deletion src/Cache/DoctrineCacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @author Henrik Bjornskov <henrik@bjrnskov.dk>
*/
class DoctrineCacheAdapter implements CacheInterface
class DoctrineCacheAdapter implements CacheInterface, ClearableCacheInterface
{
/**
* @var string
Expand Down Expand Up @@ -43,4 +43,13 @@ public function evict(string $class): void
{
$this->cache->delete($this->prefix . $class);
}

public function clear(): bool
{
if (method_exists($this->cache, 'deleteAll')) { // or $this->cache instanceof ClearableCache
return call_user_func([$this->cache, 'deleteAll']);
}

return false;
}
}
16 changes: 14 additions & 2 deletions src/Cache/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Metadata\ClassMetadata;

class FileCache implements CacheInterface
class FileCache implements CacheInterface, ClearableCacheInterface
{
/**
* @var string
Expand Down Expand Up @@ -34,7 +34,6 @@ public function load(string $class): ?ClassMetadata
if ($metadata instanceof ClassMetadata) {
return $metadata;
}

// if the file does not return anything, the return value is integer `1`.
} catch (\ParseError $e) {
// ignore corrupted cache
Expand Down Expand Up @@ -106,6 +105,19 @@ public function evict(string $class): void
}
}

public function clear(): bool
{
$result = true;
$files = glob($this->dir . '/*.cache.php');
foreach ($files as $file) {
if (is_file($file)) {
$result = $result && @unlink($file);
}
}

return $result;
}

/**
* This function computes the cache file path.
*
Expand Down
7 changes: 6 additions & 1 deletion src/Cache/PsrCacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Metadata\ClassMetadata;
use Psr\Cache\CacheItemPoolInterface;

class PsrCacheAdapter implements CacheInterface
class PsrCacheAdapter implements CacheInterface, ClearableCacheInterface
{
/**
* @var string
Expand Down Expand Up @@ -53,6 +53,11 @@ public function evict(string $class): void
$this->pool->deleteItem($this->sanitizeCacheKey($this->prefix . $class));
}

public function clear(): bool
{
return $this->pool->clear();
}

/**
* If anonymous class is to be cached, it contains invalid path characters that need to be removed/replaced
* Example of anonymous class name: class@anonymous\x00/app/src/Controller/DefaultController.php0x7f82a7e026ec
Expand Down
17 changes: 17 additions & 0 deletions tests/Cache/DoctrineCacheAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Doctrine\Common\Cache\ArrayCache;
use Metadata\Cache\DoctrineCacheAdapter;
use Metadata\ClassMetadata;
use Metadata\Tests\Driver\Fixture\A\A;
use Metadata\Tests\Driver\Fixture\B\B;
use Metadata\Tests\Fixtures\TestObject;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -50,4 +52,19 @@ public function classNameProvider()
],
];
}

public function testClear(): void
{
$cache = new ArrayCache();
$cacheAdapter = new DoctrineCacheAdapter('', $cache);

$cacheAdapter->put(new ClassMetadata(A::class));
$cacheAdapter->put(new ClassMetadata(B::class));
self::assertTrue($cache->contains(A::class));
self::assertTrue($cache->contains(B::class));

self::assertTrue($cacheAdapter->clear());
self::assertFalse($cache->contains(A::class));
self::assertFalse($cache->contains(B::class));
}
}
17 changes: 16 additions & 1 deletion tests/Cache/FileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Metadata\Cache\FileCache;
use Metadata\ClassMetadata;
use Metadata\Tests\Driver\Fixture\A\A;
use Metadata\Tests\Driver\Fixture\B\B;
use Metadata\Tests\Fixtures\TestObject;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -52,7 +54,7 @@ public function testLoadEvictPutClassMetadataFromInCache(string $className)
public function classNameProvider()
{
return [
'TestObject' => [TestObject::class],
'TestObject' => [TestObject::class],
'anonymous class' => [
get_class(new class {
}),
Expand Down Expand Up @@ -106,4 +108,17 @@ public function testThrowExceptionIfCacheFilePathNotWritable()

$cache->put($metadata = new ClassMetadata('Metadata\Tests\Fixtures\TestParent'));
}

public function testClear(): void
{
self::assertCount(0, glob($this->dir . '/*'));
$cache = new FileCache($this->dir);

$cache->put(new ClassMetadata(A::class));
$cache->put(new ClassMetadata(B::class));
self::assertCount(2, glob($this->dir . '/*'));

self::assertTrue($cache->clear());
self::assertCount(0, glob($this->dir . '/*'));
}
}
17 changes: 17 additions & 0 deletions tests/Cache/PsrCacheAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Metadata\Cache\PsrCacheAdapter;
use Metadata\ClassMetadata;
use Metadata\Tests\Driver\Fixture\A\A;
use Metadata\Tests\Driver\Fixture\B\B;
use Metadata\Tests\Fixtures\TestObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
Expand Down Expand Up @@ -51,4 +53,19 @@ public function classNameProvider()
],
];
}

public function testClear(): void
{
$pool = new ArrayAdapter();
$cacheAdapter = new PsrCacheAdapter('metadata-test', $pool);

$cacheAdapter->put(new ClassMetadata(A::class));
$cacheAdapter->put(new ClassMetadata(B::class));
self::assertTrue($pool->hasItem('metadata-testMetadata-Tests-Driver-Fixture-A-A'));
self::assertTrue($pool->hasItem('metadata-testMetadata-Tests-Driver-Fixture-B-B'));

self::assertTrue($cacheAdapter->clear());
self::assertFalse($pool->hasItem('metadata-testMetadata-Tests-Driver-Fixture-A-A'));
self::assertFalse($pool->hasItem('metadata-testMetadata-Tests-Driver-Fixture-B-B'));
}
}

0 comments on commit b5c5254

Please sign in to comment.