|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BFW\Memcache; |
|
4
|
|
|
|
|
5
|
|
|
use \Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class to manage connection to memcache(d) server with memcache lib |
|
9
|
|
|
*/ |
|
10
|
|
|
class Memcache extends \Memcache |
|
11
|
|
|
{ |
|
12
|
|
|
//Include traits to add some methods |
|
13
|
|
|
use \BFW\Traits\Memcache; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var \BFW\Application $app Instance of BFW Application |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $app; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array $config Config define on bfw config file for memcache(d) |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $config; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor. |
|
27
|
|
|
* Connect to servers |
|
28
|
|
|
* |
|
29
|
|
|
* @param \BFW\Application $app The BFW Application instance |
|
30
|
|
|
* |
|
31
|
|
|
* @throws Exception If PHP Version is >= 7.x (no memcache extension) |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(\BFW\Application $app) |
|
34
|
|
|
{ |
|
35
|
|
|
//Check php version. No memcache lib for >= 7.x |
|
36
|
|
|
if (PHP_VERSION_ID > 70000) { |
|
37
|
|
|
throw new Exception( |
|
38
|
|
|
'PHP Memcache Extension not supported for PHP 7' |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->app = $app; |
|
43
|
|
|
$this->config = $this->app->getConfig('memcached'); |
|
44
|
|
|
|
|
45
|
|
|
$this->connectToServers(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Connect to memcache(d) server defined on config |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function connectToServers() |
|
54
|
|
|
{ |
|
55
|
|
|
//Loop on declared server(s) |
|
56
|
|
|
foreach ($this->config['server'] as $server) { |
|
57
|
|
|
$this->getServerInfos($server); |
|
58
|
|
|
|
|
59
|
|
|
$host = $server['host']; |
|
60
|
|
|
$port = $server['port']; |
|
61
|
|
|
$timeout = $server['timeout']; |
|
62
|
|
|
$persistent = $server['persistent']; |
|
63
|
|
|
|
|
64
|
|
|
//Not checked if port = (int) 0; Doc said to define to 0 for socket |
|
65
|
|
|
if (empty($host) || $port === null) { |
|
66
|
|
|
continue; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
//Change method used to connect if it's a persistent connection |
|
70
|
|
|
$methodName = 'connect'; |
|
71
|
|
|
if ($persistent === true) { |
|
72
|
|
|
$methodName = 'pconnect'; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
//If a timeout is declared |
|
76
|
|
|
//not found the default value for this parameters, so a if... |
|
77
|
|
|
if ($timeout !== null) { |
|
78
|
|
|
$this->{$methodName}($host, $port, $timeout); |
|
79
|
|
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->{$methodName}($host, $port); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|