-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.php
58 lines (41 loc) · 1.57 KB
/
api.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
<?php
$this->on('cockpit.rest.init', function($routes) {
$routes['rspc'] = 'SimpleResponseCache\\Controller\\RestApi';
});
$this->on('before', function() {
// Is cache request?
if (!$this->param('rspc') || preg_match('#^/api/rspc#i', $this['route'])) {
return;
}
$hash = trim($this['route'].'/'.md5(serialize($this->request->request)), '/').'.php';
$file = $this->path("#tmp:apicache/{$hash}");
if ($file) {
try {
$cache = include($file);
} catch(\Throwable $e) {
@unlink($file);
return;
}
if ($cache['eol'] < time()) {
@unlink($file);
return;
}
$this->response->headers[] = 'COCKPIT_RSP_CACHE: true';
$this->response->mime = $cache['mime'] ?? 'text/html';
$this->response->body = $cache['contents'];
$this->response->flush();
$this->trigger('simpleresponsecache.after');
$this->stop();
}
$this->on('after', function() {
if ($this->response->status != 200) {
return;
}
$hash = trim(COCKPIT_ADMIN_ROUTE.'/'.md5(serialize($this->request->request)), '/').'.php';
$this->filestorage->put("tmp://apicache/{$hash}", '<?php return '.var_export([
'mime' => $this->response->mime,
'eol' => (time() + $this->retrieve('config/responseCache/duration', 60)),
'contents' => is_object($this->response->body) ? json_decode(json_encode($this->response->body), true) : $this->response->body
], true ).';');
}, -2000);
}, 2000);