Encapsulate cache pool instantiation in CacheFactory

This commit is contained in:
Dmitriy Simushev 2015-03-20 14:59:11 +00:00
parent 57182b0589
commit 758203b6d3
4 changed files with 182 additions and 10 deletions

View File

@ -39,6 +39,19 @@ mailer:
# "tls".
encryption: false
# Cache subsystem
cache:
# This value determines where the cached data will be stored. Possible
# values are "none" and "file_system".
#
# If "none" is used the caching will be disabled. This option should be used
# only if none of the other options works.
#
# 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.
storage: file_system
# Locales
## Native name will be used in this locale
home_locale: en

View File

@ -22,6 +22,7 @@ require_once(dirname(__FILE__) . '/libs/init.php');
use Mibew\Application;
use Mibew\Authentication\AuthenticationManager;
use Mibew\Cache\CacheFactory;
use Mibew\Mail\MailerFactory;
use Mibew\Routing\Router;
use Mibew\Routing\Loader\CacheLoader;
@ -31,14 +32,17 @@ use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Loader\YamlFileLoader;
// Use custom cache
$cache_driver = new \Stash\Driver\FileSystem();
$cache_driver->setOptions(array('path' => MIBEW_FS_ROOT . '/cache/stash'));
$cache = new \Stash\Pool($cache_driver);
$configs = load_system_configs();
// Prepare the cache
$cache_factory = new CacheFactory($configs['cache']);
// For now directory for cache files cannot be changed via the configs file.
// TODO: Evaluate possibility of using custom cache directory.
$cache_factory->setOption('path', MIBEW_FS_ROOT . '/cache/stash');
// The main route loader which loads nothig but works as a cache proxy for other
// loaders.
$route_loader = new CacheLoader($cache);
$route_loader = new CacheLoader($cache_factory->getCache());
// Real loaders are attached via the resolver.
$loader_resolver = new LoaderResolver(array(
$route_loader,
@ -49,10 +53,9 @@ $loader_resolver = new LoaderResolver(array(
$router = new Router($route_loader, 'configs/routing.yml');
$application = new Application($router, new AuthenticationManager());
$application->setCache($cache);
$application->setCache($cache_factory->getCache());
// Use custom config-dependent mailer factory
$configs = load_system_configs();
$application->setMailerFactory(new MailerFactory($configs['mailer']));
// Process request

View File

@ -0,0 +1,152 @@
<?php
/*
* This file is a part of Mibew Messenger.
*
* Copyright 2005-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Mibew\Cache;
use Stash\Interfaces\PoolInterface;
use Stash\Driver\Ephemeral as EphemeralDriver;
use Stash\Driver\FileSystem as FileSystemDriver;
use Stash\Pool as CachePool;
/**
* Creates and configures appropriate instance of Cache pool.
*/
class CacheFactory
{
/**
* @var PoolInterface|null
*/
private $cache = null;
/**
* List of factory's options.
*
* @var array
*/
private $options;
/**
* Class constructor.
*
* @param Array $options Associative array of options that should be used.
*/
public function __construct($options = array())
{
$this->setOptions($options);
}
/**
* Gets factory's option.
*
* @param string $name Name of the option to retrieve.
* @throws \InvalidArgumentException If the option is unknown.
*/
public function getOption($name)
{
if (!isset($this->options[$name])) {
throw new \InvalidArgumentException(sprintf('Unknown option "%s"', $name));
}
return $this->options[$name];
}
/**
* Sets factory's option.
*
* @param string $name Name of the option to set.
* @param string $value New value.
* @throws \InvalidArgumentException If the option is unknown.
*/
public function setOption($name, $value)
{
if (!isset($this->options[$name])) {
throw new \InvalidArgumentException(sprintf('Unknown option "%s"', $name));
}
$this->options[$name] = $value;
// Cached instance of cache is not valid any more. New one should be
// created.
$this->cache = null;
}
/**
* Sets factory's options.
*
* @param array $options Associative array of options.
* @throws \InvalidArgumentException If specified array has unknow options.
*/
public function setOptions($options)
{
$defaults = array(
'storage' => 'file_system',
'path' => '/tmp',
);
// Make sure all passed options are known
$unknown_options = array_diff(array_keys($options), array_keys($defaults));
if (count($unknown_options) != 0) {
throw new \InvalidArgumentException(sprintf(
'These options are unknown: %s',
implode(', ', $unknown_options)
));
}
if (empty($this->options)) {
// The options are set for the first time.
$this->options = $options + $defaults;
} else {
// Update only specified options.
$this->options = $options + $this->options;
}
// Cached instance of cache is not valid any more. New one should be
// created.
$this->cache = null;
}
/**
* Builds cache pool instance.
*
* @return PoolInterface An instance of cache pool.
* @throws \RuntimeException If at least one of factory's options is
* invalid.
*/
public function getCache()
{
if (is_null($this->cache)) {
$storage = $this->getOption('storage');
if ($storage === 'none') {
$driver = new EphemeralDriver();
} elseif ($storage === 'file_system') {
$driver = new FileSystemDriver();
$driver->setOptions(array('path' => $this->getOption('path')));
} else {
throw new \RuntimeException(sprintf(
'Wrong value of "storage" option: "%s"',
$storage
));
}
$this->cache = new CachePool($driver);
}
return $this->cache;
}
}

View File

@ -33,9 +33,13 @@ function load_system_configs()
if (is_null($configs)) {
$parser = new YamlParser();
$configs = $parser->parse(file_get_contents(MIBEW_FS_ROOT . '/configs/config.yml'));
// Mailer configs are not necessary and can be omited but the section
// must exist anyway.
$configs += array('mailer' => array());
$configs += array(
// Mailer configs are not necessary and can be omited but the
// section must exist anyway.
'mailer' => array(),
// Cache section must extst too.
'cache' => array(),
);
}
return $configs;