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.github.io |
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
|
34 |
|
public function __construct(CacheInterface $cacheEngine) |
36
|
|
|
{ |
37
|
34 |
|
$this->cacheEngine = $cacheEngine; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* method to check if cache has entry for given key |
42
|
|
|
* |
43
|
|
|
* @param string $key |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
22 |
|
public function has($key) |
48
|
|
|
{ |
49
|
22 |
|
$key = $this->slug($key); |
50
|
22 |
|
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
|
23 |
|
public function set($key, $value, $time = 300, array $options = array()) |
77
|
|
|
{ |
78
|
23 |
|
if (!empty($options)) { |
79
|
|
|
// not longer supported by phpFastCache |
80
|
|
|
trigger_error('Argument $options is deprecated and ignored.', E_USER_WARNING); |
81
|
|
|
} |
82
|
23 |
|
$key = $this->slug($key); |
83
|
23 |
|
$this->cacheEngine->set($key, $value, $time); |
84
|
|
|
} |
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
|
|
|
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
|
23 |
|
protected function slug(string $key): string |
119
|
|
|
{ |
120
|
23 |
|
$replacementTokens = array_map( |
121
|
23 |
|
function ($key) { |
122
|
23 |
|
return self::SLUG_PREFIX . $key; |
123
|
|
|
}, |
124
|
23 |
|
array_keys(self::SLUG) |
125
|
|
|
); |
126
|
23 |
|
return str_replace(self::SLUG, $replacementTokens, $key); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|