-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.Cache.php
More file actions
65 lines (45 loc) · 1.51 KB
/
Copy pathclass.Cache.php
File metadata and controls
65 lines (45 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
<?php
class Cache {
private $CACHE_DIR;
private $CACHE_EXT;
private $CACHE_HOURS;
private $CACHE_LIFE;
public function __construct() {
$this->CACHE_DIR = dirname(__FILE__).'/cache/';
$this->CACHE_EXT = '.tim';
$this->CACHE_HOURS = 3;
$this->CACHE_LIFE = $this->CACHE_HOURS * 3600;
}
public function writeCache($name, $content) {
$cacheFile = cacheLocation($name);
$file = fopen($cacheFile,'w');
fwrite($file, $content);
fclose($file);
}
public function readCache($name) {
$cacheFile = cacheLocation($name);
if (!file_exists($cacheFile)) { return null; }
$content = file_get_contents(($cacheFile));
return $content;
}
public function needUpdate($name) {
$cacheFile = cacheLocation($name);
if (!file_exists($cacheFile)) { return true; }
if (time() - filemtime($cacheFile) > $this->CACHE_LIFE) { return true; }
return false;
}
private function cleanName($name) {
$local = str_replace('https://', '', $name);
$local = str_replace('http://', '', $local);
$local = str_replace('www.', '', $local);
$local = str_replace('/', '_', $local);
$local = str_replace(' ', '_', $local);
return $name;
}
private function cacheLocation($name) {
$name = cleanName($name);
$cacheFile = $this->CACHE_DIR.$name.$this->CACHE_EXT;
return $cacheFile;
}
}
?>