-
Notifications
You must be signed in to change notification settings - Fork 5
/
MemcacheFactory.php
45 lines (40 loc) · 1.36 KB
/
MemcacheFactory.php
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
<?php
namespace SM\MemcacheBundle;
/**
* Factoryclass for memcache instances.
*
* @author Tarjei Huse (tarjei - a - scanmine.com) http://www.kraken.no
*/
class MemcacheFactory
{
/**
* Creates the instance. The
* @param string $host memcached host
* @param int $port port to memcache instance
* @param bool $use_mock if the factory should return a mock instanc
* @param string $memcacheClass what implementation of memcached to use.
* @param array $options options for \Memcached class
* @throws \Exception if unable to connect to memcache
* @return object
*/
public static function create($host, $port, $use_mock, $memcacheClass, array $options = array())
{
if ($use_mock) {
return new MockMemcache;
}
$memcache = new $memcacheClass();
if ($memcache instanceof \Memcache) {
/** @var \Memcache $memcache */
if (!$memcache->connect($host, $port)) {
throw new \Exception("Could not connect to memcache service on $host:$port");
}
} else {
/** @var \Memcached $memcache */
$memcache->addServer($host, $port);
foreach ($options as $optionName => $optionValue) {
$memcache->setOption($optionName, $optionValue);
}
}
return $memcache;
}
}