Skip to content

Commit 8a3868d

Browse files
authored
Added prefixed cache (#96)
* Added prefixed cache * Code style
0 parents  commit 8a3868d

File tree

9 files changed

+436
-0
lines changed

9 files changed

+436
-0
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This is a READ ONLY repository.
2+
3+
Please make your pull request to https://github.com/php-cache/cache
4+
5+
Thank you for contributing.

.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+

Changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Change Log
2+
3+
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
4+
5+
## UNRELEASED
6+
7+

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+

PrefixedCachePool.php

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

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Prefixed 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/prefixed-cache/v/stable)](https://packagist.org/packages/cache/prefixed-cache)
4+
[![Total Downloads](https://poser.pugx.org/cache/prefixed-cache/downloads)](https://packagist.org/packages/cache/prefixed-cache)
5+
[![Monthly Downloads](https://poser.pugx.org/cache/prefixed-cache/d/monthly.png)](https://packagist.org/packages/cache/prefixed-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 PSR-6 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/prefixed-cache
17+
```
18+
19+
### Use
20+
21+
```php
22+
$hierarchyPool = new RedisCachePool($client);
23+
$prefixedPool = new PrefixedCachePool($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/PrefixedCachePoolTest.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\Prefixed\Tests;
13+
14+
use Cache\Prefixed\PrefixedCachePool;
15+
use Psr\Cache\CacheItemInterface;
16+
use Psr\Cache\CacheItemPoolInterface;
17+
18+
/**
19+
* We should not use constants on interfaces in the tests. Tests should break if the constant is changed.
20+
*
21+
* @author Tobias Nyholm <[email protected]>
22+
*/
23+
class PrefixedCachePoolTest extends \PHPUnit_Framework_TestCase
24+
{
25+
/**
26+
* @return \PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private function getHierarchyCacheStub()
29+
{
30+
return $this->getMockBuilder(CacheItemPoolInterface::class)->setMethods(
31+
['getItem', 'getItems', 'hasItem', 'clear', 'deleteItem', 'deleteItems', 'save', 'saveDeferred', 'commit']
32+
)->getMock();
33+
}
34+
35+
public function testGetItem()
36+
{
37+
$prefix = 'ns';
38+
$key = 'key';
39+
$returnValue = true;
40+
41+
$stub = $this->getHierarchyCacheStub();
42+
$stub->expects($this->once())->method('getItem')->with($prefix.$key)->willReturn($returnValue);
43+
44+
$pool = new PrefixedCachePool($stub, $prefix);
45+
$this->assertEquals($returnValue, $pool->getItem($key));
46+
}
47+
48+
public function testGetItems()
49+
{
50+
$prefix = 'ns';
51+
$key0 = 'key0';
52+
$key1 = 'key1';
53+
$returnValue = true;
54+
55+
$stub = $this->getHierarchyCacheStub();
56+
$stub->expects($this->once())->method('getItems')->with([$prefix.$key0, $prefix.$key1])->willReturn($returnValue);
57+
58+
$pool = new PrefixedCachePool($stub, $prefix);
59+
$this->assertEquals($returnValue, $pool->getItems([$key0, $key1]));
60+
}
61+
62+
public function testHasItem()
63+
{
64+
$prefix = 'ns';
65+
$key = 'key';
66+
$returnValue = true;
67+
68+
$stub = $this->getHierarchyCacheStub();
69+
$stub->expects($this->once())->method('hasItem')->with($prefix.$key)->willReturn($returnValue);
70+
71+
$pool = new PrefixedCachePool($stub, $prefix);
72+
$this->assertEquals($returnValue, $pool->hasItem($key));
73+
}
74+
75+
public function testClear()
76+
{
77+
$prefix = 'ns';
78+
$key = 'key';
79+
$returnValue = true;
80+
81+
$stub = $this->getHierarchyCacheStub();
82+
$stub->expects($this->once())->method('clear')->willReturn($returnValue);
83+
84+
$pool = new PrefixedCachePool($stub, $prefix);
85+
$this->assertEquals($returnValue, $pool->clear($key));
86+
}
87+
88+
public function testDeleteItem()
89+
{
90+
$prefix = 'ns';
91+
$key = 'key';
92+
$returnValue = true;
93+
94+
$stub = $this->getHierarchyCacheStub();
95+
$stub->expects($this->once())->method('deleteItem')->with($prefix.$key)->willReturn($returnValue);
96+
97+
$pool = new PrefixedCachePool($stub, $prefix);
98+
$this->assertEquals($returnValue, $pool->deleteItem($key));
99+
}
100+
101+
public function testDeleteItems()
102+
{
103+
$prefix = 'ns';
104+
$key0 = 'key0';
105+
$key1 = 'key1';
106+
$returnValue = true;
107+
108+
$stub = $this->getHierarchyCacheStub();
109+
$stub->expects($this->once())->method('deleteItems')->with([$prefix.$key0, $prefix.$key1])->willReturn($returnValue);
110+
111+
$pool = new PrefixedCachePool($stub, $prefix);
112+
$this->assertEquals($returnValue, $pool->deleteItems([$key0, $key1]));
113+
}
114+
115+
public function testSave()
116+
{
117+
$item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
118+
$prefix = 'ns';
119+
$returnValue = true;
120+
121+
$stub = $this->getHierarchyCacheStub();
122+
$stub->expects($this->once())->method('save')->with($item)->willReturn($returnValue);
123+
124+
$pool = new PrefixedCachePool($stub, $prefix);
125+
$this->assertEquals($returnValue, $pool->save($item));
126+
}
127+
128+
public function testSaveDeffered()
129+
{
130+
$item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
131+
$prefix = 'ns';
132+
$returnValue = true;
133+
134+
$stub = $this->getHierarchyCacheStub();
135+
$stub->expects($this->once())->method('saveDeferred')->with($item)->willReturn($returnValue);
136+
137+
$pool = new PrefixedCachePool($stub, $prefix);
138+
$this->assertEquals($returnValue, $pool->saveDeferred($item));
139+
}
140+
141+
public function testCommit()
142+
{
143+
$prefix = 'ns';
144+
$returnValue = true;
145+
146+
$stub = $this->getHierarchyCacheStub();
147+
$stub->expects($this->once())->method('commit')->willReturn($returnValue);
148+
149+
$pool = new PrefixedCachePool($stub, $prefix);
150+
$this->assertEquals($returnValue, $pool->commit());
151+
}
152+
}

0 commit comments

Comments
 (0)