1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Cache\Driver; |
3
|
|
|
|
4
|
|
|
use WebStream\Module\Container; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* CacheDriverFactory |
8
|
|
|
* @author Ryuichi Tanaka |
9
|
|
|
* @since 2015/07/10 |
10
|
|
|
* @version 0.7 |
11
|
|
|
*/ |
12
|
|
|
class CacheDriverFactory |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* キャッシュドライバオブジェクトを作成する |
16
|
|
|
* @param string $classpath ドライバクラスパス |
17
|
|
|
* @param Container $config 依存コンテナ |
18
|
|
|
* @return ICache キャッシュドライバオブジェクト |
19
|
|
|
*/ |
20
|
|
|
public function create(string $classpath, Container $config = null): ICache |
21
|
|
|
{ |
22
|
|
|
$cache = null; |
23
|
|
|
switch ($classpath) { |
24
|
|
|
case "WebStream\Cache\Driver\Apcu": |
25
|
|
|
$cache = $this->createApcu(); |
26
|
|
|
break; |
27
|
|
|
case "WebStream\Cache\Driver\Memcached": |
28
|
|
|
$cache = $this->createMemcached($config); |
|
|
|
|
29
|
|
|
break; |
30
|
|
|
case "WebStream\Cache\Driver\Redis": |
31
|
|
|
$cache = $this->createRedis($config); |
|
|
|
|
32
|
|
|
break; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $cache; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* APCuオブジェクトを返却する |
40
|
|
|
* @return ICache キャッシュオブジェクト |
41
|
|
|
*/ |
42
|
|
|
private function createApcu(): ICache |
43
|
|
|
{ |
44
|
|
|
$cacheContainer = new Container(); |
45
|
|
|
$cacheContainer->available = extension_loaded('apcu'); |
|
|
|
|
46
|
|
|
$cacheContainer->cachePrefix = "cache.apcu."; |
|
|
|
|
47
|
|
|
$cacheContainer->driver = new class() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
public function delegate($function, array $args = []) |
50
|
|
|
{ |
51
|
|
|
return function_exists($function) ? call_user_func_array($function, $args) : null; |
52
|
|
|
} |
53
|
|
|
}; |
54
|
|
|
|
55
|
|
|
return new Apcu($cacheContainer); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Memcachedオブジェクトを返却する |
60
|
|
|
* @param Container $container 依存コンテナ |
61
|
|
|
* @return ICache キャッシュオブジェクト |
62
|
|
|
*/ |
63
|
|
|
private function createMemcached(Container $container): ICache |
64
|
|
|
{ |
65
|
|
|
$cacheContainer = new Container(); |
66
|
|
|
$cacheContainer->driver = new \Memcached(); |
|
|
|
|
67
|
|
|
$cacheContainer->available = extension_loaded('memcached'); |
|
|
|
|
68
|
|
|
$cacheContainer->cachePrefix = "cache.memcached."; |
|
|
|
|
69
|
|
|
$cacheContainer->codes = [ |
|
|
|
|
70
|
|
|
'success' => \Memcached::RES_SUCCESS, |
71
|
|
|
'notfound' => \Memcached::RES_NOTFOUND |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
if ($cacheContainer->available) { |
|
|
|
|
75
|
|
|
$cacheContainer->driver->addServers($container->servers); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$defaultOptions = [ |
78
|
|
|
\Memcached::OPT_CONNECT_TIMEOUT => 50, |
79
|
|
|
\Memcached::OPT_RETRY_TIMEOUT => 50, |
80
|
|
|
\Memcached::OPT_SEND_TIMEOUT => 50, |
81
|
|
|
\Memcached::OPT_RECV_TIMEOUT => 50, |
82
|
|
|
\Memcached::OPT_POLL_TIMEOUT => 50, |
83
|
|
|
\Memcached::OPT_COMPRESSION => true, |
84
|
|
|
\Memcached::OPT_LIBKETAMA_COMPATIBLE => true, |
85
|
|
|
\Memcached::OPT_BINARY_PROTOCOL => true |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
if (\Memcached::HAVE_IGBINARY) { |
89
|
|
|
$defaultOptions[\Memcached::OPT_SERIALIZER] = \Memcached::SERIALIZER_IGBINARY; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$cacheContainer->driver->setOptions($defaultOptions); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return new Memcached($cacheContainer); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Redisオブジェクトを返却する |
100
|
|
|
* @param Container $container 依存コンテナ |
101
|
|
|
* @return ICache キャッシュオブジェクト |
102
|
|
|
*/ |
103
|
|
|
private function createRedis(Container $container): ICache |
104
|
|
|
{ |
105
|
|
|
$cacheContainer = new Container(); |
106
|
|
|
$cacheContainer->driver = new \Redis(); |
|
|
|
|
107
|
|
|
$cacheContainer->available = extension_loaded('redis'); |
|
|
|
|
108
|
|
|
$cacheContainer->cachePrefix = "cache.redis."; |
|
|
|
|
109
|
|
|
|
110
|
|
|
if ($cacheContainer->available) { |
|
|
|
|
111
|
|
|
$host = $container->host; |
|
|
|
|
112
|
|
|
$port = $container->port; |
|
|
|
|
113
|
|
|
$socket = $container->socket; |
|
|
|
|
114
|
|
|
$password = $container->password; |
|
|
|
|
115
|
|
|
$isAuthed = true; |
116
|
|
|
|
117
|
|
|
if ($password !== null) { |
118
|
|
|
$isAuthed = $cacheContainer->driver->auth($password); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($isAuthed) { |
122
|
|
|
if ($host !== null && $port !== null) { |
123
|
|
|
$cacheContainer->available = $cacheContainer->driver->connect($host, $port); |
|
|
|
|
124
|
|
|
} elseif ($socket !== null) { |
125
|
|
|
$cacheContainer->available = $cacheContainer->driver->connect($socket); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (defined('\Redis::SERIALIZER_IGBINARY')) { |
129
|
|
|
$cacheContainer->driver->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_IGBINARY); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$cacheContainer->driver->setOption(\Redis::OPT_PREFIX, $container->cachePrefix); |
|
|
|
|
133
|
|
|
$cacheContainer->driver->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return new Redis($cacheContainer); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):