Skip to content

Commit

Permalink
Merge pull request #13 from Mattin/memcached_support
Browse files Browse the repository at this point in the history
Added support for memcached caching
  • Loading branch information
Rastusik authored Sep 15, 2016
2 parents 6b20a0c + f5732ad commit 26dc007
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ return [
];
```

### Enable Memcached cache

If you're using Memcached, you should have only one project per memcached instance.

```php
return [
'phpdi-zf2' => [
'cache' => [
'adapter' => 'memcached',
'host' => 'localhost', // default is localhost
'port' => 11211, // default is 11211
],
]
];
```

## Console commands

### Clear definition cache
Expand Down
32 changes: 32 additions & 0 deletions src/DI/ZendFramework2/Service/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\CacheProvider;
use Doctrine\Common\Cache\FilesystemCache;
use Doctrine\Common\Cache\MemcachedCache;
use Doctrine\Common\Cache\RedisCache;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
Expand Down Expand Up @@ -60,6 +61,10 @@ public function createService(ServiceLocatorInterface $serviceLocator)
$cache = $this->getRedisCache($config);
break;

case 'memcached':
$cache = $this->getMemcachedCache($config);
break;

default:
throw ConfigException::newUnsupportedCacheAdapterException($adapter);
}
Expand Down Expand Up @@ -125,4 +130,31 @@ private function getRedisCache(array $config)

return $cache;
}

/**
* creates memcached cache
*
* @param array $config
* @return MemcachedCache
*/
private function getMemcachedCache(array $config)
{
$host = 'localhost';
$port = 11211;

if (isset($config['host'])) {
$host = $config['host'];
}

if (isset($config['port'])) {
$port = $config['port'];
}

$cache = new MemcachedCache();
$memcache = new \Memcached;
$memcache->addServer($host, $port);
$cache->setMemcached($memcache);

return $cache;
}
}

0 comments on commit 26dc007

Please sign in to comment.