This repository was archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCache.php
More file actions
74 lines (65 loc) · 1.51 KB
/
Cache.php
File metadata and controls
74 lines (65 loc) · 1.51 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
<?php
/**
* SimpleTemplate - Cache
*
* @author Michal Vaněk
*/
namespace SimpleTemplate;
/** Init cache folder */
Cache::$cacheFolder = CACHE_PATH;
class Cache {
/** @var string */
public static $cacheFolder;
/** @var bool */
private static $enabled = true;
/** @var integer */
private static $cacheAge = 3600;
/**
* Cache settings.
* @param bool
*/
public static function setEnabled($bool){
self::$enabled = (bool)$bool;
}
/**
* Set cache folder.
* @param path
*/
public static function setFolder($folder){
self::$cacheFolder = $folder;
}
/**
* Try to load template cache file.
* @param hash template hash
* @return bool success/failure
*/
public static function loadTemplate($hash){
if(!self::$enabled) return false;
$cacheFile = self::$cacheFolder.$hash.".cache.tpl";
if(file_exists($cacheFile)) return file_get_contents($cacheFile);
return false;
}
/**
* Save template to cache file.
* @param hash template hash
* @return bool success/failure
*/
public static function saveTemplate($hash,$content){
if(!self::$enabled) return false;
$cacheFile = self::$cacheFolder.$hash.".cache.tpl";
$fp = fopen($cacheFile,"w");
fwrite($fp,$content);
fclose($fp);
return true;
}
/**
* Remove old template cache file
*/
public static function clearCacheFolder(){
foreach(scandir(self::$cacheFolder) AS $values){
if(strstr($values,".cache.tpl") && filemtime(self::$cacheFolder.$values)+self::$cacheAge < time()){
@unlink(self::$cacheFolder.$values);
}
}
}
}