1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Cache\Driver; |
3
|
|
|
|
4
|
|
|
use WebStream\Module\Container; |
5
|
|
|
use WebStream\IO\File; |
6
|
|
|
use WebStream\IO\Reader\FileReader; |
7
|
|
|
use WebStream\IO\Writer\FileWriter; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* CacheDriverFactory |
11
|
|
|
* @author Ryuichi Tanaka |
12
|
|
|
* @since 2015/07/10 |
13
|
|
|
* @version 0.7 |
14
|
|
|
*/ |
15
|
|
|
class CacheDriverFactory |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* キャッシュドライバオブジェクトを作成する |
19
|
|
|
* @param string $classpath ドライバクラスパス |
20
|
|
|
* @param Container $config 依存コンテナ |
21
|
|
|
* @return ICache キャッシュドライバオブジェクト |
22
|
|
|
*/ |
23
|
|
|
public function create(string $classpath, Container $config = null): ICache |
24
|
|
|
{ |
25
|
|
|
$cache = null; |
26
|
|
|
switch ($classpath) { |
27
|
|
|
case "WebStream\Cache\Driver\Apcu": |
28
|
|
|
$cache = new $classpath($this->getApcuContainer()); |
29
|
|
|
break; |
30
|
|
|
case "WebStream\Cache\Driver\Memcached": |
31
|
|
|
$cache = new $classpath($this->getMemcachedContainer($config)); |
|
|
|
|
32
|
|
|
break; |
33
|
|
|
case "WebStream\Cache\Driver\Redis": |
34
|
|
|
$cache = new $classpath($this->getRedisContainer($config)); |
|
|
|
|
35
|
|
|
break; |
36
|
|
|
case "WebStream\Cache\Driver\TemporaryFile": |
37
|
|
|
$cache = new $classpath($this->getTemporaryFileContainer($config)); |
|
|
|
|
38
|
|
|
break; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $cache; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* APCuオブジェクトを返却する |
46
|
|
|
* @return Container キャッシュ依存コンテナ |
47
|
|
|
*/ |
48
|
|
|
private function getApcuContainer(): Container |
49
|
|
|
{ |
50
|
|
|
$cacheContainer = new Container(); |
51
|
|
|
$cacheContainer->available = extension_loaded('apcu'); |
|
|
|
|
52
|
|
|
$cacheContainer->cachePrefix = "cache.apcu."; |
|
|
|
|
53
|
|
|
$cacheContainer->driver = new class() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
public function delegate($function, array $args = []) |
56
|
|
|
{ |
57
|
|
|
return function_exists($function) ? call_user_func_array($function, $args) : null; |
58
|
|
|
} |
59
|
|
|
}; |
60
|
|
|
|
61
|
|
|
return $cacheContainer; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Memcachedオブジェクトを返却する |
66
|
|
|
* @param Container $container 依存コンテナ |
67
|
|
|
* @return Container キャッシュ依存コンテナ |
68
|
|
|
*/ |
69
|
|
|
private function getMemcachedContainer(Container $container): Container |
70
|
|
|
{ |
71
|
|
|
$cacheContainer = new Container(); |
72
|
|
|
$cacheContainer->driver = new \Memcached(); |
|
|
|
|
73
|
|
|
$cacheContainer->available = extension_loaded('memcached'); |
|
|
|
|
74
|
|
|
$cacheContainer->cachePrefix = "cache.memcached."; |
|
|
|
|
75
|
|
|
$cacheContainer->codes = [ |
|
|
|
|
76
|
|
|
'success' => \Memcached::RES_SUCCESS, |
77
|
|
|
'notfound' => \Memcached::RES_NOTFOUND |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
if ($cacheContainer->available) { |
|
|
|
|
81
|
|
|
$cacheContainer->driver->addServers($container->servers); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$defaultOptions = [ |
84
|
|
|
\Memcached::OPT_CONNECT_TIMEOUT => 50, |
85
|
|
|
\Memcached::OPT_RETRY_TIMEOUT => 50, |
86
|
|
|
\Memcached::OPT_SEND_TIMEOUT => 50, |
87
|
|
|
\Memcached::OPT_RECV_TIMEOUT => 50, |
88
|
|
|
\Memcached::OPT_POLL_TIMEOUT => 50, |
89
|
|
|
\Memcached::OPT_COMPRESSION => true, |
90
|
|
|
\Memcached::OPT_LIBKETAMA_COMPATIBLE => true, |
91
|
|
|
\Memcached::OPT_BINARY_PROTOCOL => true |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
if (\Memcached::HAVE_IGBINARY) { |
95
|
|
|
$defaultOptions[\Memcached::OPT_SERIALIZER] = \Memcached::SERIALIZER_IGBINARY; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$cacheContainer->driver->setOptions($defaultOptions); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $cacheContainer; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Redisオブジェクトを返却する |
106
|
|
|
* @param Container $container 依存コンテナ |
107
|
|
|
* @return Container キャッシュ依存コンテナ |
108
|
|
|
*/ |
109
|
|
|
private function getRedisContainer(Container $container): Container |
110
|
|
|
{ |
111
|
|
|
$cacheContainer = new Container(); |
112
|
|
|
$cacheContainer->driver = new \Redis(); |
|
|
|
|
113
|
|
|
$cacheContainer->available = extension_loaded('redis'); |
|
|
|
|
114
|
|
|
$cacheContainer->cachePrefix = "cache.redis."; |
|
|
|
|
115
|
|
|
$cacheContainer->redisOptPrefix = \Redis::OPT_PREFIX; |
|
|
|
|
116
|
|
|
|
117
|
|
|
if ($cacheContainer->available) { |
|
|
|
|
118
|
|
|
$host = $container->host; |
|
|
|
|
119
|
|
|
$port = $container->port; |
|
|
|
|
120
|
|
|
$socket = $container->socket; |
|
|
|
|
121
|
|
|
$password = $container->password; |
|
|
|
|
122
|
|
|
$isAuthed = true; |
123
|
|
|
|
124
|
|
|
if ($password !== null) { |
125
|
|
|
$isAuthed = $cacheContainer->driver->auth($password); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($isAuthed) { |
129
|
|
|
if ($host !== null && $port !== null) { |
130
|
|
|
$cacheContainer->available = $cacheContainer->driver->connect($host, $port); |
|
|
|
|
131
|
|
|
} elseif ($socket !== null) { |
132
|
|
|
$cacheContainer->available = $cacheContainer->driver->connect($socket); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (defined('\Redis::SERIALIZER_IGBINARY')) { |
136
|
|
|
$cacheContainer->driver->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_IGBINARY); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$cacheContainer->driver->setOption(\Redis::OPT_PREFIX, $container->cachePrefix); |
|
|
|
|
140
|
|
|
$cacheContainer->driver->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $cacheContainer; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* TemporaryFileオブジェクトを返却する |
149
|
|
|
* @param Container $container 依存コンテナ |
150
|
|
|
* @return Container キャッシュ依存コンテナ |
151
|
|
|
*/ |
152
|
|
|
private function getTemporaryFileContainer(Container $container): Container |
153
|
|
|
{ |
154
|
|
|
$cacheContainer = new Container(); |
155
|
|
|
$cacheContainer->cachePrefix = "cache.file."; |
|
|
|
|
156
|
|
|
$dir = new File($container->cacheDir); |
|
|
|
|
157
|
|
|
$cacheContainer->available = $dir->isWritable(); |
|
|
|
|
158
|
|
|
$cacheContainer->cacheDir = $container->cacheDir; |
|
|
|
|
159
|
|
|
$cacheContainer->ioContainer = new Container(); |
|
|
|
|
160
|
|
|
$cacheContainer->ioContainer->fileReader = new class() |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
public function getReader(File $file) |
163
|
|
|
{ |
164
|
|
|
return new FileReader($file); |
165
|
|
|
} |
166
|
|
|
}; |
167
|
|
|
$cacheContainer->ioContainer->fileWriter = new class() |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
public function getWriter($file, $isAppend) |
170
|
|
|
{ |
171
|
|
|
return new FileWriter($file, $isAppend); |
172
|
|
|
} |
173
|
|
|
}; |
174
|
|
|
|
175
|
|
|
return $cacheContainer; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
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):