|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author jan huang <[email protected]> |
|
4
|
|
|
* @copyright 2016 |
|
5
|
|
|
* |
|
6
|
|
|
* @see https://www.github.com/janhuang |
|
7
|
|
|
* @see https://fastdlabs.com |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace FastD\Pool; |
|
11
|
|
|
|
|
12
|
|
|
use Symfony\Component\Cache\Adapter\AbstractAdapter; |
|
13
|
|
|
use Symfony\Component\Cache\Adapter\RedisAdapter; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class CachePool. |
|
17
|
|
|
*/ |
|
18
|
|
|
class CachePool implements PoolInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var AbstractAdapter[] |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $caches = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $config; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Cache constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $config |
|
34
|
|
|
*/ |
|
35
|
37 |
|
public function __construct(array $config) |
|
36
|
|
|
{ |
|
37
|
37 |
|
$this->config = $config; |
|
38
|
37 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param $key |
|
42
|
|
|
* |
|
43
|
|
|
* @return AbstractAdapter |
|
44
|
|
|
*/ |
|
45
|
10 |
|
public function getCache($key) |
|
46
|
|
|
{ |
|
47
|
10 |
|
if (!isset($this->caches[$key])) { |
|
48
|
10 |
|
if (!isset($this->config[$key])) { |
|
49
|
|
|
throw new \LogicException(sprintf('No set %s cache', $key)); |
|
50
|
|
|
} |
|
51
|
10 |
|
$config = $this->config[$key]; |
|
52
|
10 |
|
switch ($config['adapter']) { |
|
53
|
10 |
|
case RedisAdapter::class: |
|
54
|
10 |
|
$this->caches[$key] = new RedisAdapter( |
|
55
|
10 |
|
RedisAdapter::createConnection($config['params']['dsn']), |
|
56
|
10 |
|
isset($config['params']['namespace']) ? $config['params']['namespace'] : '', |
|
57
|
10 |
|
isset($config['params']['lifetime']) ? $config['params']['lifetime'] : '' |
|
58
|
10 |
|
); |
|
59
|
|
|
|
|
60
|
10 |
|
break; |
|
61
|
|
|
default: |
|
62
|
|
|
$this->caches[$key] = new $config['adapter']( |
|
63
|
|
|
isset($config['params']['namespace']) ? $config['params']['namespace'] : '', |
|
64
|
|
|
isset($config['params']['lifetime']) ? $config['params']['lifetime'] : '', |
|
65
|
|
|
isset($config['params']['directory']) ? $config['params']['directory'] : app()->getPath().'/runtime/cache' |
|
66
|
|
|
); |
|
67
|
10 |
|
} |
|
68
|
10 |
|
} |
|
69
|
|
|
|
|
70
|
10 |
|
return $this->caches[$key]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function initPool() |
|
77
|
|
|
{ |
|
78
|
|
|
foreach ($this->config as $name => $config) { |
|
79
|
|
|
$this->getCache($name); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|