CacheConnectionManager.php
1.4 KB
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
<?php
class LtCacheConnectionManager
{
public $configHandle;
protected $connectionAdapter;
public function getConnection($group, $node, $role)
{
if ($connection = $this->getNewConnection($group, $node, $role))
{
return array(
"connectionAdapter" => $this->connectionAdapter,
"connectionResource" => $connection
);
}
else
{
trigger_error("no cache server can be connected");
return false;
}
}
protected function getNewConnection($group, $node, $role)
{
$servers = $this->configHandle->get("cache.servers");
$hostTotal = count($servers[$group][$node][$role]);
$hostIndexArray = array_keys($servers[$group][$node][$role]);
while ($hostTotal)
{
$hashNumber = substr(microtime(),7,1) % $hostTotal;
$hostConfig = $servers[$group][$node][$role][$hostIndexArray[$hashNumber]];
$cacheFactory = new LtCacheAdapterFactory;
$this->connectionAdapter = $cacheFactory->getConnectionAdapter($hostConfig["adapter"]);
if ($connection = $this->connectionAdapter->connect($hostConfig))
{
return $connection;
}
else
{
//trigger_error('connection fail', E_USER_WARNING);
//delete the unavailable server
for ($i = $hashNumber; $i < $hostTotal - 1; $i ++)
{
$hostIndexArray[$i] = $hostIndexArray[$i+1];
}
unset($hostIndexArray[$hostTotal-1]);
$hostTotal --;
}//end else
}//end while
return false;
}
}