|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Acelaya\Website\Factory; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\Common\Cache; |
|
7
|
|
|
use Interop\Container\ContainerInterface; |
|
8
|
|
|
use Interop\Container\Exception\ContainerException; |
|
9
|
|
|
use Predis\Client; |
|
10
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
|
11
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
|
12
|
|
|
use Zend\ServiceManager\Factory\FactoryInterface; |
|
13
|
|
|
|
|
14
|
|
|
class CacheFactory implements FactoryInterface |
|
15
|
|
|
{ |
|
16
|
|
|
const FEED_CACHE = 'Acelaya\Website\FeedCache'; |
|
17
|
|
|
const VIEWS_CACHE = 'Acelaya\Website\ViewsCache'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Create an object |
|
21
|
|
|
* |
|
22
|
|
|
* @param ContainerInterface $container |
|
23
|
|
|
* @param string $requestedName |
|
24
|
|
|
* @param null|array $options |
|
25
|
|
|
* @return Cache\Cache|object |
|
26
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
|
27
|
3 |
|
* @throws ServiceNotCreatedException if an exception is raised when |
|
28
|
|
|
* creating a service. |
|
29
|
3 |
|
* @throws ContainerException if any other error occurs |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): Cache\Cache |
|
32
|
|
|
{ |
|
33
|
|
|
$config = $container->get('config')['cache'] ?? []; |
|
34
|
|
|
$isDev = $container->get('config')['debug'] ?? true; |
|
35
|
|
|
$adapter = $this->getAdapter($config, $requestedName, (bool) $isDev); |
|
36
|
|
|
$adapter->setNamespace($config['namespaces'][$requestedName] ?? 'www.alejandrocelaya.com'); |
|
37
|
|
|
|
|
38
|
|
|
return $adapter; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $cacheConfig |
|
43
|
|
|
* @param $requestedName |
|
44
|
|
|
* @param bool $isDev |
|
45
|
|
|
* @return Cache\CacheProvider |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function getAdapter(array $cacheConfig, $requestedName, bool $isDev): Cache\CacheProvider |
|
48
|
|
|
{ |
|
49
|
|
|
if (! $isDev || $requestedName === self::FEED_CACHE) { |
|
50
|
|
|
$client = new Client($cacheConfig['redis']); |
|
51
|
|
|
return new Cache\PredisCache($client); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return new Cache\ArrayCache(); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|