[OOP] Cache
Index.php
Code (php)
Cache\Cache.php:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace Oetzie\Framework\Cache;
use Oetzie\Framework\ServiceManager\AbstractDriverManager as AbstractDriverManager;
class Cache {
/**
* @acces protected.
* @var string.
*/
protected $driver;
/**
* @acces protected.
* @var array.
*/
protected $driverOptions = array();
/**
* @acces protected.
* @var object.
*/
protected $driverManager;
/**
* @acces public.
* @param array $options.
* @throws InvalidArgumentException.
*/
public function __construct($options = array()) {
if (!is_array($options)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), options is not an array; recieved "%s".', __CLASS__, __FUNCTION__, gettype($options)));
} else {
$options = array_merge(array(
'driver' => $this->getDriver(),
'options' => $this->getDriverOptions()
), $options);
$this->setDriver($options['driver'])
->setDriverOptions($options['options']);
}
}
/**
* @acces public.
* @param string $driver.
* @return self.
*/
public function setDriver($driver) {
$this->driver = $driver;
return $this;
}
/**
* @acces public.
* @return string.
*/
public function getDriver() {
return $this->driver;
}
/**
* @acces public.
* @param array $options.
* @return self.
*/
public function setDriverOptions($options) {
$this->driverOptions = $options;
return $this;
}
/**
* @acces public.
* @return array.
*/
public function getDriverOptions() {
return $this->driverOptions;
}
/**
* @acces public.
* @param object $driverManager.
* @return self.
*/
public function setDriverManager($driverManager) {
$this->driverManager = $driverManager;
return $this;
}
/**
* @acces public.
* @return object.
*/
public function getDriverManager() {
if (!$this->driverManager instanceof AbstractDriverManager) {
$this->setDriverManager(new DriverManager());
}
return $this->driverManager;
}
/**
* @acces public.
* @param string $method.
* @param array $event.
* @throws BadFunctionCallException.
* @return mixed.
*/
public function __call($method, $event = array()) {
$driver = $this->getDriverManager()->getDriver($this->getDriver(), $this->getDriverOptions());
if (!method_exists($driver, $method)) {
throw new Exception\BadFunctionCallException(sprintf('Failed to initialize %s (%s), method "%s" does not exists in the driver object "%s".', __CLASS__, __FUNCTION__, $method, $driver->toString()));
} else {
return call_user_func_array(array($driver, $method), $event);
}
return $this;
}
}
?>
namespace Oetzie\Framework\Cache;
use Oetzie\Framework\ServiceManager\AbstractDriverManager as AbstractDriverManager;
class Cache {
/**
* @acces protected.
* @var string.
*/
protected $driver;
/**
* @acces protected.
* @var array.
*/
protected $driverOptions = array();
/**
* @acces protected.
* @var object.
*/
protected $driverManager;
/**
* @acces public.
* @param array $options.
* @throws InvalidArgumentException.
*/
public function __construct($options = array()) {
if (!is_array($options)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), options is not an array; recieved "%s".', __CLASS__, __FUNCTION__, gettype($options)));
} else {
$options = array_merge(array(
'driver' => $this->getDriver(),
'options' => $this->getDriverOptions()
), $options);
$this->setDriver($options['driver'])
->setDriverOptions($options['options']);
}
}
/**
* @acces public.
* @param string $driver.
* @return self.
*/
public function setDriver($driver) {
$this->driver = $driver;
return $this;
}
/**
* @acces public.
* @return string.
*/
public function getDriver() {
return $this->driver;
}
/**
* @acces public.
* @param array $options.
* @return self.
*/
public function setDriverOptions($options) {
$this->driverOptions = $options;
return $this;
}
/**
* @acces public.
* @return array.
*/
public function getDriverOptions() {
return $this->driverOptions;
}
/**
* @acces public.
* @param object $driverManager.
* @return self.
*/
public function setDriverManager($driverManager) {
$this->driverManager = $driverManager;
return $this;
}
/**
* @acces public.
* @return object.
*/
public function getDriverManager() {
if (!$this->driverManager instanceof AbstractDriverManager) {
$this->setDriverManager(new DriverManager());
}
return $this->driverManager;
}
/**
* @acces public.
* @param string $method.
* @param array $event.
* @throws BadFunctionCallException.
* @return mixed.
*/
public function __call($method, $event = array()) {
$driver = $this->getDriverManager()->getDriver($this->getDriver(), $this->getDriverOptions());
if (!method_exists($driver, $method)) {
throw new Exception\BadFunctionCallException(sprintf('Failed to initialize %s (%s), method "%s" does not exists in the driver object "%s".', __CLASS__, __FUNCTION__, $method, $driver->toString()));
} else {
return call_user_func_array(array($driver, $method), $event);
}
return $this;
}
}
?>
Cache\Driver\DriverAbstract.php:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
namespace Oetzie\Framework\Cache\Driver;
use Oetzie\Framework\Cache\Exception as Exception;
abstract class DriverAbstract implements DriverInterface {
/**
* @acces protected.
* @var integer.
*/
protected $expiration = 604800;
/**
* @acces public.
* @param integer $expiration.
* @return self.
*/
public function setExpiration($expiration) {
$this->expiration = $expiration;
return $this;
}
/**
* @acces public.
* @retun integer.
*/
public function getExpiration() {
return $this->expiration;
}
}
?>
namespace Oetzie\Framework\Cache\Driver;
use Oetzie\Framework\Cache\Exception as Exception;
abstract class DriverAbstract implements DriverInterface {
/**
* @acces protected.
* @var integer.
*/
protected $expiration = 604800;
/**
* @acces public.
* @param integer $expiration.
* @return self.
*/
public function setExpiration($expiration) {
$this->expiration = $expiration;
return $this;
}
/**
* @acces public.
* @retun integer.
*/
public function getExpiration() {
return $this->expiration;
}
}
?>
Cache\Driver\DriverInterface.php:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace Oetzie\Framework\Cache\Driver;
use Oetzie\Framework\Cache\Exception as Exception;
interface DriverInterface {
/**
* @acces public.
* @param array $options.
* @throws InvalidArgumentException.
*/
public function __construct($options = array());
/**
* @acces public.
*/
public function __destruct();
/**
* @acces public.
* @param string $id.
* @param mixed $data.
* @param null|integer $expiration.
* @returns self.
*/
public function add($id, $data, $expiration = null);
/**
* @acces public.
* @param string $id.
* @param null|string $return.
* @return mixed.
*/
public function get($id, $return = 'data');
/**
* @acces public.
* @param string $id.
* @return self.
*/
public function remove($id);
/**
* @acces public.
* @param mixed $id.
* @return self.
*/
public function clear($id = null);
/**
* @acces public.
* @return string.
*/
public function toString();
}
?>
namespace Oetzie\Framework\Cache\Driver;
use Oetzie\Framework\Cache\Exception as Exception;
interface DriverInterface {
/**
* @acces public.
* @param array $options.
* @throws InvalidArgumentException.
*/
public function __construct($options = array());
/**
* @acces public.
*/
public function __destruct();
/**
* @acces public.
* @param string $id.
* @param mixed $data.
* @param null|integer $expiration.
* @returns self.
*/
public function add($id, $data, $expiration = null);
/**
* @acces public.
* @param string $id.
* @param null|string $return.
* @return mixed.
*/
public function get($id, $return = 'data');
/**
* @acces public.
* @param string $id.
* @return self.
*/
public function remove($id);
/**
* @acces public.
* @param mixed $id.
* @return self.
*/
public function clear($id = null);
/**
* @acces public.
* @return string.
*/
public function toString();
}
?>
Cache\Driver\FileCache.php:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
namespace Oetzie\Framework\Cache\Driver;
use Oetzie\Framework\Cache\Exception as Exception;
class FileCache extends DriverAbstract {
/**
* @acces protected.
* @var string.
*/
protected $fileHash = 'md5';
/**
* @acces protected.
* @var integer.
*/
protected $filePermissions = 0660;
/**
* @acces protected.
* @var string.
*/
protected $fileExtension = '';
/**
* @acces protected.
* @var string.
*/
protected $fileIdentifier = 'cache_';
/**
* @acces protected.
* @var integer.
*/
protected $pathPermissions = 0770;
/**
* @acces protected.
* @var string.
*/
protected $path = '';
/**
* @acces public.
* @param array $options.
* @throws InvalidArgumentException.
*/
public function __construct($options = array()) {
if (!is_array($options)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), options is not an array; recieved "%s".', __CLASS__, __FUNCTION__, gettype($options)));
} else {
$options = array_merge(array(
'file_hash' => $this->getFileHash(),
'file_permissions' => $this->getFilePermissions(),
'file_extension' => $this->getFileExtension(),
'file_identifier' => $this->getFileIdentifier(),
'path_permissions' => $this->getPathPermissions(),
'path' => $this->getPath(),
'expiration' => $this->getExpiration()
), $options);
$this->setFileHash($options['file_hash'])
->setFilePermissions($options['file_permissions'])
->setFileExtension($options['file_extension'])
->setFileIdentifier($options['file_indentifier'])
->setPathPermissions($options['path_permissions'])
->setPath($options['path'])
->setExpiration($options['expiration']);
}
}
/**
* @acces public.
*/
public function __destruct() {
}
/**
* @acces public.
* @param integer $permissions.
* @return self.
*/
public function setFilePermissions($permissions) {
$this->filePermissions = $permissions;
return $this;
}
/**
* @acces public.
* @return integer.
*/
public function getFilePermissions() {
return $this->filePermissions;
}
/**
* @acces public.
* @param string $function.
* @throws RuntimeException.
* @return self.
*/
public function setFileHash($hash) {
if (!function_exists($hash)) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), key hash function %s does not exists.', __CLASS__, __FUNCTION__, $hash));
}
$this->fileHash = $hash;
return $this;
}
/**
* @acces public.
* @return string.
*/
public function getFileHash() {
return $this->fileHash;
}
/**
* @acces public.
* @param string $identifier.
* @return self.
*/
public function setFileIdentifier($identifier) {
$this->fileIdentifier = $identifier;
return $this;
}
/**
* @acces public.
* @retun string.
*/
public function getFileIdentifier() {
return $this->fileIdentifier;
}
/**
* @acces public.
* @param string $extension.
* @retun self.
*/
public function setFileExtension($extension) {
$this->fileExtension = $extension;
return $this;
}
/**
* @acces public.
* @retun string.
*/
public function getFileExtension() {
return $this->fileExtension;
}
/**
* @acces public.
* @param integer $permissions.
* @return self.
*/
public function setPathPermissions($permissions) {
$this->pathPermissions = $permissions;
return $this;
}
/**
* @acces public.
* @return integer.
*/
public function getPathPermissions() {
return $this->pathPermissions;
}
/**
* @acces public.
* @param string $path.
* @throws RuntimeException|InvalidArgumentException.
* @return self.
*/
public function setPath($path) {
$path = rtrim($path, '\\/').DIRECTORY_SEPARATOR;
if (!is_dir($path)) {
if (!mkdir($path, $this->getPathPermissions(), true)) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), could not create cache path "%s" with permissions %s.', __CLASS__, __FUNCTION__, $path, $this->getPathPermissions()));
}
}
if (!is_dir($path)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), cache path "%s" does not exists.', __CLASS__, __FUNCTION__, $path));
} else if (!is_writeable($path)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), cache path "%s" is not writeable.', __CLASS__, __FUNCTION__, $path));
} else {
$this->path = $path;
}
return $this;
}
/**
* @acces public.
* @return string.
*/
public function getPath() {
return $this->path;
}
/**
* @acces protected.
* @param string $id.
* @return string.
*/
protected function makePath($id = null) {
if (null === $id) {
return $this->getPath().$this->getFileIdentifier().'*'.$this->getFileExtension();
} else {
return $this->getPath().$this->getFileIdentifier().hash($this->getFileHash(), strtolower($id)).$this->getFileExtension();
}
}
/**
* @acces public.
* @param string $id.
* @param mixed $data.
* @param null|integer $expiration.
* @throws RuntimeException.
* @returns self.
*/
public function add($id, $data, $expiration = null) {
$path = $this->makePath($id);
$data = serialize(array(
'created' => date('d-m-Y H:i:s'),
'expiration' => time() + (null === $expiration ? $this->getExpiration() : $expiration),
'data' => $data
));
if (!file_put_contents($path, $data, LOCK_EX)) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), id "%s" could not be writen to cache.', __CLASS__, __FUNCTION__, $id));
} else if (!chmod($path, $this->getFilePermissions())) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), id "%s" could not be writed to cache with permission "%s".', __CLASS__, __FUNCTION__, $id, $this->getFilePermissions()));
}
return $this;
}
/**
* @acces public.
* @param string $id.
* @param null|string $return.
* @throws RuntimeException.
* @return mixed.
*/
public function get($id, $return = 'data') {
$path = $this->makePath($id);
if (is_file($path)) {
if (!$data = file_get_contents($path)) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), id "%s" could not be read from cache.', __CLASS__, __FUNCTION__, $id));
} else {
$data = unserialize($data);
if (time() > $data['expiration']) {
$this->remove($id);
}
if (isset($data[$return])) {
return $data[$return];
} else {
return $data;
}
}
}
return false;
}
/**
* @acces public.
* @param string $id.
* @return self.
*/
public function remove($id) {
$path = $this->makePath($id);
if (is_file($path)) {
unlink($path);
}
return $this;
}
/**
* @acces public.
* @param mixed $id.
* @return self.
*/
public function clear($id = null) {
if (null === $id) {
$path = $this->makePath();
foreach (glob($path) as $key) {
unlink($key);
}
} else if (1 < func_num_args()) {
foreach (func_get_args() as $key) {
$this->clear($key);
}
} else if (is_string($id)) {
$this->remove($id)
} else if (is_array($id)) {
foreach ($id as $key) {
$this->remove($key);
}
)
return $this;
}
/**
* @acces public.
* @return string.
*/
public function toString() {
return 'FileCache';
}
}
?>
namespace Oetzie\Framework\Cache\Driver;
use Oetzie\Framework\Cache\Exception as Exception;
class FileCache extends DriverAbstract {
/**
* @acces protected.
* @var string.
*/
protected $fileHash = 'md5';
/**
* @acces protected.
* @var integer.
*/
protected $filePermissions = 0660;
/**
* @acces protected.
* @var string.
*/
protected $fileExtension = '';
/**
* @acces protected.
* @var string.
*/
protected $fileIdentifier = 'cache_';
/**
* @acces protected.
* @var integer.
*/
protected $pathPermissions = 0770;
/**
* @acces protected.
* @var string.
*/
protected $path = '';
/**
* @acces public.
* @param array $options.
* @throws InvalidArgumentException.
*/
public function __construct($options = array()) {
if (!is_array($options)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), options is not an array; recieved "%s".', __CLASS__, __FUNCTION__, gettype($options)));
} else {
$options = array_merge(array(
'file_hash' => $this->getFileHash(),
'file_permissions' => $this->getFilePermissions(),
'file_extension' => $this->getFileExtension(),
'file_identifier' => $this->getFileIdentifier(),
'path_permissions' => $this->getPathPermissions(),
'path' => $this->getPath(),
'expiration' => $this->getExpiration()
), $options);
$this->setFileHash($options['file_hash'])
->setFilePermissions($options['file_permissions'])
->setFileExtension($options['file_extension'])
->setFileIdentifier($options['file_indentifier'])
->setPathPermissions($options['path_permissions'])
->setPath($options['path'])
->setExpiration($options['expiration']);
}
}
/**
* @acces public.
*/
public function __destruct() {
}
/**
* @acces public.
* @param integer $permissions.
* @return self.
*/
public function setFilePermissions($permissions) {
$this->filePermissions = $permissions;
return $this;
}
/**
* @acces public.
* @return integer.
*/
public function getFilePermissions() {
return $this->filePermissions;
}
/**
* @acces public.
* @param string $function.
* @throws RuntimeException.
* @return self.
*/
public function setFileHash($hash) {
if (!function_exists($hash)) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), key hash function %s does not exists.', __CLASS__, __FUNCTION__, $hash));
}
$this->fileHash = $hash;
return $this;
}
/**
* @acces public.
* @return string.
*/
public function getFileHash() {
return $this->fileHash;
}
/**
* @acces public.
* @param string $identifier.
* @return self.
*/
public function setFileIdentifier($identifier) {
$this->fileIdentifier = $identifier;
return $this;
}
/**
* @acces public.
* @retun string.
*/
public function getFileIdentifier() {
return $this->fileIdentifier;
}
/**
* @acces public.
* @param string $extension.
* @retun self.
*/
public function setFileExtension($extension) {
$this->fileExtension = $extension;
return $this;
}
/**
* @acces public.
* @retun string.
*/
public function getFileExtension() {
return $this->fileExtension;
}
/**
* @acces public.
* @param integer $permissions.
* @return self.
*/
public function setPathPermissions($permissions) {
$this->pathPermissions = $permissions;
return $this;
}
/**
* @acces public.
* @return integer.
*/
public function getPathPermissions() {
return $this->pathPermissions;
}
/**
* @acces public.
* @param string $path.
* @throws RuntimeException|InvalidArgumentException.
* @return self.
*/
public function setPath($path) {
$path = rtrim($path, '\\/').DIRECTORY_SEPARATOR;
if (!is_dir($path)) {
if (!mkdir($path, $this->getPathPermissions(), true)) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), could not create cache path "%s" with permissions %s.', __CLASS__, __FUNCTION__, $path, $this->getPathPermissions()));
}
}
if (!is_dir($path)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), cache path "%s" does not exists.', __CLASS__, __FUNCTION__, $path));
} else if (!is_writeable($path)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), cache path "%s" is not writeable.', __CLASS__, __FUNCTION__, $path));
} else {
$this->path = $path;
}
return $this;
}
/**
* @acces public.
* @return string.
*/
public function getPath() {
return $this->path;
}
/**
* @acces protected.
* @param string $id.
* @return string.
*/
protected function makePath($id = null) {
if (null === $id) {
return $this->getPath().$this->getFileIdentifier().'*'.$this->getFileExtension();
} else {
return $this->getPath().$this->getFileIdentifier().hash($this->getFileHash(), strtolower($id)).$this->getFileExtension();
}
}
/**
* @acces public.
* @param string $id.
* @param mixed $data.
* @param null|integer $expiration.
* @throws RuntimeException.
* @returns self.
*/
public function add($id, $data, $expiration = null) {
$path = $this->makePath($id);
$data = serialize(array(
'created' => date('d-m-Y H:i:s'),
'expiration' => time() + (null === $expiration ? $this->getExpiration() : $expiration),
'data' => $data
));
if (!file_put_contents($path, $data, LOCK_EX)) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), id "%s" could not be writen to cache.', __CLASS__, __FUNCTION__, $id));
} else if (!chmod($path, $this->getFilePermissions())) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), id "%s" could not be writed to cache with permission "%s".', __CLASS__, __FUNCTION__, $id, $this->getFilePermissions()));
}
return $this;
}
/**
* @acces public.
* @param string $id.
* @param null|string $return.
* @throws RuntimeException.
* @return mixed.
*/
public function get($id, $return = 'data') {
$path = $this->makePath($id);
if (is_file($path)) {
if (!$data = file_get_contents($path)) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), id "%s" could not be read from cache.', __CLASS__, __FUNCTION__, $id));
} else {
$data = unserialize($data);
if (time() > $data['expiration']) {
$this->remove($id);
}
if (isset($data[$return])) {
return $data[$return];
} else {
return $data;
}
}
}
return false;
}
/**
* @acces public.
* @param string $id.
* @return self.
*/
public function remove($id) {
$path = $this->makePath($id);
if (is_file($path)) {
unlink($path);
}
return $this;
}
/**
* @acces public.
* @param mixed $id.
* @return self.
*/
public function clear($id = null) {
if (null === $id) {
$path = $this->makePath();
foreach (glob($path) as $key) {
unlink($key);
}
} else if (1 < func_num_args()) {
foreach (func_get_args() as $key) {
$this->clear($key);
}
} else if (is_string($id)) {
$this->remove($id)
} else if (is_array($id)) {
foreach ($id as $key) {
$this->remove($key);
}
)
return $this;
}
/**
* @acces public.
* @return string.
*/
public function toString() {
return 'FileCache';
}
}
?>
Gewijzigd op 23/04/2013 13:17:20 door Joakim Broden
Er zijn nog geen reacties op dit bericht.