-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSafeCache.class.php
243 lines (173 loc) · 5.21 KB
/
SafeCache.class.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/*
*/
class SafeCache {
private $work_dir;
private $_WLock;
private $_RLock;
private $content_fp;
// persistent counter/etc.
// private $seen = array();
private $isReadyToRead = false;
public $publisher;
public $file; // string id
public $temp_file;
private $hash_id;
private $opt;
function __construct ($id, $arg=array()) {
$this->file = $id;
$this->temp_file = $id .".temp";
$this->hash_id = md5($id);
$this->opt = $arg + array(
"work_dir" => "/tmp/safe_resource_access",
"work_dir_perm" => 0755,
"cacheTTL" => 3*60, // sec; cache expiration for readers
"diffTTL" => 15, // sec; for publisher cache expires after cacheTTL-diffTTL
"clearstatcache" => true,
);
$this->work_dir = $this->opt["work_dir"];
// mkdir recursive
if (! is_dir($this->work_dir) ) mkdir($this->work_dir, $this->opt["work_dir_perm"], true);
}
// call user func or inherit this method
function hasResourceExpired () {
$func = $this->opt["hasResourceExpired"];
if ($func) return $func($this);
$cacheTTL = $this->opt["cacheTTL"];
if ($this->publisher) $cacheTTL -= $this->opt["diffTTL"];
if ($this->opt["clearstatcache"]) clearstatcache();
$filemtime = file_exists($this->file) ? filemtime($this->file) : 0;
return ($filemtime + $cacheTTL < time());
}
// call user func or inherit this method
function newContent () {
$func = $this->opt["newContent"];
if (!$func) return false;
// $fp = $this->get_temp_fp();
$fp = fopen($this->temp_file, "wb");
if (!$fp) return false;
$content = $func($this, $fp);
if (isset($content) and $content !== false) fwrite($fp, $content);
return fclose($fp);
}
// print static content
function output () {
$this->ready();
$ok = readfile($this->file) !== false;
$this->finish();
return $ok;
}
// include php file
function include_php () {
$this->ready();
include $this->file;
$this->finish();
}
// get file contents
function get () {
$this->ready();
$ret = file_get_contents($this->file);
$this->finish();
return $ret;
}
// get file handle
function getHandle ($force=false) {
$this->ready();
if (!$this->content_fp or $force) {
$this->content_fp = fopen($this->file, "r");
}
// $this->finish();
return $this->content_fp;
}
//
function ready () {
if ($this->isReadyToRead) return;
$this->publisher = $this->getExclusive();
if ($this->publisher) {
if ( $this->hasResourceExpired() ) {
// generate new content in $this->temp_file
$this->newContent();
// publish new content
$this->publish();
}
// do reading..
// $this->doneExclusive();
}
else {
if ( $this->hasResourceExpired() ) $this->waitForRefresh();
$this->waitForReading();
// do reading..
// $this->doneReading();
}
$this->isReadyToRead = true;
}
// manually release locks
function finish () {
$this->isReadyToRead = false;
$this->content_fp = null;
return $this->publisher ? $this->doneExclusive() : $this->doneReading();
}
// acquire ex. Wlock; nonblocking, return true|false
function getExclusive() {
return $this->getWLock($block=0);
}
// publish new content
function publish () {
// $this->waitForWriting();
$this->getRLock($exclusive=1);
rename($this->temp_file, $this->file);
// $this->doneWriting();
$this->doneReading();
}
// release sh. Rlock
function doneReading () {
$ok = $this->releaseLock($this->_RLock);
unset($this->_RLock);
return $ok;
}
// acquire and release ex Wlock
function waitForRefresh () {
$ok = $this->getWLock($block=1) && $this->doneExclusive();
return $ok;
}
// acquire sh. Rlock
function waitForReading () {
return $this->getRLock($exclusive=0);
}
// release ex. Wlock
function doneExclusive () {
$ok = $this->releaseLock($this->_WLock);
unset($this->_WLock);
return $ok;
}
//
function releaseLock($fp) {
if (!$fp) return;
$ok = flock($fp, LOCK_UN);
// fclose alone does unlock
$ok = fclose($fp);
return $ok;
}
// write lock is always exclusive (only my thread and nobody else)
function getWLock($block) {
$lock = $block ? LOCK_EX : LOCK_EX|LOCK_NB;
$lfile = $this->work_dir ."/". $this->hash_id .".wlock";
$fp = fopen($lfile, "wb");
if (!$fp) return false;
// get exclusive lock
$ok = flock($fp, $lock);
if ($ok) { $this->_WLock = $fp; } else { fclose($fp); }
return $ok;
}
// read lock is always blocking (always waiting for lock)
function getRLock($exclusive) {
$lock = $exclusive ? LOCK_EX : LOCK_SH;
$lfile = $this->work_dir ."/". $this->hash_id .".rlock";
$fp = fopen($lfile, "wb");
if (!$fp) return false;
// get shared (read) or ex. lock
$ok = flock($fp, $lock);
if ($ok) { $this->_RLock = $fp; } else { fclose($fp); }
return $ok;
}
}