forked from phpcoinn/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.php
More file actions
107 lines (92 loc) · 2.82 KB
/
Cache.php
File metadata and controls
107 lines (92 loc) · 2.82 KB
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
<?php
class Cache
{
public static $path;
private static $enabled = false;
static function init() {
self::$path = ROOT . "/tmp/cache";
if(!file_exists(self::$path)) {
self::$enabled = mkdir(self::$path, 0777);
@chown(self::$path, "www-data");
@chgrp(self::$path, "www-data");
} else {
self::$enabled = true;
}
// _log("Cache: Init caching enabled=".self::$enabled, 5);
}
static function getCacheFile($key){
return self::$path . "/" . $key;
}
static function get($key, $default = null) {
$cache_file = self::getCacheFile($key);
// _log("Cache:get $key file=$cache_file exists=".file_exists($cache_file)." callable=".is_callable($default), 5);
if(file_exists($cache_file)) {
$content = file_get_contents($cache_file);
$res = json_decode($content, true);
if (json_last_error() === JSON_ERROR_NONE) {
return $res;
}
}
if(is_callable($default)) {
$res = call_user_func($default);
return $res;
} else {
return $default;
}
}
static function set($key, $value) {
$cache_file = self::getCacheFile($key);
$value = json_encode($value);
$res = file_put_contents($cache_file, $value, LOCK_EX);
// _log("Cache:set $key file=$cache_file value=$value res=$res", 5);
}
static function exists ($key) {
$cache_file = self::getCacheFile($key);
return file_exists($cache_file);
}
static function remove($key) {
$cache_file = self::getCacheFile($key);
if(file_exists($cache_file)) {
unlink($cache_file);
}
}
public static function clearOldFiles()
{
$cmd = "find ".self::$path." -mtime +1 -exec ls -al {} +";
shell_exec($cmd);
}
public static function resetCache() {
$cmd = "rm -rf ".self::$path = ROOT . "/tmp/cache";
shell_exec($cmd);
}
static function syncStore($key,$ttl,$fn) {
$cache_file = self::getCacheFile($key);
return synchronized("cache-$key", function() use ($cache_file,$ttl,$fn) {
$expireTime = time() + $ttl;
if(is_callable($fn)) {
$data=call_user_func($fn);
}
$cacheData = json_encode(['expire' => $expireTime, 'data' => $data]);
file_put_contents($cache_file, $cacheData, LOCK_EX);
return $data;
});
}
static function getTempCache($key,$ttl,$fn) {
$cache_file = self::getCacheFile($key);
if (file_exists($cache_file)) {
$cacheData = json_decode(file_get_contents($cache_file), true);
if ($cacheData) {
if($cacheData['expire'] >= time()) {
return $cacheData['data'];
}
self::syncStore($key, $ttl, $fn);
return $cacheData['data'];
} else {
return self::syncStore($key, $ttl, $fn);
}
} else {
return self::syncStore($key, $ttl, $fn);
}
}
}
Cache::init();