-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.php
More file actions
96 lines (65 loc) · 2.62 KB
/
cache.php
File metadata and controls
96 lines (65 loc) · 2.62 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
<?php
/*
* This software is the property of its authors.
* See the copyright.txt file for more details.
*
*/
/**
* Static cache that is older than this timestamp is considered expired.
*/
function lastCodeChangeTimestamp () {
static $resultCache = 0;
static $resultCacheSet = false;
$timestampFile = $_SERVER['cachePath'] . '/lastCodeChangeTimestamp';
$correlationFile = "$timestampFile.correlation";
if (!$resultCacheSet) {
if (!is_file($timestampFile) || (version_development && microtime(true) > filemtime($timestampFile) + 2)) {
file_put_contents($timestampFile, codeTimestamp());
} else {
$resultCache = file_get_contents($timestampFile);
enforce($resultCache !== false, "Could not read '$timestampFile'");
$resultCache = (float) $resultCache;
}
$resultCacheSet = true;
}
return $resultCache > 1 ? $resultCache : codeTimestamp();
}
function staticCacheExpired ($key) {
if (!version_development)
return false;
static $resultCache = array();
if (!array_key_exists($key, $resultCache)) {
$timestampFile = $_SERVER['cachePath'] . '/staticCacheExpired_' . sha1($key) . '.timestamp';
$resultCache[$key] = !is_file($timestampFile) || filemtime($timestampFile) < lastCodeChangeTimestamp();
directory(dirname($timestampFile));
$touchResult = touch($timestampFile);
enforce($touchResult, "Could not touch '$timestampFile'");
}
return $resultCache[$key];
}
// staticCache($file, $line, [$key,] $callback)
function staticCache ($file, $line, $key, $callback = null) {
if (!isset($callback)) {
$callback = $key;
$key = null;
}
static $map;
if (!isset($map))
$map = (object) array();
if (isset($map->{"$file:$line-$key"}))
return $map->{"$file:$line-$key"};
$cacheFile = $_SERVER['cachePath'] . '/' . substr(pathinfo($file, PATHINFO_FILENAME), 0, 10) . '__'
. substr(preg_replace('/(?i)[^a-z0-9]+/', '', $key), 0, 10) . '__' . sha1("$file:$line-$key") . '.cache';
$cacheCorrelationFile = "$cacheFile.correlation";
if (version_development && is_file($cacheCorrelationFile))
foreach (array_diff(unserialize(file_get_contents($cacheCorrelationFile)), includedFile()) as $file)
if (is_file($file))
includedFile($file);
if (!is_file($cacheFile) || (version_development && filemtime($cacheFile) < lastCodeChangeTimestamp())) {
directory(dirname($cacheFile));
file_put_contents($cacheFile, serialize($callback()));
file_put_contents($cacheCorrelationFile, serialize(includedFile()));
}
$map->{"$file:$line-$key"} = unserialize(file_get_contents($cacheFile));
return $map->{"$file:$line-$key"};
}