Skip to content

Commit 6878b25

Browse files
committed
Added simple-cache dependency. Added Decorator implementation (v1).
1 parent b0980f5 commit 6878b25

2 files changed

Lines changed: 146 additions & 0 deletions

File tree

PrefixedSimpleCache.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
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 Psr\SimpleCache\CacheInterface;
15+
16+
/**
17+
* PrefixedSimpleCache.
18+
*
19+
* Prefixes all cache keys with a string.
20+
*
21+
* @author ndobromirov
22+
*/
23+
class PrefixedSimpleCache implements CacheInterface
24+
{
25+
/**
26+
* @type CacheInterface
27+
*/
28+
private $cache;
29+
30+
/**
31+
* @type string
32+
*/
33+
private $prefix;
34+
35+
/**
36+
* @param CacheInterface $simpleCache
37+
* @param string $prefix
38+
*/
39+
public function __construct(CacheInterface $simpleCache, $prefix)
40+
{
41+
$this->cache = $simpleCache;
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 clear()
69+
{
70+
$this->cache->clear();
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function delete($key)
77+
{
78+
$this->prefixValue($key);
79+
80+
return $this->cache->delete($key);
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function deleteMultiple($keys)
87+
{
88+
$this->prefixValues($keys);
89+
90+
return $this->cache->deleteMultiple($keys);
91+
}
92+
93+
/**
94+
* {@inheritdoc}
95+
*/
96+
public function get($key, $default = null)
97+
{
98+
$this->prefixValue($key);
99+
100+
return $this->cache->get($key, $default);
101+
}
102+
103+
/**
104+
* {@inheritdoc}
105+
*/
106+
public function getMultiple($keys, $default = null)
107+
{
108+
$this->prefixValues($keys);
109+
110+
return $this->cache->getMultiple($keys, $default);
111+
}
112+
113+
/**
114+
* {@inheritdoc}
115+
*/
116+
public function has($key)
117+
{
118+
$this->prefixValue($key);
119+
120+
return $this->cache->has($key);
121+
}
122+
123+
/**
124+
* {@inheritdoc}
125+
*/
126+
public function set($key, $value, $ttl = null)
127+
{
128+
$this->prefixValue($key);
129+
130+
return $this->set($key, $value, $ttl);
131+
}
132+
133+
/**
134+
* {@inheritdoc}
135+
*/
136+
public function setMultiple($values, $ttl = null)
137+
{
138+
$keys = array_keys($values);
139+
$this->prefixValues($keys);
140+
141+
$values = array_combine($keys, array_values($values));
142+
143+
return $this->cache->setMultiple($values, $ttl);
144+
}
145+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"require": {
2121
"php": "^5.6 || ^7.0",
2222
"psr/cache": "^1.0",
23+
"psr/simple-cache": "^1.0",
2324
"cache/hierarchical-cache": "^0.4"
2425
},
2526
"require-dev": {

0 commit comments

Comments
 (0)