Skip to content

Commit 5f9f45b

Browse files
committed
Merge branch 'stevleibelt-master'
2 parents 043079e + e24ee6f commit 5f9f45b

File tree

7 files changed

+222
-14
lines changed

7 files changed

+222
-14
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
.idea
12
vendor
2-
composer.lock
3+
composer.lock

README.md

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
DominoCacheStore
2-
=================
1+
# DominoCacheStore
2+
33
[![Build Status](https://travis-ci.org/SNakano/CacheStore.png)](https://travis-ci.org/SNakano/CacheStore)
44
[![Latest Stable Version](https://poser.pugx.org/snakano/cache-store/v/stable.svg)](https://packagist.org/packages/snakano/cache-store)
55
[![Total Downloads](https://poser.pugx.org/snakano/cache-store/downloads.svg)](https://packagist.org/packages/snakano/cache-store)
@@ -12,8 +12,7 @@ provides a generic way to cache any data.
1212
- support namespace. (use namespace delete)
1313

1414

15-
Install
16-
-------
15+
## Install
1716

1817
using Composer(recommended):
1918

@@ -27,8 +26,7 @@ using Composer(recommended):
2726
or cloning this repository.
2827

2928

30-
Usage
31-
-----
29+
## Usage
3230

3331
```php
3432
// configure cache setting.
@@ -89,9 +87,18 @@ $storage->get('ns2', 'key1');
8987
$storage->clearAll();
9088
$storage->get('ns2', 'key1');
9189
# => null
90+
91+
// disable caching at runtime
92+
Domino\CacheStore\Factory::disableCaching();
93+
// since caching is disabled, the factory will return an instance of "NoCache"
94+
$storage = Domino\CacheStore\Factory::factory('memcached');
95+
96+
// enable caching at runtime
97+
Domino\CacheStore\Factory::enableCaching();
98+
// since caching is enabled again, the factory will return an instance of "Memcached"
99+
$storage = Domino\CacheStore\Factory::factory('memcached');
92100
```
93101

94-
License
95-
-------
102+
## License
96103

97104
MIT license.

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"require": {
1818
"php": ">=5.3.3"
1919
},
20+
"require-dev": {
21+
"phpunit/phpunit": "~4.8||~5.1"
22+
},
2023
"suggest": {
2124
"ext-apc": "APC >= 3.1.6 to use the APC storage adapter",
2225
"ext-apcu": "APCu >= 4.0.0 to use the APC User Cache",

src/Domino/CacheStore/Factory.php

+30-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class Factory
3131
*/
3232
private static $connectionMap = array();
3333

34+
/** @var bool */
35+
private static $cachingIsDisabled = false;
36+
3437
/**
3538
* Registered storage
3639
* @var array
@@ -39,6 +42,7 @@ class Factory
3942
'apc' => 'Domino\CacheStore\Storage\Apc',
4043
'memcached' => 'Domino\CacheStore\Storage\Memcached',
4144
'memcache' => 'Domino\CacheStore\Storage\Memcache',
45+
'nocache' => 'Domino\CacheStore\Storage\NoCache',
4246
'redis' => 'Domino\CacheStore\Storage\Redis',
4347
);
4448

@@ -104,6 +108,27 @@ public static function clearConnectionCache()
104108
self::$connectionMap = array();
105109
}
106110

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+
107132
/**
108133
* Instantiate a cache storage
109134
* @param string $storage_type cache store storage type (eg. 'apc', 'memcached')
@@ -115,6 +140,9 @@ public static function factory($storage_type)
115140
if (!array_key_exists($storage_type, self::$storage)) {
116141
throw new StorageException(sprintf('Storage class not set for type %s', $storage_type));
117142
}
143+
if (self::$cachingIsDisabled) {
144+
$storage_type = 'nocache';
145+
}
118146
if (!isset(self::$connectionMap[$storage_type])) {
119147
self::$connectionMap[$storage_type] = new self::$storage[$storage_type](self::getOption($storage_type));
120148
}
@@ -125,8 +153,8 @@ public static function factory($storage_type)
125153
/**
126154
* Register a cache storage
127155
*
128-
* @param $storage_type cache store storage type (eg. 'apc', 'memcached', 'my_apc')
129-
* @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
130158
* @throws StorageException when $storage_class not implements Domino\CacheStore\Storage\StorageInterface
131159
*/
132160
public static function registerStorage($storage_type, $storage_class)
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* @author: stev leibelt <artodeto@bazzline.net>
4+
* @since: 2016-01-15
5+
*/
6+
namespace Domino\CacheStore\Storage;
7+
8+
/**
9+
* Class NoCache
10+
*
11+
* @package Domino\CacheStore\Storage
12+
*/
13+
class NoCache implements StorageInterface
14+
{
15+
/**
16+
* Constructor
17+
*
18+
* @param array $option cache store storage option
19+
*/
20+
public function __construct($option = array()) {}
21+
22+
/**
23+
* Set item
24+
*
25+
* @param string $namespace
26+
* @param string $key
27+
* @param mixed $value
28+
* @param integer $ttl expiration time (sec)
29+
*/
30+
public function set($namespace, $key, $value, $ttl = null) {}
31+
32+
/**
33+
* Get item
34+
*
35+
* @param string $namespace
36+
* @param string $key
37+
* @return mixed stored item
38+
*/
39+
public function get($namespace, $key)
40+
{
41+
return null;
42+
}
43+
44+
/**
45+
* Clear item
46+
*
47+
* @param string $namespace
48+
* @param string $key
49+
* @return boolean success or failure
50+
*/
51+
public function clear($namespace, $key)
52+
{
53+
return true;
54+
}
55+
56+
/**
57+
* Clear by namespace
58+
*
59+
* @param string $namespace
60+
* @return boolean success or failure
61+
*/
62+
public function clearByNamespace($namespace)
63+
{
64+
return true;
65+
}
66+
67+
/**
68+
* Clear all
69+
*
70+
* @return boolean success or failure
71+
*/
72+
public function clearAll()
73+
{
74+
return true;
75+
}
76+
}

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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* @author: stev leibelt <artodeto@bazzline.net>
4+
* @since: 2016-01-15
5+
*/
6+
namespace Domino\CacheStore\Tests\Storage;
7+
8+
use Domino\CacheStore\Storage\NoCache;
9+
10+
class NoCacheTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/** @var NoCache */
13+
private $storage;
14+
15+
public function setUp()
16+
{
17+
$this->storage = new NoCache(array());
18+
}
19+
20+
public function testSet()
21+
{
22+
$this->storage->set('namespace', 'key', 'value');
23+
}
24+
25+
public function testGet()
26+
{
27+
$result = $this->storage->get('namespace', 'key');
28+
$this->assertNull($result);
29+
}
30+
31+
public function testGetWhenKeyNotExist()
32+
{
33+
$this->assertNull($this->storage->get('namespace', 'key'));
34+
}
35+
36+
public function testClear()
37+
{
38+
$this->storage->set('namespace', 'key', 'value');
39+
$this->storage->clear('namespace', 'key');
40+
41+
$this->assertNull($this->storage->get('namespace', 'key'));
42+
}
43+
44+
public function testClearByNamespace()
45+
{
46+
$this->storage->set('namespace1', 'key1', 'value1');
47+
$this->storage->set('namespace1', 'key2', 'value2');
48+
$this->storage->set('namespace2', 'key3', 'value3');
49+
50+
$this->storage->clearByNamespace('namespace1');
51+
$this->assertNull($this->storage->get('namespace1', 'key1'));
52+
$this->assertNull($this->storage->get('namespace1', 'key2'));
53+
$this->assertNull($this->storage->get('namespace2', 'key3'));
54+
}
55+
}

0 commit comments

Comments
 (0)