|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Filesystem\Exception\IOException; |
|
6
|
|
|
use Nip_File_System as FileSystem; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Manager |
|
10
|
|
|
* @package Nip\Cache |
|
11
|
|
|
*/ |
|
12
|
|
|
class Manager |
|
13
|
|
|
{ |
|
14
|
|
|
protected $active = false; |
|
15
|
|
|
|
|
16
|
|
|
protected $ttl = 180; |
|
17
|
|
|
|
|
18
|
|
|
protected $data; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param $cacheId |
|
22
|
|
|
* @return mixed |
|
23
|
|
|
*/ |
|
24
|
|
|
public function get($cacheId) |
|
25
|
|
|
{ |
|
26
|
|
|
if (!$this->valid($cacheId)) { |
|
27
|
|
|
return null; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return $this->getData($cacheId); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param $cacheId |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
public function valid($cacheId) |
|
38
|
|
|
{ |
|
39
|
|
|
if ($this->isActive() && $this->exists($cacheId)) { |
|
40
|
|
|
$fmtime = filemtime($this->filePath($cacheId)); |
|
41
|
|
|
if (($fmtime + $this->ttl) > time()) { |
|
42
|
|
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
|
|
public function isActive() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->active; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param $active |
|
59
|
|
|
* @return $this |
|
60
|
|
|
*/ |
|
61
|
|
|
public function setActive($active) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->active = $active; |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $cacheId |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public function exists($cacheId) |
|
73
|
|
|
{ |
|
74
|
|
|
return file_exists($this->filePath($cacheId)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param $cacheId |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
public function filePath($cacheId) |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->cachePath() . $cacheId . '.php'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function cachePath() |
|
90
|
|
|
{ |
|
91
|
|
|
return app('path.storage') . DIRECTORY_SEPARATOR . 'cache'; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $cacheId |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getData($cacheId) |
|
99
|
|
|
{ |
|
100
|
|
|
if (!isset($this->data[$cacheId])) { |
|
101
|
|
|
$this->data[$cacheId] = $this->loadData($cacheId); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->data[$cacheId]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param $cacheId |
|
109
|
|
|
* @param bool $retry |
|
110
|
|
|
* @return bool|mixed |
|
111
|
|
|
*/ |
|
112
|
|
|
public function loadData($cacheId, $retry = true) |
|
113
|
|
|
{ |
|
114
|
|
|
$file = $this->filePath($cacheId); |
|
115
|
|
|
$content = file_get_contents($file); |
|
116
|
|
|
$data = unserialize($content); |
|
117
|
|
|
|
|
118
|
|
|
if (!$data) { |
|
119
|
|
|
if ($retry === false) { |
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
$this->reload($cacheId); |
|
123
|
|
|
|
|
124
|
|
|
return $this->loadData($cacheId, false); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $data; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param $cacheId |
|
132
|
|
|
*/ |
|
133
|
|
|
public function reload($cacheId) |
|
134
|
|
|
{ |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param $cacheId |
|
139
|
|
|
* @param $data |
|
140
|
|
|
* @return $this |
|
141
|
|
|
*/ |
|
142
|
|
|
public function set($cacheId, $data) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->data[$cacheId] = $data; |
|
145
|
|
|
|
|
146
|
|
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param $cacheId |
|
151
|
|
|
* @param $data |
|
152
|
|
|
* @return bool |
|
153
|
|
|
*/ |
|
154
|
|
|
public function saveData($cacheId, $data) |
|
155
|
|
|
{ |
|
156
|
|
|
$file = $this->filePath($cacheId); |
|
157
|
|
|
$content = serialize($data); |
|
158
|
|
|
|
|
159
|
|
|
return $this->save($file, $content); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param $file |
|
164
|
|
|
* @param $content |
|
165
|
|
|
* @return bool |
|
166
|
|
|
*/ |
|
167
|
|
|
public function save($file, $content) |
|
168
|
|
|
{ |
|
169
|
|
|
$dir = dirname($file); |
|
170
|
|
|
$filesystem = FileSystem::instance(); |
|
171
|
|
|
|
|
172
|
|
|
if (!is_dir($dir)) { |
|
173
|
|
|
$filesystem->createDirectory($dir, 0777); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if (file_put_contents($file, $content)) { |
|
177
|
|
|
try { |
|
178
|
|
|
$filesystem->chmod($file, 0777); |
|
179
|
|
|
} catch (IOException $e) { |
|
180
|
|
|
// discard chmod failure (some filesystem may not support it) |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return true; |
|
184
|
|
|
} else { |
|
185
|
|
|
$message = "Cannot open cache file for writing: "; |
|
186
|
|
|
// if (!Nip_Staging::instance()->isPublic()) { |
|
|
|
|
|
|
187
|
|
|
// $message .= " [ ".$this->cache_file." ] "; |
|
188
|
|
|
// } |
|
189
|
|
|
die($message); |
|
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param $cacheId |
|
195
|
|
|
*/ |
|
196
|
|
|
public function delete($cacheId) |
|
197
|
|
|
{ |
|
198
|
|
|
$file = $this->filePath($cacheId); |
|
199
|
|
|
if (is_file($file)) { |
|
200
|
|
|
unlink($file); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.