1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Adapter to use PSR-16 compatible cache class with Phile |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Phile\Plugin\Phile\PhpFastCache; |
7
|
|
|
|
8
|
|
|
use Psr\SimpleCache\CacheInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class PhpFastCache |
12
|
|
|
* |
13
|
|
|
* @author PhileCMS |
14
|
|
|
* @link https://philecms.com |
15
|
|
|
* @license http://opensource.org/licenses/MIT |
16
|
|
|
* @package Phile\Plugin\Phile\PhpFastCache |
17
|
|
|
*/ |
18
|
|
|
class PhileToPsr16CacheAdapter implements \Phile\ServiceLocator\CacheInterface |
19
|
|
|
{ |
20
|
|
|
/** @var string slug */ |
21
|
|
|
const SLUG_PREFIX = '-phile.phpFastCache.slug-'; |
22
|
|
|
|
23
|
|
|
const SLUG = ['{', '}' , '(', ')','/' , '\\' , '@', ':']; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var CacheInterface the cache engine |
27
|
|
|
*/ |
28
|
|
|
protected $cacheEngine; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* the constructor |
32
|
|
|
* |
33
|
|
|
* @param CacheInterface $cacheEngine |
34
|
|
|
*/ |
35
|
28 |
|
public function __construct(CacheInterface $cacheEngine) |
36
|
|
|
{ |
37
|
28 |
|
$this->cacheEngine = $cacheEngine; |
38
|
28 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* method to check if cache has entry for given key |
42
|
|
|
* |
43
|
|
|
* @param string $key |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
20 |
|
public function has($key) |
48
|
|
|
{ |
49
|
20 |
|
$key = $this->slug($key); |
50
|
20 |
|
return $this->cacheEngine->has($key); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* method to get cache entry |
55
|
|
|
* |
56
|
|
|
* @param string $key |
57
|
|
|
* |
58
|
|
|
* @return mixed|null |
59
|
|
|
*/ |
60
|
2 |
|
public function get($key) |
61
|
|
|
{ |
62
|
2 |
|
$key = $this->slug($key); |
63
|
2 |
|
return $this->cacheEngine->get($key); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* method to set cache entry |
68
|
|
|
* |
69
|
|
|
* @param string $key |
70
|
|
|
* @param mixed $value |
71
|
|
|
* @param int $time |
72
|
|
|
* @param array $options deprecated |
73
|
|
|
* |
74
|
|
|
* @return mixed|void |
75
|
|
|
*/ |
76
|
21 |
View Code Duplication |
public function set($key, $value, $time = 300, array $options = array()) |
|
|
|
|
77
|
|
|
{ |
78
|
21 |
|
if (!empty($options)) { |
79
|
|
|
// not longer supported by phpFastCache |
80
|
|
|
trigger_error('Argument $options is deprecated and ignored.', E_USER_WARNING); |
81
|
|
|
} |
82
|
21 |
|
$key = $this->slug($key); |
83
|
21 |
|
$this->cacheEngine->set($key, $value, $time); |
84
|
21 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* method to delete cache entry |
88
|
|
|
* |
89
|
|
|
* @param string $key |
90
|
|
|
* @param array $options deprecated |
91
|
|
|
* |
92
|
|
|
* @return mixed|void |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function delete($key, array $options = array()) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
if (!empty($options)) { |
97
|
|
|
// not longer supported by phpFastCache |
98
|
|
|
trigger_error('Argument $options is deprecated and ignored.', E_USER_WARNING); |
99
|
|
|
} |
100
|
|
|
$key = $this->slug($key); |
101
|
|
|
$this->cacheEngine->delete($key); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* clean complete cache and delete all cached entries |
106
|
|
|
*/ |
107
|
|
|
public function clean() |
108
|
|
|
{ |
109
|
|
|
$this->cacheEngine->clear(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* replaces chars forbidden in PSR-16 cache-keys |
114
|
|
|
* |
115
|
|
|
* @param string $key key to slug |
116
|
|
|
* @return string $key slugged key |
117
|
|
|
*/ |
118
|
21 |
|
protected function slug(string $key): string |
119
|
|
|
{ |
120
|
21 |
|
$replacementTokens = array_map( |
121
|
21 |
|
function ($key) { |
122
|
21 |
|
return self::SLUG_PREFIX . $key; |
123
|
21 |
|
}, |
124
|
21 |
|
array_keys(self::SLUG) |
125
|
|
|
); |
126
|
21 |
|
return str_replace(self::SLUG, $replacementTokens, $key); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.