Skip to content

Commit b0c41d1

Browse files
committed
APCu cache support
1 parent 6b20a0c commit b0c41d1

File tree

12 files changed

+312
-137
lines changed

12 files changed

+312
-137
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ return [
8787
];
8888
```
8989

90+
### Enable APCu
91+
92+
```php
93+
return [
94+
'phpdi-zf2' => [
95+
'cache' => [
96+
'adapter' => 'apcu',
97+
'namespace' => 'your_di_cache_key',
98+
],
99+
]
100+
];
101+
```
102+
90103
### Enable file cache
91104

92105
```php
@@ -129,4 +142,4 @@ To clear the definition cache, run the following command from the project root:
129142

130143
```
131144
php public/index.php php-di-clear-cache
132-
```
145+
```

config/module.config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
'factories' => [
2424
'ControllerLoader' => __NAMESPACE__ . '\\Service\\ControllerLoaderFactory',
25-
'DiCache' => __NAMESPACE__ . '\\Service\\CacheFactory',
25+
'DiCache' => __NAMESPACE__ . '\\Service\\CacheFactory\CacheFactory',
2626
],
2727
],
2828

src/DI/ZendFramework2/Service/CacheFactory.php

Lines changed: 0 additions & 128 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* @author Martin Fris <[email protected]>
5+
*/
6+
7+
namespace DI\ZendFramework2\Service\CacheFactory;
8+
9+
use Doctrine\Common\Cache\ApcCache;
10+
11+
/**
12+
* Class RedisFactory
13+
* @author mfris
14+
* @package DI\ZendFramework\Service\CacheFactory
15+
*/
16+
final class ApcuFactory implements CacheFactoryInterface
17+
{
18+
19+
/**
20+
* @param array $config
21+
* @return ApcCache
22+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
23+
*/
24+
public function newInstance(array $config)
25+
{
26+
return new ApcCache();
27+
}
28+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* @author mfris
4+
*/
5+
6+
namespace DI\ZendFramework2\Service\CacheFactory;
7+
8+
use DI\ZendFramework2\Service\ConfigTrait;
9+
use Doctrine\Common\Cache\Cache;
10+
use Doctrine\Common\Cache\CacheProvider;
11+
use Zend\ServiceManager\FactoryInterface;
12+
use Zend\ServiceManager\ServiceLocatorInterface;
13+
14+
/**
15+
* Factory for php di definitions cache
16+
*
17+
* @author mfris
18+
* @package \DI\ZendFramework\Service\Cache
19+
*/
20+
class CacheFactory implements FactoryInterface
21+
{
22+
use ConfigTrait;
23+
24+
/**
25+
* @var array
26+
*/
27+
private $adapterClasses = [
28+
'filesystem' => FileSystemFactory::class,
29+
'redis' => RedisFactory::class,
30+
'memcached' => MemcachedFactory::class,
31+
'apcu' => ApcuFactory::class,
32+
];
33+
34+
/**
35+
* @var CacheFactoryInterface[]
36+
*/
37+
private $adapters = [];
38+
39+
/**
40+
* Create service
41+
*
42+
* @param ServiceLocatorInterface $serviceLocator
43+
* @return mixed
44+
*/
45+
public function createService(ServiceLocatorInterface $serviceLocator)
46+
{
47+
$config = $this->getConfig($serviceLocator);
48+
49+
if (!isset($config['cache'])) {
50+
return null;
51+
}
52+
53+
$config = $config['cache'];
54+
/* @var $cache CacheProvider|Cache */
55+
$cache = null;
56+
$cacheFactory = $this->getCacheFactory($config);
57+
$cache = $cacheFactory->newInstance($config);
58+
59+
if (isset($config['namespace'])) {
60+
$cache->setNamespace(trim($config['namespace']));
61+
}
62+
63+
return $cache;
64+
}
65+
66+
/**
67+
* @param array $config
68+
* @return CacheFactoryInterface
69+
* @throws ConfigException
70+
* @SuppressWarnings(PHPMD.StaticAccess)
71+
*/
72+
private function getCacheFactory(array $config)
73+
{
74+
if (!isset($config['adapter'])) {
75+
throw ConfigException::newCacheAdapterMissingException();
76+
}
77+
78+
$adapter = $config['adapter'];
79+
80+
if (!isset($this->adapterClasses[$adapter])) {
81+
throw ConfigException::newUnsupportedCacheAdapterException($adapter);
82+
}
83+
84+
if (!isset($this->adapters[$adapter])) {
85+
$this->adapters[$adapter] = new $this->adapterClasses[$adapter]();
86+
}
87+
88+
/* @var CacheFactoryInterface $cacheFactory */
89+
return $this->adapters[$adapter];
90+
}
91+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/**
4+
* @author Martin Fris <[email protected]>
5+
*/
6+
7+
namespace DI\ZendFramework2\Service\CacheFactory;
8+
9+
use Doctrine\Common\Cache\Cache;
10+
11+
/**
12+
* Interface CacheFactoryInterface
13+
* @package DI\ZendFramework\Service\CacheFactory
14+
*/
15+
interface CacheFactoryInterface
16+
{
17+
18+
/**
19+
* @param array $config
20+
* @return Cache
21+
*/
22+
public function newInstance(array $config);
23+
}

src/DI/ZendFramework2/Service/ConfigException.php renamed to src/DI/ZendFramework2/Service/CacheFactory/ConfigException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
/**
33
* @author mfris
44
*/
5-
namespace DI\ZendFramework2\Service;
5+
namespace DI\ZendFramework2\Service\CacheFactory;
66

77
use Exception;
88

99
/**
1010
* Class ConfigException - custom exception for configuration problems
1111
*
1212
* @author mfris
13-
* @package DI\ZendFramework2\Service
13+
* @package DI\ZendFramework\Service
1414
*/
1515
class ConfigException extends Exception
1616
{
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/**
4+
* @author Martin Fris <[email protected]>
5+
*/
6+
7+
namespace DI\ZendFramework2\Service\CacheFactory;
8+
9+
use Doctrine\Common\Cache\FilesystemCache;
10+
11+
/**
12+
* Class FileSystemFactory
13+
* @author mfris
14+
* @package DI\ZendFramework\Service\CacheFactory
15+
*/
16+
final class FileSystemFactory implements CacheFactoryInterface
17+
{
18+
19+
/**
20+
* @param array $config
21+
* @return FilesystemCache
22+
* @throws \InvalidArgumentException
23+
*/
24+
public function newInstance(array $config)
25+
{
26+
$directory = getcwd() . '/data/php-di/cache';
27+
28+
if (isset($config['directory'])) {
29+
$directory = $config['directory'];
30+
}
31+
32+
return new FilesystemCache($directory);
33+
}
34+
}

0 commit comments

Comments
 (0)