Skip to content

Commit

Permalink
Merge pull request #1142: add CacheStorageRegistryInterface
Browse files Browse the repository at this point in the history
CacheManager ability to set custom cache storage.
  • Loading branch information
roxblnfk authored Nov 26, 2024
2 parents f2b3268 + 8806380 commit 8e89922
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Cache/src/Bootloader/CacheBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Spiral\Boot\EnvironmentInterface;
use Spiral\Cache\CacheManager;
use Spiral\Cache\CacheStorageProviderInterface;
use Spiral\Cache\CacheStorageRegistryInterface;
use Spiral\Cache\Config\CacheConfig;
use Spiral\Cache\Core\CacheInjector;
use Spiral\Cache\Storage\ArrayStorage;
Expand All @@ -23,6 +24,7 @@
final class CacheBootloader extends Bootloader
{
protected const SINGLETONS = [
CacheStorageRegistryInterface::class => CacheManager::class,
CacheStorageProviderInterface::class => CacheManager::class,
CacheManager::class => [self::class, 'initCacheManager'],
];
Expand Down
14 changes: 12 additions & 2 deletions src/Cache/src/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
use Spiral\Core\FactoryInterface;

#[Singleton]
class CacheManager implements CacheStorageProviderInterface
class CacheManager implements CacheStorageProviderInterface, CacheStorageRegistryInterface
{
/** @var CacheInterface[] */
/** @var array<non-empty-string, CacheInterface> */
private array $storages = [];

public function __construct(
Expand Down Expand Up @@ -43,6 +43,16 @@ public function storage(?string $name = null): CacheInterface
return new CacheRepository($this->storages[$storage], $this->dispatcher, $prefix);
}

public function register(string $name, CacheInterface $cache): void
{
$this->storages[$name] = $cache;
}

public function getCacheStorages(): array
{
return $this->storages;
}

private function resolve(?string $name): CacheInterface
{
$config = $this->config->getStorageConfig($name);
Expand Down
23 changes: 23 additions & 0 deletions src/Cache/src/CacheStorageRegistryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Spiral\Cache;

use Psr\SimpleCache\CacheInterface;

/**
* Use this interface to register cache storages at the bootloading stage.
*/
interface CacheStorageRegistryInterface
{
/**
* @param non-empty-string $name
*/
public function register(string $name, CacheInterface $cache): void;

/**
* @return array<non-empty-string, CacheInterface>
*/
public function getCacheStorages(): array;
}
31 changes: 31 additions & 0 deletions src/Cache/tests/CacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Psr\SimpleCache\CacheInterface;
use Spiral\Cache\CacheManager;
use Spiral\Cache\Config\CacheConfig;
use Spiral\Cache\Storage\ArrayStorage;
use Spiral\Core\FactoryInterface;

final class CacheManagerTest extends TestCase
Expand Down Expand Up @@ -218,4 +219,34 @@ public static function prefixesDataProvider(): \Traversable
yield ['order-data', null];
yield ['delivery-data', null];
}

public function testRegister(): void
{
$uniq = \uniqid();
$cache = new ArrayStorage();
$cache->set('uniq', $uniq);
$name = 'brandNewCache';
$this->assertArrayNotHasKey($name, $this->manager->getCacheStorages());

$this->manager->register($name, $cache);
$this->assertArrayHasKey($name, $this->manager->getCacheStorages());

$repo = $this->manager->storage($name);
$this->assertSame($uniq, $repo->get('uniq'));
}

public function testMany(): void
{
$cache1 = new ArrayStorage();
$name1 = 'brandNewCache';
$cache2 = new ArrayStorage();
$name2 = 'brandNewCache2';

$this->manager->register($name1, $cache1);
$this->manager->register($name2, $cache2);
$this->assertEquals([
$name1 => $cache1,
$name2 => $cache2,
], $this->manager->getCacheStorages());
}
}
12 changes: 9 additions & 3 deletions tests/Framework/Bootloader/Cache/CacheBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Spiral\Cache\CacheManager;
use Spiral\Cache\CacheRepository;
use Spiral\Cache\CacheStorageProviderInterface;
use Spiral\Cache\CacheStorageRegistryInterface;
use Spiral\Cache\Config\CacheConfig;
use Spiral\Cache\Storage\ArrayStorage;
use Spiral\Cache\Storage\FileStorage;
Expand All @@ -18,17 +19,22 @@

final class CacheBootloaderTest extends BaseTestCase
{
public function testBindings()
public function testBindings(): void
{
$this->assertContainerInstantiable(CacheInterface::class, CacheRepository::class);
$this->assertContainerBoundAsSingleton(CacheStorageProviderInterface::class, CacheManager::class);
$this->assertContainerBoundAsSingleton(CacheStorageRegistryInterface::class, CacheManager::class);

$repository = $this->getContainer()->get(CacheInterface::class);

$this->assertInstanceOf(ArrayStorage::class, $repository->getStorage());

$provider = $this->getContainer()->get(CacheStorageProviderInterface::class);
$this->assertInstanceOf(CacheManager::class, $provider);
$registry = $this->getContainer()->get(CacheStorageRegistryInterface::class);
$this->assertInstanceOf(CacheManager::class, $registry);
}

public function testGetsStorageByAlias()
public function testGetsStorageByAlias(): void
{
$manager = $this->getContainer()->get(CacheStorageProviderInterface::class);
$repository = $manager->storage('user-data');
Expand Down

0 comments on commit 8e89922

Please sign in to comment.