Skip to content

Commit

Permalink
Refactor "cache/driver" (Take 2)
Browse files Browse the repository at this point in the history
Fixed:
- Accidentally ignored "cache/config.active" when building the main cache pool
- Merge of default cache type with "cache/config.types"

Added:
- Public method `CacheConfig::defaultTypes()`
- Protected constant `CacheConfig::DEFAULT_TYPES`

Changed:
- `CacheConfig::types()` to merge default cache type with current values
- `CacheConfigTest` to test new `::defaultTypes()` method and new behaviour on `::types()`
- `CacheServiceProviderTest::provideConfigsForMainDriver()` to simplify test cases
- README to mention default cache type from "cache/config" settings

Amends: 01736ae
  • Loading branch information
mcaskill committed May 11, 2018
1 parent 1ca0833 commit d17bc6d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).



Expand Down
30 changes: 26 additions & 4 deletions src/Charcoal/Cache/CacheConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -39,7 +46,7 @@ class CacheConfig extends AbstractConfig
*
* @var array
*/
private $types = [ 'memory' ];
private $types;

/**
* Time-to-live in seconds.
Expand All @@ -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
];
Expand Down Expand Up @@ -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);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Charcoal/Cache/ServiceProvider/CacheServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
17 changes: 14 additions & 3 deletions tests/Charcoal/Cache/CacheConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function configFactory(array $args = [])
* @covers ::defaults
* @covers ::active
* @covers ::types
* @covers ::defaultTypes
* @covers ::defaultTtl
* @covers ::prefix
*/
Expand All @@ -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());
Expand Down Expand Up @@ -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);
}

/**
Expand Down
23 changes: 7 additions & 16 deletions tests/Charcoal/Cache/ServiceProvider/CacheServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ],
]
],
];
Expand Down

0 comments on commit d17bc6d

Please sign in to comment.