1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpCache\CacheServer; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Description of ActionHandler. |
7
|
|
|
* |
8
|
|
|
* @author dude920228 |
9
|
|
|
*/ |
10
|
|
|
class ActionHandler |
11
|
|
|
{ |
12
|
|
|
public function __invoke($data, $bucket, $ioHandler, $connection, $server) |
13
|
|
|
{ |
14
|
|
|
$action = $data['action']; |
15
|
|
|
$functionName = 'handle'.ucfirst($action); |
16
|
|
|
if (!method_exists($this, $functionName)) { |
17
|
|
|
return false; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
return call_user_func_array([$this, $functionName], [$data, $bucket, $ioHandler, $connection, $server]); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
private function handleSet($data, $bucket, $ioHandler, $connection, $server) |
24
|
|
|
{ |
25
|
|
|
$package = $data['message']; |
26
|
|
|
$success = $bucket->store($data['key'], $package); |
27
|
|
|
|
28
|
|
|
return $success; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private function handleGet($data, $bucket, $ioHandler, $connection, $server) |
32
|
|
|
{ |
33
|
|
|
$key = $data['key']; |
34
|
|
|
$package = $bucket->get($key); |
35
|
|
|
if ($package === false) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
$dataToSend = serialize($package); |
39
|
|
|
$ioHandler->writeToSocket($connection, $dataToSend); |
40
|
|
|
|
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function handleDelete($data, $bucket, $ioHandler, $connection, $server) |
45
|
|
|
{ |
46
|
|
|
$key = $data['key']; |
47
|
|
|
$success = $bucket->delete($key); |
48
|
|
|
|
49
|
|
|
return $success; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function handleGetEntries($data, $bucket, $ioHandler, $connection) |
53
|
|
|
{ |
54
|
|
|
$entries = $bucket->getEntries(); |
55
|
|
|
$entriesFormatted = []; |
56
|
|
|
foreach ($entries as $key => $value) { |
57
|
|
|
$entriesFormatted[$key] = gzuncompress($value['content']); |
58
|
|
|
} |
59
|
|
|
$dataToSend = serialize($entriesFormatted); |
60
|
|
|
$ioHandler->writeToSocket($connection, $dataToSend); |
61
|
|
|
|
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function handleQuit($data, $bucket, $ioHandler, $connection, $server) |
66
|
|
|
{ |
67
|
|
|
$server->beforeServiceStop(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|