-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache.php
95 lines (84 loc) · 2.63 KB
/
Cache.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
<?php
namespace infrajs\cache;
use infrajs\mem\Mem;
use infrajs\access\Access;
use infrajs\path\Path;
use infrajs\nostore\Nostore;
/*
Cache::exec(true,'somefn',array($arg1,$arg2)); - выполняется всегда
Cache::exec(true,'somefn',array($arg1,$arg2),$data); - Установка нового значения в кэше
*/
class Cache
{
public static $once = array();
public static function fullrmdir($delfile, $ischild = false)
{
return Path::fullrmdir($delfile, $ischild);
}
public static function execF($conds, $name, $fn, $args = array(), $re = false)
{
return Cache::exec($conds, $name, $fn, $args, $re, true);
}
public static function exec($conds, $name, $fn, $args = array(), $re = false, $savetofile = false)
{
$hash = json_encode($args, JSON_UNESCAPED_UNICODE);
$key = $name . $hash;
if (isset(Cache::$once[$key])) {
if (Cache::$once[$key]['nostore']) Nostore::on();
return Cache::$once[$key]['result'];
}
$data = Mem::get($key, $savetofile);
if (!$data) $data = array('time' => 0);
$execute = Access::adminIsTime($data['time'], function ($cache_time) use ($conds) {
if (!sizeof($conds)) {
return false; //Если нет conds кэш навсегда и develop не поможет
}
$max_time = 1;
for ($i = 0, $l = sizeof($conds); $i < $l; $i++) {
$mark = $conds[$i];
if ($mark === true) return true;
$mark = Path::theme($mark);
if (!$mark) {
continue;
}
$m = filemtime($mark);
if ($m > $max_time) {
$max_time = $m;
}
if (!is_dir($mark)) {
continue;
}
foreach (glob($mark . '*.*') as $filename) {
$m = filemtime($filename);
if ($m > $max_time) {
$max_time = $m;
}
}
}
return $max_time > $cache_time;
}, $re);
if ($execute) {
$is = Nostore::check(function () use (&$data, $fn, $args, $re) { //Проверка был ли запрет кэша
$data['result'] = call_user_func_array($fn, array_merge($args, array($re)));
});
if (!$is && !$re) { //При $re кэш не сохраняется. Это позволяет запустит Cache::exec до установи расширений в Search
$data['time'] = time();
Mem::set($key, $data, $savetofile);
} else {
Mem::delete($key);
}
} else {
$is = false;
}
Cache::$once[$key] = ['result' => $data['result'], 'nostore' => $is];
return Cache::$once[$key]['result'];
}
public static function clear($name, $args = array())
{
$hash = json_encode($args, JSON_UNESCAPED_UNICODE);
$key = $name . $hash;
if (isset(Cache::$once[$key])) unset(Cache::$once[$key]);
Mem::delete($key);
return $hash;
}
}