|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mosaic\Cache\Adapters\PhpCache; |
|
4
|
|
|
|
|
5
|
|
|
use BadMethodCallException; |
|
6
|
|
|
use Cache\Adapter\Apc\ApcCachePool; |
|
7
|
|
|
use Cache\Adapter\Apcu\ApcuCachePool; |
|
8
|
|
|
use Cache\Adapter\Filesystem\FilesystemCachePool; |
|
9
|
|
|
use Cache\Adapter\Memcache\MemcacheCachePool; |
|
10
|
|
|
use Cache\Adapter\Memcached\MemcachedCachePool; |
|
11
|
|
|
use Cache\Adapter\PHPArray\ArrayCachePool; |
|
12
|
|
|
use Cache\Adapter\Predis\PredisCachePool; |
|
13
|
|
|
use Cache\Adapter\Void\VoidCachePool; |
|
14
|
|
|
use Interop\Container\Definition\DefinitionProviderInterface; |
|
15
|
|
|
use League\Flysystem\Adapter\Local; |
|
16
|
|
|
use League\Flysystem\Filesystem; |
|
17
|
|
|
use Memcache; |
|
18
|
|
|
use Memcached; |
|
19
|
|
|
use Mosaic\Cache\Providers\PhpCacheProvider; |
|
20
|
|
|
use Mosaic\Common\Components\Component as ComponentInterface; |
|
21
|
|
|
use Mosaic\Common\Conventions\FolderStructureConvention; |
|
22
|
|
|
use Predis\Client; |
|
23
|
|
|
|
|
24
|
|
|
class Component implements ComponentInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private static $custom = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var callable[] |
|
33
|
|
|
*/ |
|
34
|
|
|
private $pools = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param callable $callable |
|
38
|
|
|
* @return $this |
|
39
|
|
|
*/ |
|
40
|
|
|
public function pool(callable $callable) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->pools[] = $callable; |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $host |
|
49
|
|
|
* @param int $port |
|
50
|
|
|
* @return $this |
|
51
|
|
|
*/ |
|
52
|
|
|
public function redis(string $host = '127.0.0.1', int $port = 6379) |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->pool(function () use ($host, $port) { |
|
55
|
|
|
return new PredisCachePool( |
|
56
|
|
|
new Client('tcp:/' . $host . ':' . $port) |
|
57
|
|
|
); |
|
58
|
|
|
}); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return Component |
|
63
|
|
|
*/ |
|
64
|
|
|
public function apc() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->pool(function () { |
|
67
|
|
|
return new ApcCachePool(); |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return Component |
|
73
|
|
|
*/ |
|
74
|
|
|
public function apcu() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->pool(function () { |
|
77
|
|
|
return new ApcuCachePool(); |
|
78
|
|
|
}); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param null $limit |
|
83
|
|
|
* @param array $cache |
|
84
|
|
|
* @return Component |
|
85
|
|
|
*/ |
|
86
|
|
|
public function array($limit = null, array &$cache = []) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->pool(function () use ($limit, $cache) { |
|
89
|
|
|
return new ArrayCachePool($limit, $cache); |
|
90
|
|
|
}); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $host |
|
95
|
|
|
* @param int $port |
|
96
|
|
|
* @return Component |
|
97
|
|
|
*/ |
|
98
|
|
|
public function memcache(string $host = '127.0.0.1', int $port = 11211) |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->pool(function () use ($host, $port) { |
|
101
|
|
|
|
|
102
|
|
|
$client = new Memcache(); |
|
103
|
|
|
$client->connect($host, $port); |
|
104
|
|
|
|
|
105
|
|
|
return new MemcacheCachePool( |
|
106
|
|
|
$client |
|
107
|
|
|
); |
|
108
|
|
|
}); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $host |
|
113
|
|
|
* @param int $port |
|
114
|
|
|
* @return Component |
|
115
|
|
|
*/ |
|
116
|
|
|
public function memcached(string $host = '127.0.0.1', int $port = 11211) |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->pool(function () use ($host, $port) { |
|
119
|
|
|
|
|
120
|
|
|
$client = new Memcached(); |
|
121
|
|
|
$client->addServer($host, $port); |
|
122
|
|
|
|
|
123
|
|
|
return new MemcachedCachePool( |
|
124
|
|
|
$client |
|
125
|
|
|
); |
|
126
|
|
|
}); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param FolderStructureConvention $folderStructure |
|
131
|
|
|
* @return Component |
|
132
|
|
|
*/ |
|
133
|
|
|
public function file(FolderStructureConvention $folderStructure) |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->pool(function () use ($folderStructure) { |
|
136
|
|
|
|
|
137
|
|
|
$filesystem = new Filesystem( |
|
138
|
|
|
new Local($folderStructure->cachePath()) |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
return new FilesystemCachePool($filesystem); |
|
142
|
|
|
}); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return Component |
|
147
|
|
|
*/ |
|
148
|
|
|
public function void() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->pool(function () { |
|
151
|
|
|
return new VoidCachePool(); |
|
152
|
|
|
}); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return DefinitionProviderInterface[] |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getProviders() : array |
|
159
|
|
|
{ |
|
160
|
|
|
return [ |
|
161
|
|
|
new PhpCacheProvider($this->pools) |
|
162
|
|
|
]; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param string $name |
|
167
|
|
|
* @param callable $callback |
|
168
|
|
|
*/ |
|
169
|
|
|
public static function extend(string $name, callable $callback) |
|
170
|
|
|
{ |
|
171
|
|
|
self::$custom[$name] = $callback; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param string $name |
|
176
|
|
|
* @param array $params |
|
177
|
|
|
* @return Component |
|
178
|
|
|
*/ |
|
179
|
|
|
public function __call(string $name, array $params = []) |
|
180
|
|
|
{ |
|
181
|
|
|
if (isset(self::$custom[$name])) { |
|
182
|
|
|
return $this->pool(self::$custom[$name]); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
throw new BadMethodCallException; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|