1 | <?php |
||
2 | /** |
||
3 | * BEdita, API-first content management framework |
||
4 | * Copyright 2021 ChannelWeb Srl, Chialab Srl |
||
5 | * |
||
6 | * This file is part of BEdita: you can redistribute it and/or modify |
||
7 | * it under the terms of the GNU Lesser General Public License as published |
||
8 | * by the Free Software Foundation, either version 3 of the License, or |
||
9 | * (at your option) any later version. |
||
10 | * |
||
11 | * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
||
12 | */ |
||
13 | namespace BEdita\Core\Cache\Engine; |
||
14 | |||
15 | use Cake\Cache\Cache; |
||
16 | use Cake\Cache\CacheEngine; |
||
17 | use Cake\Cache\Engine\ArrayEngine; |
||
18 | use Exception; |
||
19 | |||
20 | /** |
||
21 | * This engine uses two layers of cache, one persistent and one in-memory for faster lookup times. |
||
22 | */ |
||
23 | class LayeredEngine extends CacheEngine |
||
24 | { |
||
25 | /** |
||
26 | * Persistent cache instance. |
||
27 | * |
||
28 | * @var \Cake\Cache\CacheEngine |
||
29 | */ |
||
30 | protected $persistent = null; |
||
31 | |||
32 | /** |
||
33 | * In-memory cache instance. |
||
34 | * |
||
35 | * @var \Cake\Cache\Engine\ArrayEngine |
||
36 | */ |
||
37 | protected $memory = null; |
||
38 | |||
39 | /** |
||
40 | * The default config used unless overridden by runtime configuration |
||
41 | * |
||
42 | * - `persistent` A cache configuration or an alias, to use as persistent cache |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $_defaultConfig = [ |
||
47 | 'duration' => 3600, |
||
48 | 'groups' => [], |
||
49 | 'prefix' => 'cake_', |
||
50 | 'probability' => 100, |
||
51 | 'warnOnWriteFailures' => true, |
||
52 | 'persistent' => [ |
||
53 | 'className' => 'File', |
||
54 | ], |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * {@inheritDoc} |
||
59 | * |
||
60 | * @throws \Exception If the configuration is wrong |
||
61 | */ |
||
62 | public function init(array $config = []): bool |
||
63 | { |
||
64 | parent::init($config); |
||
65 | |||
66 | $this->persistent = $this->getEngineInstance($this->getConfig('persistent')); |
||
67 | $this->memory = new ArrayEngine(); |
||
68 | $this->memory->setConfig($this->getConfig()); |
||
69 | |||
70 | return true; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Get the engine instance from a configuration or an alias. |
||
75 | * |
||
76 | * @param array|string $config Engine configuration or an alias |
||
77 | * @return \Cake\Cache\CacheEngine The engine instance |
||
78 | * @throws \Exception If the configuration is wrong |
||
79 | */ |
||
80 | protected function getEngineInstance($config): CacheEngine |
||
81 | { |
||
82 | $registry = Cache::getRegistry(); |
||
83 | |||
84 | if (is_string($config)) { |
||
85 | if (!$registry->has($config)) { |
||
86 | throw new Exception("Cache engine alias '{$config}' is not defined"); |
||
87 | } |
||
88 | |||
89 | $instance = $registry->get($config); |
||
90 | |||
91 | if ($instance === $this) { |
||
92 | throw new Exception('Recursion detected, Layered cache engine is configured as persistent engine of itself'); |
||
93 | } |
||
94 | |||
95 | if (!($instance instanceof CacheEngine)) { |
||
96 | throw new Exception("Cache engine alias '{$config}' is not an implementation of CacheEngine"); |
||
97 | } |
||
98 | |||
99 | return $instance; |
||
100 | } |
||
101 | |||
102 | if (is_array($config)) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
103 | $name = $config['className']; |
||
104 | |||
105 | if (!empty($config['prefix'])) { |
||
106 | $name = $config['prefix'] . $name; |
||
107 | } |
||
108 | |||
109 | return $registry->load($name, $config); |
||
110 | } |
||
111 | |||
112 | throw new Exception('Unknown cache configuration'); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @inheritDoc |
||
117 | */ |
||
118 | public function write($key, $value): bool |
||
119 | { |
||
120 | $this->memory->write($key, $value); |
||
121 | |||
122 | return $this->persistent->write($key, $value); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @inheritDoc |
||
127 | */ |
||
128 | public function read($key) |
||
129 | { |
||
130 | $value = $this->memory->read($key); |
||
131 | |||
132 | if ($value !== false) { |
||
133 | return $value; |
||
134 | } |
||
135 | |||
136 | $value = $this->persistent->read($key); |
||
137 | $this->memory->write($key, $value); |
||
138 | |||
139 | return $value; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @inheritDoc |
||
144 | */ |
||
145 | public function increment($key, $offset = 1) |
||
146 | { |
||
147 | $value = $this->persistent->increment($key, $offset); |
||
148 | $this->memory->write($key, $value); |
||
149 | |||
150 | return $value; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @inheritDoc |
||
155 | */ |
||
156 | public function decrement($key, $offset = 1) |
||
157 | { |
||
158 | $value = $this->persistent->decrement($key, $offset); |
||
159 | $this->memory->write($key, $value); |
||
160 | |||
161 | return $value; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @inheritDoc |
||
166 | */ |
||
167 | public function delete($key): bool |
||
168 | { |
||
169 | $this->memory->delete($key); |
||
170 | |||
171 | return $this->persistent->delete($key); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @inheritDoc |
||
176 | */ |
||
177 | public function clear($check): bool |
||
178 | { |
||
179 | $this->memory->clear($check); |
||
180 | |||
181 | return $this->persistent->clear($check); |
||
182 | } |
||
183 | } |
||
184 |