Skip to content

Commit 3150736

Browse files
committed
Added namespace decorator
0 parents  commit 3150736

File tree

8 files changed

+451
-0
lines changed

8 files changed

+451
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
composer.lock
3+

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Aaron Scherer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

NamespacedCachePool.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Namespaced;
13+
14+
use Cache\Hierarchy\HierarchicalPoolInterface;
15+
use Psr\Cache\CacheItemInterface;
16+
use Psr\Cache\CacheItemPoolInterface;
17+
18+
/**
19+
* Prefix all the stored items with a namespace. Also make sure you can clear all items
20+
* in that namespace.
21+
*
22+
* @author Tobias Nyholm <[email protected]>
23+
*/
24+
class NamespacedCachePool implements CacheItemPoolInterface
25+
{
26+
/**
27+
* @type HierarchicalPoolInterface|CacheItemPoolInterface
28+
*/
29+
private $cachePool;
30+
31+
/**
32+
* @type string
33+
*/
34+
private $namespace;
35+
36+
/**
37+
* @param HierarchicalPoolInterface $cachePool
38+
* @param string $namespace
39+
*/
40+
public function __construct(HierarchicalPoolInterface $cachePool, $namespace)
41+
{
42+
$this->cachePool = $cachePool;
43+
$this->namespace = $namespace;
44+
}
45+
46+
/**
47+
* Add namespace prefix on the key.
48+
*
49+
* @param array $keys
50+
*/
51+
private function prefixValue(&$key)
52+
{
53+
// |namespace|key
54+
$key = HierarchicalPoolInterface::HIERARCHY_SEPARATOR.$this->namespace.HierarchicalPoolInterface::HIERARCHY_SEPARATOR.$key;
55+
}
56+
57+
/**
58+
* @param array $keys
59+
*/
60+
private function prefixValues(array &$keys)
61+
{
62+
foreach ($keys as &$key) {
63+
$this->prefixValue($key);
64+
}
65+
}
66+
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
public function getItem($key)
71+
{
72+
$this->prefixValue($key);
73+
74+
return $this->cachePool->getItem($key);
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
public function getItems(array $keys = [])
81+
{
82+
$this->prefixValues($keys);
83+
84+
return $this->cachePool->getItems($keys);
85+
}
86+
87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function hasItem($key)
91+
{
92+
$this->prefixValue($key);
93+
94+
return $this->cachePool->hasItem($key);
95+
}
96+
97+
/**
98+
* {@inheritdoc}
99+
*/
100+
public function clear()
101+
{
102+
return $this->cachePool->deleteItem(HierarchicalPoolInterface::HIERARCHY_SEPARATOR.$this->namespace);
103+
}
104+
105+
/**
106+
* {@inheritdoc}
107+
*/
108+
public function deleteItem($key)
109+
{
110+
$this->prefixValue($key);
111+
112+
return $this->cachePool->deleteItem($key);
113+
}
114+
115+
/**
116+
* {@inheritdoc}
117+
*/
118+
public function deleteItems(array $keys)
119+
{
120+
$this->prefixValues($keys);
121+
122+
return $this->cachePool->deleteItems($keys);
123+
}
124+
125+
/**
126+
* {@inheritdoc}
127+
*/
128+
public function save(CacheItemInterface $item)
129+
{
130+
return $this->cachePool->save($item);
131+
}
132+
133+
/**
134+
* {@inheritdoc}
135+
*/
136+
public function saveDeferred(CacheItemInterface $item)
137+
{
138+
return $this->cachePool->saveDeferred($item);
139+
}
140+
141+
/**
142+
* {@inheritdoc}
143+
*/
144+
public function commit()
145+
{
146+
return $this->cachePool->commit();
147+
}
148+
}

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Namespaced PSR-6 cache pool
2+
[![Gitter](https://badges.gitter.im/php-cache/cache.svg)](https://gitter.im/php-cache/cache?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
3+
[![Latest Stable Version](https://poser.pugx.org/cache/namespaced-cache/v/stable)](https://packagist.org/packages/cache/namespaced-cache)
4+
[![Total Downloads](https://poser.pugx.org/cache/namespaced-cache/downloads)](https://packagist.org/packages/cache/namespaced-cache)
5+
[![Monthly Downloads](https://poser.pugx.org/cache/namespaced-cache/d/monthly.png)](https://packagist.org/packages/cache/namespaced-cache)
6+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
7+
8+
This is a decorator for a PSR56 hierarchical cache. It will allow you do use namespaces.
9+
10+
It is a part of the PHP Cache organisation. To read about features like tagging and hierarchy support please read
11+
the shared documentation at [www.php-cache.com](http://www.php-cache.com).
12+
13+
### Install
14+
15+
```bash
16+
composer require cache/namespaced-cache
17+
```
18+
19+
### Use
20+
21+
```php
22+
$hierarchyPool = new RedisCachePool($client);
23+
$namespacedPool = new NamespacedCachePool($hierarchyPool, 'acme');
24+
```
25+
26+
### Contribute
27+
28+
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/php-cache/cache) or
29+
report any issues you find on the [issue tracker](http://issues.php-cache.com).
30+

Tests/HelperInterface.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Namespaced\Tests;
13+
14+
use Cache\Hierarchy\HierarchicalPoolInterface;
15+
use Psr\Cache\CacheItemPoolInterface;
16+
17+
interface HelperInterface extends HierarchicalPoolInterface, CacheItemPoolInterface
18+
{
19+
}

Tests/NamespacedCachePoolTest.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Namespaced\Tests;
13+
14+
use Cache\Namespaced\NamespacedCachePool;
15+
use Psr\Cache\CacheItemInterface;
16+
17+
/**
18+
* We should not use constants on interfaces in the tests. Tests should break if the constant is changed.
19+
*
20+
* @author Tobias Nyholm <[email protected]>
21+
*/
22+
class NamespacedCachePoolTest extends \PHPUnit_Framework_TestCase
23+
{
24+
/**
25+
* @return \PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
private function getHierarchyCacheStub()
28+
{
29+
return $this->getMock(
30+
HelperInterface::class,
31+
['getItem', 'getItems', 'hasItem', 'clear', 'deleteItem', 'deleteItems', 'save', 'saveDeferred', 'commit']
32+
);
33+
}
34+
35+
public function testGetItem()
36+
{
37+
$namespace = 'ns';
38+
$key = 'key';
39+
$returnValue = true;
40+
41+
$stub = $this->getHierarchyCacheStub();
42+
$stub->expects($this->once())->method('getItem')->with('|'.$namespace.'|'.$key)->willReturn($returnValue);
43+
44+
$pool = new NamespacedCachePool($stub, $namespace);
45+
$this->assertEquals($returnValue, $pool->getItem($key));
46+
}
47+
48+
public function testGetItems()
49+
{
50+
$namespace = 'ns';
51+
$key0 = 'key0';
52+
$key1 = 'key1';
53+
$returnValue = true;
54+
55+
$stub = $this->getHierarchyCacheStub();
56+
$stub->expects($this->once())->method('getItems')->with(['|'.$namespace.'|'.$key0, '|'.$namespace.'|'.$key1])->willReturn($returnValue);
57+
58+
$pool = new NamespacedCachePool($stub, $namespace);
59+
$this->assertEquals($returnValue, $pool->getItems([$key0, $key1]));
60+
}
61+
62+
public function testHasItem()
63+
{
64+
$namespace = 'ns';
65+
$key = 'key';
66+
$returnValue = true;
67+
68+
$stub = $this->getHierarchyCacheStub();
69+
$stub->expects($this->once())->method('hasItem')->with('|'.$namespace.'|'.$key)->willReturn($returnValue);
70+
71+
$pool = new NamespacedCachePool($stub, $namespace);
72+
$this->assertEquals($returnValue, $pool->hasItem($key));
73+
}
74+
75+
public function testClear()
76+
{
77+
$namespace = 'ns';
78+
$key = 'key';
79+
$returnValue = true;
80+
81+
$stub = $this->getHierarchyCacheStub();
82+
$stub->expects($this->once())->method('deleteItem')->with('|'.$namespace)->willReturn($returnValue);
83+
84+
$pool = new NamespacedCachePool($stub, $namespace);
85+
$this->assertEquals($returnValue, $pool->clear($key));
86+
}
87+
88+
public function testDeleteItem()
89+
{
90+
$namespace = 'ns';
91+
$key = 'key';
92+
$returnValue = true;
93+
94+
$stub = $this->getHierarchyCacheStub();
95+
$stub->expects($this->once())->method('deleteItem')->with('|'.$namespace.'|'.$key)->willReturn($returnValue);
96+
97+
$pool = new NamespacedCachePool($stub, $namespace);
98+
$this->assertEquals($returnValue, $pool->deleteItem($key));
99+
}
100+
101+
public function testDeleteItems()
102+
{
103+
$namespace = 'ns';
104+
$key0 = 'key0';
105+
$key1 = 'key1';
106+
$returnValue = true;
107+
108+
$stub = $this->getHierarchyCacheStub();
109+
$stub->expects($this->once())->method('deleteItems')->with(['|'.$namespace.'|'.$key0, '|'.$namespace.'|'.$key1])->willReturn($returnValue);
110+
111+
$pool = new NamespacedCachePool($stub, $namespace);
112+
$this->assertEquals($returnValue, $pool->deleteItems([$key0, $key1]));
113+
}
114+
115+
public function testSave()
116+
{
117+
$item = $this->getMock(CacheItemInterface::class);
118+
$namespace = 'ns';
119+
$returnValue = true;
120+
121+
$stub = $this->getHierarchyCacheStub();
122+
$stub->expects($this->once())->method('save')->with($item)->willReturn($returnValue);
123+
124+
$pool = new NamespacedCachePool($stub, $namespace);
125+
$this->assertEquals($returnValue, $pool->save($item));
126+
}
127+
128+
public function testSaveDeffered()
129+
{
130+
$item = $this->getMock(CacheItemInterface::class);
131+
$namespace = 'ns';
132+
$returnValue = true;
133+
134+
$stub = $this->getHierarchyCacheStub();
135+
$stub->expects($this->once())->method('saveDeferred')->with($item)->willReturn($returnValue);
136+
137+
$pool = new NamespacedCachePool($stub, $namespace);
138+
$this->assertEquals($returnValue, $pool->saveDeferred($item));
139+
}
140+
141+
public function testCommit()
142+
{
143+
$namespace = 'ns';
144+
$returnValue = true;
145+
146+
$stub = $this->getHierarchyCacheStub();
147+
$stub->expects($this->once())->method('commit')->willReturn($returnValue);
148+
149+
$pool = new NamespacedCachePool($stub, $namespace);
150+
$this->assertEquals($returnValue, $pool->commit());
151+
}
152+
}

0 commit comments

Comments
 (0)