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