Add support of memcached storage for caching

This commit is contained in:
Dmitriy Simushev 2015-03-20 15:26:15 +00:00
parent 758203b6d3
commit 22aaedd3be
2 changed files with 26 additions and 1 deletions

View File

@ -42,7 +42,7 @@ mailer:
# Cache subsystem
cache:
# This value determines where the cached data will be stored. Possible
# values are "none" and "file_system".
# values are "none", "file_system" and "memcached".
#
# If "none" is used the caching will be disabled. This option should be used
# only if none of the other options works.
@ -50,8 +50,17 @@ cache:
# If "file_system" is used the cached data will be stored in files within
# the "cache/" directory. Notice, that the file system storage does not work
# on NFS systems bucause exclusive file locks are not supported where.
#
# If "memcached" is used the cached data will be stored in Memcached
# storage. Before using this kind of storage make sure PHP's memcached
# extension is installed and enabled.
storage: file_system
# The lines below represents default Memcached server configurations. They
# will be used only if the "storage" option is set to "memcached".
memcached_host: localhost
memcached_port: 11211
# Locales
## Native name will be used in this locale
home_locale: en

View File

@ -22,6 +22,7 @@ namespace Mibew\Cache;
use Stash\Interfaces\PoolInterface;
use Stash\Driver\Ephemeral as EphemeralDriver;
use Stash\Driver\FileSystem as FileSystemDriver;
use Stash\Driver\Memcache as MemcacheDriver;
use Stash\Pool as CachePool;
/**
@ -97,6 +98,8 @@ class CacheFactory
$defaults = array(
'storage' => 'file_system',
'path' => '/tmp',
'memcached_host' => 'localhost',
'memcached_port' => 11211,
);
// Make sure all passed options are known
@ -124,6 +127,9 @@ class CacheFactory
/**
* Builds cache pool instance.
*
* @todo Most likely the factory should return Ephemeral cache if a real
* storage cannot be used by some resons.
*
* @return PoolInterface An instance of cache pool.
* @throws \RuntimeException If at least one of factory's options is
* invalid.
@ -137,6 +143,16 @@ class CacheFactory
} elseif ($storage === 'file_system') {
$driver = new FileSystemDriver();
$driver->setOptions(array('path' => $this->getOption('path')));
} elseif ($storage === 'memcached') {
$driver = new MemcacheDriver();
$driver->setOptions(array(
'servers' => array(
$this->getOption('memcached_host'),
$this->getOption('memcached_port'),
),
// Use only PHP's "memcached" extension.
'extension' => 'memcached'
));
} else {
throw new \RuntimeException(sprintf(
'Wrong value of "storage" option: "%s"',