-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacheToolbox.php
165 lines (135 loc) · 4.33 KB
/
cacheToolbox.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
<?php
/**
* by Antoine Cadoret (@jacknumber)
*/
class CacheToolbox extends Memcached
{
private $host = 'localhost';
private $port = 11211;
public function run()
{
// take arguments
global $argv;
$this->addServer($this->host, $this->port);
$key = (empty($argv[2])) ? '' : $argv[2];
$data = (empty($argv[3])) ? '' : $argv[3];
switch ($argv[1]) {
case 'help':
$this->_help();
break;
case 'flush':
$this->flush();
break;
case 'get':
if (!$key) {
print 'key missing' . "\n";
break;
}
print_r($this->get($key));
print "\n";
break;
case 'getAllKeys':
$this->_getAllKeys();
break;
case 'delete':
if (!$key) {
print 'key missing' . "\n";
break;
}
$this->delete($key);
if ($this->get($key)) {
print '/!\ ' . $key . ' not deleted' . "\n";
}
break;
case 'deleteAllKeys':
$keys = $this->getAllKeys();
foreach ($keys as $key) {
$this->delete($key);
if ($this->get($key)) {
print '/!\ ' . $key . ' not deleted' . "\n";
}
}
break;
case 'set':
if (!$key) {
print 'key missing' . "\n";
break;
}
$this->set($key, $data);
print 'Stored: '. $key . ' => ';
print_r($this->get($key));
print "\n";
break;
case 'increment':
if (!$key) {
print 'key missing' . "\n";
break;
}
$this->increment($key);
print 'New value: ' . $this->get($key) . "\n";
break;
case 'decrement':
if (!$key) {
print 'key missing' . "\n";
break;
}
$this->decrement($key);
print 'New value: ' . $this->get($key) . "\n";
break;
case 'stats':
print_r($this->getStats());
print "\n";
break;
default:
print 'Unknown command. Use "help" ;)' . "\n";
break;
}
}
private function _help() {
// echo "stats" | nc -w 1 localhost 11211 | awk '$2 == "bytes" { print $2" "$3 }'
$help = [
'flush' => 'Invalidate all keys',
'get <key>' => 'Return the key value',
'getAllKeys' => 'List keys on the server',
'delete <key>' => 'Delete the key',
'deleteAllKeys' => 'Delete all the key',
'set <key> <value>' => 'Set the key with the value',
'increment <key>' => 'Add 1 to the key',
'decrement <key>' => 'Remove 1 to the key',
'stats' => 'Show some stats about Memcached',
];
print "\n\n";
print '== Help of cache toolbox ==' . "\n";
print "\n\n";
print 'Engine:' . "\t" . 'memcached' . "\n";
print 'Host: ' . "\t" . $this->host . "\n";
print 'Port: ' . "\t" . $this->port . "\n";
print "\n";
print 'Commands:' . "\n";
foreach ($help as $command => $desc) {
$separator = "\t";
if (strlen($command) < 15) {
$separator .= "\t";
}
if (strlen($command) < 7) {
$separator .= "\t";
}
print $command . $separator . $desc . "\n";
}
}
private function _getAllKeys() {
$keys = $this->getAllKeys();
if ($keys) {
foreach ($keys as $key => $value) {
print $value . "\n";
}
} else {
// no keys
print 'no keys' . "\n";
}
}
}
if (php_sapi_name() == 'cli') {
$obj = new CacheToolbox();
echo $obj->run();
}