1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the m1\vars library |
5
|
|
|
* |
6
|
|
|
* (c) m1 <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @package m1/vars |
12
|
|
|
* @version 0.3.0 |
13
|
|
|
* @author Miles Croxford <[email protected]> |
14
|
|
|
* @copyright Copyright (c) Miles Croxford <[email protected]> |
15
|
|
|
* @license http://github.com/m1/vars/blob/master/LICENSE |
16
|
|
|
* @link http://github.com/m1/vars/blob/master/README.MD Documentation |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace M1\Vars\Cache; |
20
|
|
|
|
21
|
|
|
use M1\Vars\Traits\PathTrait; |
22
|
|
|
use M1\Vars\Vars; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The CacheProvider provides the cached file if one exists and is requested |
26
|
|
|
* |
27
|
|
|
* @since 0.1.0 |
28
|
|
|
*/ |
29
|
|
|
class CacheProvider |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Used for path functions and variables |
33
|
|
|
*/ |
34
|
|
|
use PathTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Has the cache been attempted |
38
|
|
|
* |
39
|
|
|
* @var bool $attempted |
40
|
|
|
*/ |
41
|
|
|
private $attempted = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* How long for the cache to be fresh |
45
|
|
|
* |
46
|
|
|
* @var int $expire |
47
|
|
|
*/ |
48
|
|
|
private $expire; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Has the cache been found |
52
|
|
|
* |
53
|
|
|
* @var bool $hit |
54
|
|
|
*/ |
55
|
|
|
private $hit = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The loaded Vars object from cache |
59
|
|
|
* |
60
|
|
|
* @var \M1\Vars\Vars $loaded_vars |
61
|
|
|
*/ |
62
|
|
|
private $loaded_vars; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The cache file name |
66
|
|
|
* |
67
|
|
|
* @var string $name |
68
|
|
|
*/ |
69
|
|
|
private $name; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Is the cache turned on |
73
|
|
|
* |
74
|
|
|
* @var boolean $provide |
75
|
|
|
*/ |
76
|
|
|
private $provide; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* If cached, the time when the class was cached |
80
|
|
|
* |
81
|
|
|
* @var int $time |
82
|
|
|
*/ |
83
|
|
|
private $time; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Creates a new instance of the cacheProvider for Vars |
87
|
|
|
* |
88
|
|
|
* @param string|array $resource The main configuration resource |
89
|
|
|
* @param array $options The options being used for Vars |
90
|
|
|
*/ |
91
|
80 |
|
public function __construct($resource, $options) |
92
|
|
|
{ |
93
|
80 |
|
$this->setProvide($options['cache']); |
94
|
80 |
|
$this->setPath($options['cache_path'], true); |
95
|
|
|
|
96
|
79 |
|
$this->expire = $options['cache_expire']; |
97
|
79 |
|
$this->name = md5(serialize($resource)); |
98
|
79 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Checks the cache to see if there is a valid cache available |
102
|
|
|
* |
103
|
|
|
* @return bool Returns true if has the cached resource |
104
|
|
|
*/ |
105
|
78 |
|
public function checkCache() |
106
|
|
|
{ |
107
|
78 |
|
if ($this->provide && $this->path && !$this->getAttempted()) { |
108
|
7 |
|
$file = sprintf('%s/%s.php', $this->path, $this->name); |
109
|
7 |
|
$this->attempted = true; |
110
|
|
|
|
111
|
7 |
|
if (is_file($file) && |
112
|
7 |
|
filemtime($file) >= (time() - $this->expire)) { |
113
|
3 |
|
$this->hit = true; |
114
|
3 |
|
return true; |
115
|
|
|
} |
116
|
7 |
|
} |
117
|
78 |
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Load the cached file into $this->loaded_vars |
122
|
|
|
*/ |
123
|
3 |
|
public function load() |
124
|
|
|
{ |
125
|
3 |
|
$cached_file = sprintf('%s/%s.php', $this->path, $this->name); |
126
|
3 |
|
$this->loaded_vars = unserialize(file_get_contents($cached_file)); |
127
|
3 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Transfer the contents of the parent Vars object into a file for cache |
131
|
|
|
* |
132
|
|
|
* @param \M1\Vars\Vars $vars Parent vars object |
133
|
|
|
*/ |
134
|
62 |
|
public function makeCache(Vars $vars) |
135
|
|
|
{ |
136
|
62 |
|
if ($this->provide) { |
137
|
7 |
|
$cache_file = sprintf('%s/%s.php', $this->path, $this->name); |
138
|
7 |
|
file_put_contents($cache_file, serialize($vars)); |
139
|
7 |
|
} |
140
|
62 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns if cache is on or off |
144
|
|
|
* |
145
|
|
|
* @return bool Is cache on or off |
146
|
|
|
*/ |
147
|
68 |
|
public function getProvide() |
148
|
|
|
{ |
149
|
68 |
|
return $this->provide; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Set the cache on or off |
154
|
|
|
* |
155
|
|
|
* @param bool $provide Does the cache want to be on or off |
156
|
|
|
* |
157
|
|
|
* @return \M1\Vars\Cache\CacheProvider |
158
|
|
|
*/ |
159
|
80 |
|
public function setProvide($provide) |
160
|
|
|
{ |
161
|
80 |
|
$this->provide = $provide; |
162
|
80 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns if the cache has been attempted |
167
|
|
|
* |
168
|
|
|
* @return bool Has the cache been attempted |
169
|
|
|
*/ |
170
|
7 |
|
public function getAttempted() |
171
|
|
|
{ |
172
|
7 |
|
return $this->attempted; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Returns how long the cache lasts for |
177
|
|
|
* |
178
|
|
|
* @return int Cache expire time |
179
|
|
|
*/ |
180
|
2 |
|
public function getExpire() |
181
|
|
|
{ |
182
|
2 |
|
return $this->expire; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Returns how long the cache lasts for |
187
|
|
|
* |
188
|
|
|
* @return int Cache expire time |
189
|
|
|
*/ |
190
|
62 |
|
public function isHit() |
191
|
|
|
{ |
192
|
62 |
|
return $this->hit; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns when the cache was made |
197
|
|
|
* |
198
|
|
|
* @return int Cache creation time |
199
|
|
|
*/ |
200
|
3 |
|
public function getTime() |
201
|
|
|
{ |
202
|
3 |
|
return $this->time; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Sets the time when the Vars was cached |
207
|
|
|
* |
208
|
|
|
* @param int $time Time when vars cached was created |
209
|
|
|
* |
210
|
|
|
* @return \M1\Vars\Cache\CacheProvider The cacheProvider object |
211
|
|
|
*/ |
212
|
62 |
|
public function setTime($time) |
213
|
|
|
{ |
214
|
62 |
|
$this->time = $time; |
215
|
62 |
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Returns the loaded Vars object |
220
|
|
|
* |
221
|
|
|
* @return \M1\Vars\Vars The loaded Vars object |
222
|
|
|
*/ |
223
|
3 |
|
public function getLoadedVars() |
224
|
|
|
{ |
225
|
3 |
|
return $this->loaded_vars; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|