-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpulpCache.php
More file actions
150 lines (128 loc) · 3.19 KB
/
pulpCache.php
File metadata and controls
150 lines (128 loc) · 3.19 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
class PulpCache {
private $config = array('cache_dir' => "cache", "config_file" => "config.json", "expire_at" => 0, "cache_file" => 'empty.tmp', "cache_name" => "cache",'ttl' => 86400);
public function __construct($config = null) {
$this -> configure($config);
$this -> init();
}
private function configure($config) {
if (is_array($config)) {
$this -> config = array_merge($this -> config, $config);
}
if ($this -> getConfig()) {
$this -> config = array_merge($this -> config, $this -> getConfig());
}
}
private function init() {
if (!is_dir($this -> getDir())) {
$this -> createDir();
}
}
private function getDir() {
return $this -> config['cache_dir'] . "/";
}
/**
* Checks if the Cache is Fresh
* @return boolean
*/
private function isFresh() {
return ($this -> config['expire_at'] > time());
}
/**
* Returns the cached Content if available
* @return string
*/
public function getCache() {
if ($this -> hasCache()) {
return $this -> getCacheFile();
}
return false;
}
/**
* Returns true if there is fresh cached Content
* @return boolean
*/
public function hasCache() {
return $this -> isFresh() && is_file($this -> getCachePath());
}
/**
* Deletes old cached Content and saves new Cache
* @return boolean
*/
public function saveCache($data) {
if ($this -> getCacheFile()) {
unlink($this -> getCachePath());
}
$this -> config['cache_file'] = sha1(md5(time()));
$this -> config['expire_at'] = time() + $this -> config['ttl'];
$this -> save($this -> getCachePath(), $data);
$this -> saveConfig();
return true;
}
/**
* Returns the Directory of the cached File
* @return string
*/
private function getCachePath() {
return $this -> getDir() . $this -> config['cache_file'];
}
/**
* Returns the content of the Cached File
* @return string
*/
private function getCacheFile() {
$file = $this -> getCachePath();
if (!is_file($file)) {
return false;
}
return $this -> load($file);
}
/**
* Creates the cache Directory
*/
public function createDir() {
mkdir($this -> getDir());
}
/*** CONFIG ***/
/**
* Returns the Config for the current Cache Name
* @return array;
*/
private function getConfig() {
if (!is_file($this -> getConfigFile()))
return false;
$config = $this -> getConfigRaw();
if (!isset($config[$this -> config['cache_name']]))
return array();
return $config[$this -> config['cache_name']];
}
private function getConfigRaw() {
$config = json_decode($this -> load($this -> getConfigFile()), true);
if (is_array($config))
return $config;
return array();
}
private function saveConfig() {
$config = array_merge($this -> getConfigRaw(), array($this -> config['cache_name'] => $this -> config));
$this -> save($this -> getConfigFile(), json_encode($config));
return true;
}
private function getConfigFile() {
return $this -> getDir() . $this -> config["config_file"];
}
/** UTILITIES **/
/**
* Loads a File
* @return string
*/
private function load($file) {
return file_get_contents($file);
}
/**
* Saves a File
* @return boolean
*/
private function save($filename, $data) {
return file_put_contents($filename, $data);
}
}