1 | <?php |
||
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 | 32 | public function __construct(array $config) |
|
39 | |||
40 | /** |
||
41 | * @param $key |
||
42 | * |
||
43 | * @return AbstractAdapter |
||
44 | */ |
||
45 | 9 | public function getCache($key) |
|
46 | { |
||
47 | 9 | if (!isset($this->caches[$key])) { |
|
48 | 9 | if (!isset($this->config[$key])) { |
|
49 | throw new \LogicException(sprintf('No set %s cache', $key)); |
||
50 | } |
||
51 | 9 | $config = $this->config[$key]; |
|
52 | 9 | switch ($config['adapter']) { |
|
53 | 9 | case RedisAdapter::class: |
|
54 | $this->caches[$key] = new RedisAdapter( |
||
55 | RedisAdapter::createConnection($config['params']['dsn']), |
||
56 | isset($config['params']['namespace']) ? $config['params']['namespace'] : '', |
||
57 | isset($config['params']['lifetime']) ? $config['params']['lifetime'] : '' |
||
58 | ); |
||
59 | |||
60 | break; |
||
61 | 9 | default: |
|
62 | 9 | $this->caches[$key] = new $config['adapter']( |
|
63 | 9 | isset($config['params']['namespace']) ? $config['params']['namespace'] : '', |
|
64 | 9 | isset($config['params']['lifetime']) ? $config['params']['lifetime'] : '', |
|
65 | 9 | isset($config['params']['directory']) ? $config['params']['directory'] : app()->getPath().'/runtime/cache' |
|
66 | 9 | ); |
|
67 | 9 | } |
|
68 | 9 | } |
|
69 | |||
70 | 9 | return $this->caches[$key]; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function initPool() |
||
82 | } |
||
83 |