|
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 = new $classpath($this->getApcuContainer()); |
|
26
|
|
|
break; |
|
27
|
|
|
case "WebStream\Cache\Driver\Memcached": |
|
28
|
|
|
$cache = new $classpath($this->getMemcachedContainer($config)); |
|
|
|
|
|
|
29
|
|
|
break; |
|
30
|
|
|
case "WebStream\Cache\Driver\Redis": |
|
31
|
|
|
$cache = new $classpath($this->getRedisContainer($config)); |
|
|
|
|
|
|
32
|
|
|
break; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return $cache; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* APCuオブジェクトを返却する |
|
40
|
|
|
* @return Container キャッシュ依存コンテナ |
|
41
|
|
|
*/ |
|
42
|
|
|
private function getApcuContainer(): Container |
|
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 $cacheContainer; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Memcachedオブジェクトを返却する |
|
60
|
|
|
* @param Container $container 依存コンテナ |
|
61
|
|
|
* @return Container キャッシュ依存コンテナ |
|
62
|
|
|
*/ |
|
63
|
|
|
private function getMemcachedContainer(Container $container): Container |
|
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 $cacheContainer; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Redisオブジェクトを返却する |
|
100
|
|
|
* @param Container $container 依存コンテナ |
|
101
|
|
|
* @return Container キャッシュ依存コンテナ |
|
102
|
|
|
*/ |
|
103
|
|
|
private function getRedisContainer(Container $container): Container |
|
104
|
|
|
{ |
|
105
|
|
|
$cacheContainer = new Container(); |
|
106
|
|
|
$cacheContainer->driver = new \Redis(); |
|
|
|
|
|
|
107
|
|
|
$cacheContainer->available = extension_loaded('redis'); |
|
|
|
|
|
|
108
|
|
|
$cacheContainer->cachePrefix = "cache.redis."; |
|
|
|
|
|
|
109
|
|
|
$cacheContainer->redisOptPrefix = \Redis::OPT_PREFIX; |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
if ($cacheContainer->available) { |
|
|
|
|
|
|
112
|
|
|
$host = $container->host; |
|
|
|
|
|
|
113
|
|
|
$port = $container->port; |
|
|
|
|
|
|
114
|
|
|
$socket = $container->socket; |
|
|
|
|
|
|
115
|
|
|
$password = $container->password; |
|
|
|
|
|
|
116
|
|
|
$isAuthed = true; |
|
117
|
|
|
|
|
118
|
|
|
if ($password !== null) { |
|
119
|
|
|
$isAuthed = $cacheContainer->driver->auth($password); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if ($isAuthed) { |
|
123
|
|
|
if ($host !== null && $port !== null) { |
|
124
|
|
|
$cacheContainer->available = $cacheContainer->driver->connect($host, $port); |
|
|
|
|
|
|
125
|
|
|
} elseif ($socket !== null) { |
|
126
|
|
|
$cacheContainer->available = $cacheContainer->driver->connect($socket); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (defined('\Redis::SERIALIZER_IGBINARY')) { |
|
130
|
|
|
$cacheContainer->driver->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_IGBINARY); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$cacheContainer->driver->setOption(\Redis::OPT_PREFIX, $container->cachePrefix); |
|
|
|
|
|
|
134
|
|
|
$cacheContainer->driver->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $cacheContainer; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
private function createTemporaryFile(): ICache |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
|
|
// TODO |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
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):