|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpCache\CacheServer; |
|
4
|
|
|
|
|
5
|
|
|
use PhpCache\CacheEventListener\CacheEventListenerInterface; |
|
6
|
|
|
use PhpCache\IO\CacheIOHandler; |
|
7
|
|
|
use PhpCache\Storage\Bucket; |
|
8
|
|
|
use PhpCache\Storage\Maintainer; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Description of CacheServer. |
|
12
|
|
|
* |
|
13
|
|
|
* @author dude920228 |
|
14
|
|
|
*/ |
|
15
|
|
|
class CacheServer implements CacheServerInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private $socket; |
|
18
|
|
|
private $running; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var CacheIOHandler |
|
22
|
|
|
*/ |
|
23
|
|
|
private $ioHandler; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Bucket |
|
27
|
|
|
*/ |
|
28
|
|
|
private $bucket; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ActionHandler |
|
32
|
|
|
*/ |
|
33
|
|
|
private $actionHandler; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Maintainer |
|
37
|
|
|
*/ |
|
38
|
|
|
private $maintainer; |
|
39
|
|
|
|
|
40
|
|
|
private $cacheEventListener; |
|
41
|
|
|
|
|
42
|
|
|
private $clients; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct( |
|
45
|
|
|
CacheIOHandler $ioHandler, |
|
46
|
|
|
Bucket $bucket, |
|
47
|
|
|
ActionHandler $actionHandler, |
|
48
|
|
|
Maintainer $maintainer, |
|
49
|
|
|
CacheEventListenerInterface $cacheEventListener = null |
|
50
|
|
|
) { |
|
51
|
|
|
$this->running = true; |
|
52
|
|
|
$this->ioHandler = $ioHandler; |
|
53
|
|
|
$this->bucket = $bucket; |
|
54
|
|
|
$this->actionHandler = $actionHandler; |
|
55
|
|
|
$this->maintainer = $maintainer; |
|
56
|
|
|
$this->clients = []; |
|
57
|
|
|
$this->cacheEventListener = $cacheEventListener; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function run(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->socket = $this->ioHandler->createServerSocket(); |
|
63
|
|
|
while ($this->running) { |
|
64
|
|
|
$this->maintainer->checkBackup(time(), $this->bucket); |
|
65
|
|
|
$this->maintainer->maintainBucket($this->bucket); |
|
66
|
|
|
if (($connection = @socket_accept($this->socket))) { |
|
67
|
|
|
$clientId = uniqid(); |
|
68
|
|
|
socket_set_nonblock($connection); |
|
69
|
|
|
$this->clients[$clientId] = $connection; |
|
70
|
|
|
$read = $this->clients; |
|
71
|
|
|
$write = []; |
|
72
|
|
|
$except = []; |
|
73
|
|
|
socket_select($read, $write, $except, 10); |
|
74
|
|
|
$dataString = $this->ioHandler->readFromSocket($connection); |
|
75
|
|
|
$data = unserialize($dataString); |
|
76
|
|
|
($this->actionHandler)( |
|
77
|
|
|
$this, |
|
78
|
|
|
$data, |
|
79
|
|
|
$connection |
|
80
|
|
|
); |
|
81
|
|
|
$this->ioHandler->closeSocket($connection); |
|
82
|
|
|
unset($this->clients[$clientId]); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getBucket(): Bucket |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->bucket; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getIOHandler(): CacheIOHandler |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->ioHandler; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getMaintainer(): Maintainer |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->maintainer; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function getSocket() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->socket; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getEventListener(): ?CacheEventListenerInterface |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->cacheEventListener; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function close(): void |
|
113
|
|
|
{ |
|
114
|
|
|
$this->maintainer->backup($this->bucket); |
|
115
|
|
|
$this->running = false; |
|
116
|
|
|
$this->ioHandler->closeSocket($this->socket); |
|
117
|
|
|
$this->ioHandler->removeSocket(); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|