|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UniMan\Drivers\Memcache; |
|
4
|
|
|
|
|
5
|
|
|
use UniMan\Core\Driver\AbstractDriver; |
|
6
|
|
|
use UniMan\Core\Exception\ConnectException; |
|
7
|
|
|
use UniMan\Drivers\Memcache\Forms\MemcacheCredentialsForm; |
|
8
|
|
|
use Memcache; |
|
9
|
|
|
|
|
10
|
|
|
class MemcacheDriver extends AbstractDriver |
|
11
|
|
|
{ |
|
12
|
|
|
const TYPE_KEY = 'key'; |
|
13
|
|
|
|
|
14
|
|
|
private $connection; |
|
15
|
|
|
|
|
16
|
2 |
|
public function extensions() |
|
17
|
|
|
{ |
|
18
|
2 |
|
return ['memcache']; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
2 |
|
public function type() |
|
22
|
|
|
{ |
|
23
|
2 |
|
return 'memcache'; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
2 |
|
public function defaultCredentials() |
|
27
|
|
|
{ |
|
28
|
|
|
return [ |
|
29
|
2 |
|
'servers' => 'localhost:11211', |
|
30
|
|
|
]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
public function getCredentialsForm() |
|
34
|
|
|
{ |
|
35
|
2 |
|
return new MemcacheCredentialsForm(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function connect(array $credentials) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->connection = new Memcache(); |
|
41
|
|
|
$servers = array_map('trim', explode("\n", trim($credentials['servers']))); |
|
42
|
|
|
foreach ($servers as $server) { |
|
43
|
|
|
list($host, $port) = explode(':', $server, 2); |
|
44
|
|
|
if (!$this->connection->addserver($host, $port)) { |
|
45
|
|
|
throw new ConnectException("Couldn't connect to $host:$port"); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function getFormManager() |
|
51
|
|
|
{ |
|
52
|
|
|
return new MemcacheFormManager($this->connection); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
protected function getHeaderManager() |
|
56
|
|
|
{ |
|
57
|
2 |
|
return new MemcacheHeaderManager(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
protected function getPermissions() |
|
61
|
|
|
{ |
|
62
|
2 |
|
return new MemcachePermissions(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function getDataManager() |
|
66
|
|
|
{ |
|
67
|
|
|
return new MemcacheDataManager($this->connection, $this->translator); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|