diff --git a/README.md b/README.md index 08fe64a..f2d32ba 100644 --- a/README.md +++ b/README.md @@ -135,8 +135,8 @@ Each pool comes with a set of default options which can be individually overridd |:----------------|:----------:|:----------:|:------------| | **active** | `boolean` | `TRUE` | Whether to enable or disable the cache service. | **prefix** | `string` | `charcoal` | Name of the main Stash pool. -| **types** | `string[]` | `memory` | List of cache drivers to choose from for the main Stash pool. -| **default_ttl** | `integer` | 1 week | Default time-to-live (in seconds) for a cached item. Currently, only used by the APC driver (`cache/drivers.apc`). +| **types** | `string[]` | `memory` | List of cache drivers to choose from for the main Stash pool. Defaults to "memory". +| **default_ttl** | `integer` | 1 week | Default time-to-live (in seconds) for a cached item. Currently, only used by the APC driver (`cache/drivers.apc`). diff --git a/src/Charcoal/Cache/CacheConfig.php b/src/Charcoal/Cache/CacheConfig.php index 6316e7a..3d761a9 100644 --- a/src/Charcoal/Cache/CacheConfig.php +++ b/src/Charcoal/Cache/CacheConfig.php @@ -14,6 +14,13 @@ class CacheConfig extends AbstractConfig { const DEFAULT_NAMESPACE = 'charcoal'; + /** + * Default cache type and fallback for user preference. + */ + const DEFAULT_TYPES = [ + 'memory' => true + ]; + /** * Human-readable intervals in seconds. */ @@ -39,7 +46,7 @@ class CacheConfig extends AbstractConfig * * @var array */ - private $types = [ 'memory' ]; + private $types; /** * Time-to-live in seconds. @@ -64,7 +71,7 @@ public function defaults() { return [ 'active' => true, - 'types' => [ 'memory' ], + 'types' => $this->defaultTypes(), 'default_ttl' => self::WEEK_IN_SECONDS, 'prefix' => self::DEFAULT_NAMESPACE ]; @@ -137,18 +144,33 @@ public function addType($type) ); } - $this->types[] = $type; + $this->types[$type] = true; return $this; } /** * Retrieve the cache type(s) to use. * + * Note: + * 1. The default cache type is always appended. + * 2. Duplicate types are removed. + * * @return array */ public function types() { - return $this->types; + $types = $this->types + self::DEFAULT_TYPES; + return array_keys($types); + } + + /** + * Retrieve the default cache types. + * + * @return string[] + */ + public function defaultTypes() + { + return array_keys(self::DEFAULT_TYPES); } /** diff --git a/src/Charcoal/Cache/ServiceProvider/CacheServiceProvider.php b/src/Charcoal/Cache/ServiceProvider/CacheServiceProvider.php index 60c8570..0a79065 100644 --- a/src/Charcoal/Cache/ServiceProvider/CacheServiceProvider.php +++ b/src/Charcoal/Cache/ServiceProvider/CacheServiceProvider.php @@ -232,7 +232,12 @@ public function registerService(Container $container) $container['cache'] = function (Container $container) { $cacheBuilder = $container['cache/builder']; $cacheConfig = $container['cache/config']; - $cacheDrivers = array_unique($cacheConfig['types'] + [ 'memory' ]); + + if ($cacheConfig['active'] === true) { + $cacheDrivers = $cacheConfig['types']; + } else { + $cacheDrivers = $cacheConfig['default_types']; + } return $cacheBuilder($cacheDrivers); }; diff --git a/tests/Charcoal/Cache/CacheConfigTest.php b/tests/Charcoal/Cache/CacheConfigTest.php index 9627c1f..b43e6d9 100644 --- a/tests/Charcoal/Cache/CacheConfigTest.php +++ b/tests/Charcoal/Cache/CacheConfigTest.php @@ -43,6 +43,7 @@ public function configFactory(array $args = []) * @covers ::defaults * @covers ::active * @covers ::types + * @covers ::defaultTypes * @covers ::defaultTtl * @covers ::prefix */ @@ -60,6 +61,7 @@ public function testDefaults() $this->assertArrayHasKey('types', $defaults); $this->assertEquals($defaults['types'], $this->cfg->types()); + $this->assertEquals($defaults['types'], $this->cfg->defaultTypes()); $this->assertArrayHasKey('default_ttl', $defaults); $this->assertEquals($defaults['default_ttl'], $this->cfg->defaultTtl()); @@ -94,9 +96,18 @@ public function testReplaceDrivers() // Mutated State $types = $this->cfg->types(); - $this->assertNotContains('memory', $types); - $this->assertContains('memcache', $types); - $this->assertContains('noop', $types); + $this->assertEquals([ 'memcache', 'noop', 'memory' ], $types); + } + + /** + * @covers ::types + */ + public function testUniqueDrivers() + { + $this->cfg->setTypes([ 'memcache', 'memory', 'file', 'memcache' ]); + + $types = $this->cfg->types(); + $this->assertEquals([ 'memcache', 'memory', 'file' ], $types); } /** diff --git a/tests/Charcoal/Cache/ServiceProvider/CacheServiceProviderTest.php b/tests/Charcoal/Cache/ServiceProvider/CacheServiceProviderTest.php index 613bebe..925cab7 100644 --- a/tests/Charcoal/Cache/ServiceProvider/CacheServiceProviderTest.php +++ b/tests/Charcoal/Cache/ServiceProvider/CacheServiceProviderTest.php @@ -205,27 +205,18 @@ public function provideConfigsForMainDriver() $driverClassNames = DriverList::getAvailableDrivers(); return [ - 'Cache: Disabled' => [ - $driverClassNames['Ephemeral'], - [ - 'active' => false - ] - ], - 'Cache: Default Type' => [ - $driverClassNames['Ephemeral'], + 'Cache: Enabled' => [ + $driverClassNames['BlackHole'], [ + 'active' => true, + 'types' => [ 'noop' ], ] ], - 'Cache: Fallback Type' => [ + 'Cache: Disabled' => [ $driverClassNames['Ephemeral'], [ - 'types' => [] - ] - ], - 'Cache: Chosen Type' => [ - $driverClassNames['BlackHole'], - [ - 'types' => [ 'noop' ] + 'active' => false, + 'types' => [ 'noop' ], ] ], ];