Skip to content

Commit 26dc007

Browse files
authored
Merge pull request #13 from Mattin/memcached_support
Added support for memcached caching
2 parents 6b20a0c + f5732ad commit 26dc007

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,22 @@ return [
121121
];
122122
```
123123

124+
### Enable Memcached cache
125+
126+
If you're using Memcached, you should have only one project per memcached instance.
127+
128+
```php
129+
return [
130+
'phpdi-zf2' => [
131+
'cache' => [
132+
'adapter' => 'memcached',
133+
'host' => 'localhost', // default is localhost
134+
'port' => 11211, // default is 11211
135+
],
136+
]
137+
];
138+
```
139+
124140
## Console commands
125141

126142
### Clear definition cache

src/DI/ZendFramework2/Service/CacheFactory.php

+32
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\Common\Cache\Cache;
99
use Doctrine\Common\Cache\CacheProvider;
1010
use Doctrine\Common\Cache\FilesystemCache;
11+
use Doctrine\Common\Cache\MemcachedCache;
1112
use Doctrine\Common\Cache\RedisCache;
1213
use Zend\ServiceManager\FactoryInterface;
1314
use Zend\ServiceManager\ServiceLocatorInterface;
@@ -60,6 +61,10 @@ public function createService(ServiceLocatorInterface $serviceLocator)
6061
$cache = $this->getRedisCache($config);
6162
break;
6263

64+
case 'memcached':
65+
$cache = $this->getMemcachedCache($config);
66+
break;
67+
6368
default:
6469
throw ConfigException::newUnsupportedCacheAdapterException($adapter);
6570
}
@@ -125,4 +130,31 @@ private function getRedisCache(array $config)
125130

126131
return $cache;
127132
}
133+
134+
/**
135+
* creates memcached cache
136+
*
137+
* @param array $config
138+
* @return MemcachedCache
139+
*/
140+
private function getMemcachedCache(array $config)
141+
{
142+
$host = 'localhost';
143+
$port = 11211;
144+
145+
if (isset($config['host'])) {
146+
$host = $config['host'];
147+
}
148+
149+
if (isset($config['port'])) {
150+
$port = $config['port'];
151+
}
152+
153+
$cache = new MemcachedCache();
154+
$memcache = new \Memcached;
155+
$memcache->addServer($host, $port);
156+
$cache->setMemcached($memcache);
157+
158+
return $cache;
159+
}
128160
}

0 commit comments

Comments
 (0)