Skip to content

Commit a674c1f

Browse files
committed
added factory methods disableCaching and enableCaching to disable or enable caching on the fly
1 parent 86f8f7e commit a674c1f

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

src/Domino/CacheStore/Factory.php

+32-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Domino\CacheStore;
1111

1212
use Domino\CacheStore\Exception\StorageException;
13+
use Domino\CacheStore\Storage\StorageInterface;
1314

1415
/**
1516
* Domino Cache Store Factory
@@ -30,6 +31,9 @@ class Factory
3031
*/
3132
private static $connectionMap = array();
3233

34+
/** @var bool */
35+
private static $cachingIsDisabled = false;
36+
3337
/**
3438
* Registered storage
3539
* @var array
@@ -38,6 +42,7 @@ class Factory
3842
'apc' => 'Domino\CacheStore\Storage\Apc',
3943
'memcached' => 'Domino\CacheStore\Storage\Memcached',
4044
'memcache' => 'Domino\CacheStore\Storage\Memcache',
45+
'nocache' => 'Domino\CacheStore\Storage\NoCache',
4146
'redis' => 'Domino\CacheStore\Storage\Redis',
4247
);
4348

@@ -103,17 +108,41 @@ public static function clearConnectionCache()
103108
self::$connectionMap = array();
104109
}
105110

111+
/**
112+
* Changes the behavior of the method factory
113+
* If you trigger this method, you will get back a NoCache storage
114+
*/
115+
public static function disableCaching()
116+
{
117+
self::$cachingIsDisabled = true;
118+
}
119+
120+
/**
121+
* Resets behavior of the method factory
122+
* If you trigger this method, you will get back the storage you want to
123+
*
124+
* HINT:
125+
* If you enable caching, I recommend to clear the cache right afterwards.
126+
*/
127+
public static function enableCaching()
128+
{
129+
self::$cachingIsDisabled = false;
130+
}
131+
106132
/**
107133
* Instantiate a cache storage
108134
* @param string $storage_type cache store storage type (eg. 'apc', 'memcached')
109-
* @return Storage cache store storage instance
135+
* @return StorageInterface cache store storage instance
110136
* @throws StorageException when $storage_type is not registered
111137
*/
112138
public static function factory($storage_type)
113139
{
114140
if (!array_key_exists($storage_type, self::$storage)) {
115141
throw new StorageException(sprintf('Storage class not set for type %s', $storage_type));
116142
}
143+
if (self::$cachingIsDisabled) {
144+
$storage_type = 'nocache';
145+
}
117146
if (!isset(self::$connectionMap[$storage_type])) {
118147
self::$connectionMap[$storage_type] = new self::$storage[$storage_type](self::getOption($storage_type));
119148
}
@@ -124,8 +153,8 @@ public static function factory($storage_type)
124153
/**
125154
* Register a cache storage
126155
*
127-
* @param $storage_type cache store storage type (eg. 'apc', 'memcached', 'my_apc')
128-
* @param $storage_class class name which must implement Domino\CacheStore\Storage\StorageInterface
156+
* @param string $storage_type cache store storage type (eg. 'apc', 'memcached', 'my_apc')
157+
* @param string $storage_class class name which must implement Domino\CacheStore\Storage\StorageInterface
129158
* @throws StorageException when $storage_class not implements Domino\CacheStore\Storage\StorageInterface
130159
*/
131160
public static function registerStorage($storage_type, $storage_class)

tests/Domino/CacheStore/Tests/FactoryTest.php

+41-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class FactoryTest extends \PHPUnit_Framework_TestCase
2222
public function setUp()
2323
{
2424
CacheStore\Factory::clearOptions();
25+
CacheStore\Factory::enableCaching();
2526
}
2627

2728
public function testOptions()
@@ -36,10 +37,10 @@ public function testOptions()
3637

3738
public function testOption()
3839
{
39-
$set_apc_option = array("storage" => 'apc', 'default_ttl' => 10);
40+
$set_apc_option = array('storage' => 'apc', 'default_ttl' => 10);
4041
CacheStore\Factory::setOption($set_apc_option);
4142

42-
$set_memcached_option = array("storage" => 'memcached', 'option1' => 20);
43+
$set_memcached_option = array('storage' => 'memcached', 'option1' => 20);
4344
CacheStore\Factory::setOption($set_memcached_option);
4445

4546
$get_apc_option = CacheStore\Factory::getOption('apc');
@@ -60,6 +61,10 @@ public function testFactoryApc()
6061

6162
public function testFactoryMemcached()
6263
{
64+
if (!extension_loaded('memcached')) {
65+
$this->markTestSkipped('Memcached extension is not loaded');
66+
}
67+
6368
$memcached_option = array('storage' => 'memcached', 'default_ttl' => 10, 'prefix' => '_md', 'servers' => array());
6469
CacheStore\Factory::setOption($memcached_option);
6570

@@ -69,6 +74,10 @@ public function testFactoryMemcached()
6974

7075
public function testFactoryMemcache()
7176
{
77+
if (!extension_loaded('memcache')) {
78+
$this->markTestSkipped('Memcache extension is not loaded');
79+
}
80+
7281
$memcached_option = array('storage' => 'memcache', 'default_ttl' => 10, 'prefix' => '_md', 'servers' => array());
7382
CacheStore\Factory::setOption($memcached_option);
7483

@@ -93,7 +102,7 @@ public function testRegisterStorage()
93102
// register custom storage
94103
$customStorage = $this->getMock('Domino\CacheStore\Storage\StorageInterface');
95104
CacheStore\Factory::registerStorage('custom', get_class($customStorage));
96-
$cacheStore = CacheStore\Factory::factory('custom', get_class($customStorage));
105+
$cacheStore = CacheStore\Factory::factory('custom');
97106
$this->assertInstanceOf(get_class($customStorage), $cacheStore);
98107

99108
// try to register custom storage with bad interface
@@ -124,4 +133,33 @@ public function testClearConnectionCache()
124133

125134
$this->assertNotSame($store1, $store2);
126135
}
136+
137+
public function testDisableCache()
138+
{
139+
CacheStore\Factory::disableCaching();
140+
$storage = CacheStore\Factory::factory('apc');
141+
142+
$this->assertInstanceOf('\Domino\CacheStore\Storage\NoCache', $storage);
143+
}
144+
145+
public function testDisableAndEnableCache()
146+
{
147+
//begin of setup
148+
$name = 'custom';
149+
/** @var CacheStore\Storage\StorageInterface $storageMock */
150+
$storageMock = $this->getMock('Domino\CacheStore\Storage\StorageInterface');
151+
CacheStore\Factory::registerStorage($name, get_class($storageMock));
152+
//end of setup
153+
154+
//begin of assertions
155+
$customStorage = CacheStore\Factory::factory($name);
156+
CacheStore\Factory::disableCaching();
157+
$noCacheStorage = CacheStore\Factory::factory($name);
158+
159+
$this->assertInstanceOf('\Domino\CacheStore\Storage\NoCache', $noCacheStorage);
160+
CacheStore\Factory::enableCaching();
161+
162+
$this->assertSame($customStorage, CacheStore\Factory::factory($name));
163+
//end of assertions
164+
}
127165
}

0 commit comments

Comments
 (0)