From 1634607051341f6d348404cd4bc478cf2474a259 Mon Sep 17 00:00:00 2001 From: Thomas Steur Date: Tue, 16 Dec 2014 22:25:51 +0100 Subject: added support for different caching backends such as redis --- core/Cache.php | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 core/Cache.php (limited to 'core/Cache.php') diff --git a/core/Cache.php b/core/Cache.php new file mode 100644 index 0000000000..b90be8d27b --- /dev/null +++ b/core/Cache.php @@ -0,0 +1,117 @@ +get('Piwik\Cache\Lazy'); + } + + /** + * This class is used to cache any data during one request. It won't be persisted between requests and it can + * cache all kind of data, even objects or resources. This cache is very fast. + * + * @return Cache\Transient + */ + public static function getTransientCache() + { + return StaticContainer::getContainer()->get('Piwik\Cache\Transient'); + } + + /** + * This cache stores all its cache entries under one "cache" entry in a configurable backend. + * + * This comes handy for things that you need very often, nearly in every request. For example plugin metadata, the + * list of tracker plugins, the list of available languages, ... + * Instead of having to read eg. a hundred cache entries from files (or any other backend) it only loads one cache + * entry which contains the hundred keys. Should be used only for things that you need very often and only for + * cache entries that are not too large to keep loading and parsing the single cache entry fast. + * All cache entries it contains have the same life time. For fast performance it won't validate any cache ids. + * It is not possible to cache any objects using this cache. + * + * @return Cache\Eager + */ + public static function getEagerCache() + { + return StaticContainer::getContainer()->get('Piwik\Cache\Eager'); + } + + public static function flushAll() + { + self::getLazyCache()->flushAll(); + self::getTransientCache()->flushAll(); + self::getEagerCache()->flushAll(); + } + + /** + * @param $type + * @return Cache\Backend + */ + public static function buildBackend($type) + { + $factory = new Cache\Backend\Factory(); + $options = self::getOptions($type); + + $backend = $factory->buildBackend($type, $options); + + return $backend; + } + + private static function getOptions($type) + { + $options = self::getBackendOptions($type); + + switch ($type) { + case 'file': + + $options = array('directory' => StaticContainer::getContainer()->get('path.cache')); + break; + + case 'chained': + + foreach ($options['backends'] as $backend) { + $options[$backend] = self::getOptions($backend); + } + + break; + + case 'redis': + + if (!empty($options['timeout'])) { + $options['timeout'] = (float)Common::forceDotAsSeparatorForDecimalPoint($options['timeout']); + } + + break; + } + + return $options; + } + + private static function getBackendOptions($backend) + { + $key = ucfirst($backend) . 'Cache'; + $options = Config::getInstance()->$key; + + return $options; + } +} -- cgit v1.2.3