This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.php
executable file
·98 lines (79 loc) · 2.12 KB
/
bootstrap.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
<?php
/**
* @file
* Addon bootstrap.
*/
// Check if Redis is configured.
if (!isset($app->config['redis']) || empty($app->config['redis']['host'])) {
return;
}
// Main module functions.
$this->module('rediscache')->extend([
'connect' => function() {
$settings = $this->app->config['redis'];
if (!$settings || !isset($settings['host'], $settings['port'])) {
return;
}
$redis = new Redis();
if ($redis->connect($settings['host'], $settings['port'])) {
$redis->setOption(Redis::OPT_PREFIX, $settings['prefix']);
return $redis;
}
},
'set' => function($key, $value, $ttl = 30) {
if ($redis = $this->connect()) {
$redis->set(trim($key), json_encode($value), ['px' => ($ttl * 1000)]);
}
},
'get' => function($key) {
if ($redis = $this->connect()) {
if ($data = $redis->get($key)) {
return json_decode($data);
}
}
},
'bypass' => function() {
$settings = $this->app->config['redis'];
if (empty($_REQUEST['_bypass'])) {
return FALSE;
}
if (empty($settings['bypass_token'])) {
return FALSE;
}
return ($_REQUEST['_bypass'] == $settings['bypass_token']);
},
'clearCache' => function() {
if ($redis = $this->connect()) {
$redis->flushDb();
}
},
]);
// Include admin.
if (COCKPIT_ADMIN && !COCKPIT_API_REQUEST) {
include_once __DIR__ . '/admin.php';
$this->module('rediscache')->extend([
'getInfo' => function() {
$config = $this->app->config['redis'];
$info = [];
$keys = [];
$count = 0;
$settings = $config;
$settings['cache_paths'] = [];
foreach ($config['cache_paths'] as $path => $ttl) {
$settings['cache_paths'][] = ['path' => $path, 'ttl' => $ttl];
}
if ($redis = $this->connect()) {
$keys = $redis->keys('*');
$count = $redis->dbSize();
foreach ($redis->info() as $key => $value) {
$info[] = ['key' => $key, 'value' => $value];
}
}
return compact('settings', 'info', 'count', 'keys');
},
]);
}
// Is REST?
if (COCKPIT_API_REQUEST) {
include_once __DIR__ . '/rest.php';;
}