1
|
|
|
<?php namespace Comodojo\Cache\Providers; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Cache\Components\FileSystemTools; |
4
|
|
|
use \Comodojo\Exception\CacheException; |
5
|
|
|
use \Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* File cache class |
9
|
|
|
* |
10
|
|
|
* @package Comodojo Spare Parts |
11
|
|
|
* @author Marco Giovinazzi <[email protected]> |
12
|
|
|
* @license MIT |
13
|
|
|
* |
14
|
|
|
* LICENSE: |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
22
|
|
|
* THE SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
class Filesystem extends AbstractFileSystemProvider { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
33 |
|
public function set($name, $data, $ttl = null) { |
31
|
|
|
|
32
|
33 |
|
if ( empty($name) ) throw new CacheException("Name of object cannot be empty"); |
33
|
|
|
|
34
|
33 |
|
if ( is_null($data) ) throw new CacheException("Object content cannot be null"); |
35
|
|
|
|
36
|
33 |
|
if ( !$this->isEnabled() ) return false; |
37
|
|
|
|
38
|
33 |
|
$this->resetErrorState(); |
39
|
|
|
|
40
|
|
|
try { |
41
|
|
|
|
42
|
33 |
|
$this->setTtl($ttl); |
43
|
|
|
|
44
|
33 |
|
$shadowName = $this->cache_folder.md5($name)."-".$this->getNamespace(); |
45
|
|
|
|
46
|
33 |
|
$shadowData = serialize($data); |
47
|
|
|
|
48
|
33 |
|
$shadowTtl = $this->getTime() + $this->ttl; |
49
|
|
|
|
50
|
33 |
|
if ( $this->xattr_support ) { |
51
|
|
|
|
52
|
|
|
$return = $this->setXattr($shadowName, $shadowData, $shadowTtl); |
53
|
|
|
|
54
|
|
|
} else { |
55
|
|
|
|
56
|
33 |
|
$return = $this->setGhost($shadowName, $shadowData, $shadowTtl); |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
33 |
|
} catch (CacheException $ce) { |
61
|
|
|
|
62
|
|
|
throw $ce; |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
33 |
|
return $return; |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
20 |
|
public function get($name) { |
74
|
|
|
|
75
|
20 |
|
if ( empty($name) ) throw new CacheException("Name of object cannot be empty"); |
76
|
|
|
|
77
|
20 |
|
if ( $this->isEnabled() === false ) return null; |
78
|
|
|
|
79
|
20 |
|
$this->resetErrorState(); |
80
|
|
|
|
81
|
20 |
|
$shadowName = $this->cache_folder.md5($name)."-".$this->getNamespace(); |
82
|
|
|
|
83
|
20 |
|
if ( $this->xattr_support ) { |
84
|
|
|
|
85
|
|
|
$return = $this->getXattr($shadowName, $this->getTime()); |
86
|
|
|
|
87
|
|
|
} else { |
88
|
|
|
|
89
|
20 |
|
$return = $this->getGhost($shadowName, $this->getTime()); |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
20 |
|
return is_null($return) ? $return : unserialize($return); |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
11 |
|
public function delete($name = null) { |
101
|
|
|
|
102
|
11 |
|
if ( !$this->isEnabled() ) return false; |
103
|
|
|
|
104
|
11 |
|
$this->resetErrorState(); |
105
|
|
|
|
106
|
11 |
|
$return = true; |
107
|
|
|
|
108
|
11 |
|
if ( is_null($name) ) { |
109
|
|
|
|
110
|
|
|
$filesList = glob($this->cache_folder."*-".$this->getNamespace().".{cache,expire}", GLOB_BRACE); |
111
|
|
|
|
112
|
|
|
} else { |
113
|
|
|
|
114
|
11 |
|
$filesList = glob($this->cache_folder.md5($name)."-".$this->getNamespace().".{cache,expire}", GLOB_BRACE); |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
11 |
View Code Duplication |
foreach ( $filesList as $file ) { |
|
|
|
|
119
|
|
|
|
120
|
11 |
|
if ( unlink($file) === false ) { |
121
|
|
|
|
122
|
|
|
$this->logger->error("Failed to unlink cache file $file, exiting gracefully", pathinfo($file)); |
123
|
|
|
|
124
|
|
|
$this->setErrorState("Failed to unlink cache file $file"); |
125
|
|
|
|
126
|
|
|
$return = false; |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
11 |
|
} |
131
|
|
|
|
132
|
11 |
|
return $return; |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
7 |
|
public function flush() { |
140
|
|
|
|
141
|
7 |
|
if ( !$this->isEnabled() ) return false; |
142
|
|
|
|
143
|
7 |
|
$this->resetErrorState(); |
144
|
|
|
|
145
|
7 |
|
$return = true; |
146
|
|
|
|
147
|
7 |
|
$filesList = glob($this->cache_folder."*.{cache,expire}", GLOB_BRACE); |
148
|
|
|
|
149
|
7 |
View Code Duplication |
foreach ( $filesList as $file ) { |
|
|
|
|
150
|
|
|
|
151
|
7 |
|
if ( unlink($file) === false ) { |
152
|
|
|
|
153
|
|
|
$this->logger->error("Failed to unlink cache file $file, exiting gracefully", pathinfo($file)); |
154
|
|
|
|
155
|
|
|
$this->setErrorState("Failed to unlink cache file $file"); |
156
|
|
|
|
157
|
|
|
$return = false; |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
7 |
|
} |
162
|
|
|
|
163
|
7 |
|
return $return; |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
7 |
|
public function status() { |
171
|
|
|
|
172
|
7 |
|
$filesList = glob($this->cache_folder."*.cache"); |
173
|
|
|
|
174
|
7 |
|
if ( FileSystemTools::checkXattrSupport() ) { |
175
|
|
|
|
176
|
|
|
$options = array( |
177
|
|
|
"xattr_supported" => true, |
178
|
|
|
"xattr_enabled" => FileSystemTools::checkXattrFilesystemSupport($this->cache_folder) |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
} else { |
182
|
|
|
|
183
|
|
|
$options = array( |
184
|
7 |
|
"xattr_supported" => false, |
185
|
|
|
"xattr_enabled" => false |
186
|
7 |
|
); |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return array( |
191
|
7 |
|
"provider" => "file", |
192
|
7 |
|
"enabled" => $this->isEnabled(), |
193
|
7 |
|
"objects" => count($filesList), |
194
|
|
|
"options" => $options |
195
|
7 |
|
); |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
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.