1 | <?php |
||
15 | class CacheFilesystem extends CacheAbstract { |
||
16 | /** |
||
17 | * |
||
18 | * @var \Cache_Lite_Timed |
||
19 | */ |
||
20 | private $cache; |
||
21 | |||
22 | private $path = '/tmp/'; |
||
23 | |||
24 | // @codeCoverageIgnoreStart |
||
25 | /** |
||
26 | * Cache constructor (this is a singleton). |
||
27 | * |
||
28 | * @return CacheFS The cache singleton. |
||
29 | */ |
||
30 | static public function singleton() { |
||
31 | static $instances; |
||
32 | |||
33 | if (!isset($instances)) { |
||
34 | $instances = new CacheFilesystem(); |
||
35 | } |
||
36 | return $instances; |
||
37 | } |
||
38 | |||
39 | // Prevent users to clone the instance |
||
40 | public function __clone() { |
||
41 | trigger_error('Cloning is not allowed.', LH_TRIGGER_UNEXPECTED); |
||
42 | } |
||
43 | // @codeCoverageIgnoreEnd |
||
44 | |||
45 | /** |
||
46 | * Constructor. |
||
47 | * @codeCoverageIgnore |
||
48 | */ |
||
49 | private function __construct() { |
||
50 | $this->lifetime = 3600 * 24 * 30; |
||
51 | $this->enable(); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Sets path to store data |
||
56 | * |
||
57 | * @param string $path |
||
58 | * @throws RuntimeException |
||
59 | * @return CacheFS |
||
60 | * @codeCoverageIgnore |
||
61 | */ |
||
62 | public function setPath($path) { |
||
63 | if (!is_writable($this->path) || !file_exists($this->path) || !is_dir($this->path)) { |
||
64 | throw new RuntimeException('Invalid dir or missing permissions'); |
||
65 | } |
||
66 | |||
67 | $this->path = $path . '/'; |
||
68 | |||
69 | // reload |
||
70 | if ($this->isEnabled()) { |
||
71 | $this->enable(); |
||
72 | } |
||
73 | |||
74 | return $this; |
||
75 | } |
||
76 | |||
77 | public function enable() { |
||
78 | $this->cache = new \Cache_Lite_Timed( |
||
79 | array( |
||
80 | 'cacheDir' => $this->path, |
||
81 | 'lifeTime' => $this->getDefaultLifetime(), // in seconds |
||
82 | 'automaticCleaningFactor' => 200, |
||
83 | 'hashedDirectoryLevel' => 1, |
||
84 | 'writeControl' => false, |
||
85 | ) |
||
86 | ); |
||
87 | return parent::enable(); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * (non-PHPdoc) |
||
92 | * @see \Cachearium\CacheAbstract::hashKey($k) |
||
93 | */ |
||
94 | protected function hashKey(CacheKey $k) { |
||
98 | |||
99 | /** |
||
100 | * (non-PHPdoc) |
||
101 | * @see \Cachearium\CacheAbstract::increment($value, $k, $default) |
||
102 | */ |
||
103 | public function increment($value, CacheKey $k, $default = 0) { |
||
106 | |||
107 | /** |
||
108 | * (non-PHPdoc) |
||
109 | * @see \Cachearium\CacheAbstract::get($k) |
||
110 | */ |
||
111 | public function get(CacheKey $k) { |
||
137 | |||
138 | /** |
||
139 | * (non-PHPdoc) |
||
140 | * @see \Cachearium\CacheAbstract::store($data, $k, $lifetime) |
||
141 | */ |
||
142 | public function store($data, CacheKey $k, $lifetime = -1) { |
||
143 | // @codeCoverageIgnoreStart |
||
144 | if (!$this->enabled) { |
||
145 | return false; |
||
146 | } |
||
147 | // @codeCoverageIgnoreEnd |
||
148 | |||
149 | $group = $this->hashKey($k); |
||
150 | if (!is_string($k->sub)) { |
||
151 | $cacheid = md5(serialize($k->sub)); |
||
152 | } |
||
153 | else { |
||
154 | $cacheid = $k->sub; |
||
155 | } |
||
156 | return $this->cache->save( |
||
157 | serialize($data), $cacheid, $group, ($lifetime < 0 ? $this->getDefaultLifetime() : $lifetime) |
||
158 | ); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * (non-PHPdoc) |
||
163 | * @see \Cachearium\CacheAbstract::delete($k) |
||
164 | */ |
||
165 | public function delete(CacheKey $k) { |
||
176 | |||
177 | /** |
||
178 | * (non-PHPdoc) |
||
179 | * @see \Cachearium\CacheAbstract::setDefaultLifetime($lifetime) |
||
180 | */ |
||
181 | public function setDefaultLifetime($lifetime = 0) { |
||
182 | parent::setDefaultLifetime($lifetime); |
||
183 | $this->cache->setLifeTime($this->getDefaultLifetime()); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * (non-PHPdoc) |
||
188 | * @see \Cachearium\CacheAbstract::cleanP($base, $id) |
||
189 | */ |
||
190 | public function cleanP($base, $id) { |
||
191 | // @codeCoverageIgnoreStart |
||
192 | if (!$this->enabled) { |
||
193 | return false; |
||
194 | } |
||
195 | // @codeCoverageIgnoreEnd |
||
196 | |||
197 | $group = $this->hashKey(new CacheKey($base, $id)); |
||
198 | $retval = $this->cache->clean($group, 'ingroup'); |
||
199 | $this->log(CacheLogEnum::CLEANED, new CacheKey($base, $id)); |
||
200 | return $retval; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * (non-PHPdoc) |
||
205 | * @see \Cachearium\CacheAbstract::clear() |
||
206 | */ |
||
207 | public function clear() { |
||
208 | if ($this->cache) { |
||
209 | $this->cache->clean(); |
||
210 | } |
||
211 | return true; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * (non-PHPdoc) |
||
216 | * @see CacheAbstract::prefetch() |
||
217 | * @codeCoverageIgnore |
||
218 | */ |
||
219 | public function prefetch($data) { |
||
220 | // nothing. |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * (non-PHPdoc) |
||
225 | * @see \Cachearium\CacheAbstract::report() |
||
226 | * @codeCoverageIgnore |
||
227 | */ |
||
228 | public function report() { |
||
240 | } |
||
241 |