1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of php-cache organization. |
5
|
|
|
* |
6
|
|
|
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cache\AdapterBundle\Factory; |
13
|
|
|
|
14
|
|
|
use Cache\Adapter\Memcached\MemcachedCachePool; |
15
|
|
|
use Cache\AdapterBundle\ProviderHelper\Memcached; |
16
|
|
|
use Cache\Namespaced\NamespacedCachePool; |
17
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Tobias Nyholm <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
final class MemcachedFactory extends AbstractAdapterFactory |
23
|
|
|
{ |
24
|
|
|
protected static $dependencies = [ |
25
|
|
|
['requiredClass' => 'Cache\Adapter\Memcached\MemcachedCachePool', 'packageName' => 'cache/memcached-adapter'], |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
3 |
|
public function getAdapter(array $config) |
32
|
|
|
{ |
33
|
3 |
|
$client = new Memcached($config['persistent_id']); |
34
|
3 |
|
$client->addServer($config['host'], $config['port']); |
35
|
|
|
|
36
|
3 |
|
foreach ($config['redundant_servers'] as $server) { |
37
|
|
|
if (!isset($server['host'])) { |
38
|
|
|
continue; |
39
|
|
|
} |
40
|
|
|
$port = $config['port']; |
41
|
|
|
if (isset($server['port'])) { |
42
|
|
|
$port = $server['port']; |
43
|
|
|
} |
44
|
|
|
$client->addServer($server['host'], $port); |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
foreach ($config['driver_options'] as $constant => $value) { |
48
|
|
|
$client->setOption(constant($constant), $value); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
$pool = new MemcachedCachePool($client); |
52
|
|
|
|
53
|
3 |
|
if (null !== $config['pool_namespace']) { |
54
|
|
|
$pool = new NamespacedCachePool($pool, $config['pool_namespace']); |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
return $pool; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
3 |
|
protected static function configureOptionResolver(OptionsResolver $resolver) |
64
|
|
|
{ |
65
|
3 |
|
$resolver->setDefaults([ |
66
|
3 |
|
'persistent_id' => null, |
67
|
|
|
'host' => '127.0.0.1', |
68
|
|
|
'port' => 11211, |
69
|
|
|
'pool_namespace' => null, |
70
|
|
|
'redundant_servers' => [], |
71
|
|
|
'driver_options' => [], |
72
|
|
|
]); |
73
|
|
|
|
74
|
3 |
|
$resolver->setAllowedTypes('persistent_id', ['string', 'null']); |
75
|
3 |
|
$resolver->setAllowedTypes('host', ['string']); |
76
|
3 |
|
$resolver->setAllowedTypes('port', ['string', 'int']); |
77
|
3 |
|
$resolver->setAllowedTypes('pool_namespace', ['string', 'null']); |
78
|
3 |
|
$resolver->setAllowedTypes('redundant_servers', ['array']); |
79
|
3 |
|
$resolver->setAllowedTypes('driver_options', ['array']); |
80
|
3 |
|
} |
81
|
|
|
} |
82
|
|
|
|