-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileCache.php
53 lines (41 loc) · 1.13 KB
/
FileCache.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
<?php
/**
* File Cache
* @author Gökhan Kaya <[email protected]>
*/
class FileCache {
const CACHE_DIR = './cache/';
public static function get($key) {
$file = self::getFileName($key);
if (!is_file($file)) {
return false;
}
list($lifetime, $value) = unserialize(
file_get_contents($file)
);
if ($lifetime !== 0 &&
time() - filemtime($file) > $lifetime
) {
@unlink($file);
return false;
}
return $value;
}
public static function set($key, $value, $lifetime = 0) {
if (!is_dir(self::CACHE_DIR)) {
if (!mkdir(self::CACHE_DIR, 0777, true)) {
return false;
}
}
$file = self::getFileName($key);
$data = serialize([$lifetime, $value]);
return (bool) file_put_contents($file, $data);
}
public static function delete($key) {
$file = self::getFileName($key);
return @unlink($file);
}
private static function getFileName($key) {
return self::CACHE_DIR . '/' . md5($key) . '.cache';
}
}