[BUGFIX] Caching framework: Improve configuration handling
authorChristian Kuhn <lolli@schwarzbu.ch>
Fri, 24 Jun 2011 19:54:14 +0000 (21:54 +0200)
committerSteffen Gebert <steffen.gebert@typo3.org>
Sat, 25 Jun 2011 10:47:36 +0000 (12:47 +0200)
The cache manager takes care of correct cache configurations
given to the cache factory. The patch improves the handling of
default configuration for lazy cache setups.

Registration of new cache frontends or backends in TYPO3_CONF_VARS
is removed, new cache frontends or backends of extensions can be
defined by registering them to the autoloader only.

Change-Id: I62fd755b16d84dae2d13f0aab0062c95df2232c7
Resolves: #27689
Reviewed-on: http://review.typo3.org/2921
Reviewed-by: Philipp Gampe
Tested-by: Philipp Gampe
Reviewed-by: Susanne Moog
Reviewed-by: Steffen Gebert
Tested-by: Steffen Gebert
NEWS.txt
t3lib/cache/class.t3lib_cache_factory.php
t3lib/cache/class.t3lib_cache_manager.php
t3lib/config_default.php
tests/t3lib/cache/class.t3lib_cache_factoryTest.php
tests/t3lib/cache/class.t3lib_cache_managerTest.php

index 21022d8..fff8b3b 100644 (file)
--- a/NEWS.txt
+++ b/NEWS.txt
@@ -152,4 +152,16 @@ Security
 Performance
 -------------------------------------------------------------------------------
 
-...
\ No newline at end of file
+* Switch to caching framework
+
+The caching framework is enabled by default and the old database-only driven
+caching is removed. This opens a lot of options for administrators to optimize
+cache deployment and performance. Details about configuration options and usage
+in own extensions can be found at http://wiki.typo3.org/Caching_framework.
+
+
+* Caching framework: db backend uses own table namespace
+
+The database backend must not define own tables and table definitions anymore.
+Cache tables and table layout of the database backend will created by the
+caching framework when needed.
\ No newline at end of file
index 6d8d113..775ae8b 100644 (file)
@@ -81,72 +81,29 @@ class t3lib_cache_Factory implements t3lib_Singleton {
         * @api
         */
        public function create($cacheIdentifier, $cacheObjectName, $backendObjectName, array $backendOptions = array()) {
-
-               $backendReference = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheBackends'][$backendObjectName];
-
-               if (strpos($backendReference, ':') === FALSE) {
-                       $backendClassReference = $backendReference;
-               } else {
-                       t3lib_div::deprecationLog("Configuring cacheBackend with filename is deprecated since TYPO3 4.5. Use the autoloader instead.");
-                               // Loading the cache backend file and class
-                       list($backendFile, $backendClassReference) = explode(
-                               ':',
-                               $backendReference
-                       );
-
-                       $backendRequireFile = t3lib_div::getFileAbsFileName($backendFile);
-                       if ($backendRequireFile) {
-                               t3lib_div::requireOnce($backendRequireFile);
-                       }
-               }
-
-               $backend = t3lib_div::makeInstance($backendClassReference, $this->context, $backendOptions);
-
+               $backend = t3lib_div::makeInstance($backendObjectName, $this->context, $backendOptions);
                if (!$backend instanceof t3lib_cache_backend_Backend) {
                        throw new t3lib_cache_exception_InvalidBackend(
-                               '"' . $backendObjectName . '" is not a valid cache backend.',
+                               '"' . $backendObjectName . '" is not a valid cache backend object.',
                                1216304301
                        );
                }
-
                if (is_callable(array($backend, 'initializeObject'))) {
                        $backend->initializeObject();
                }
 
-
-               $cacheReference = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheFrontends'][$cacheObjectName];
-               $cacheReference = $cacheReference ? $cacheReference : 't3lib_cache_frontend_VariableFrontend';
-
-               if (strpos($cacheReference, ':') === FALSE) {
-                       $cacheClassReference = $cacheReference;
-               } else {
-                       t3lib_div::deprecationLog("Configuring cacheFrontends with filename is deprecated since TYPO3 4.5. Use the autoloader instead.");
-                               // loading the cache frontend file and class
-                       list($cacheFile, $cacheClassReference) = explode(
-                               ':',
-                               $cacheReference
-                       );
-
-                       $cacheRequireFile = t3lib_div::getFileAbsFileName($cacheFile);
-                       if ($cacheRequireFile) {
-                               t3lib_div::requireOnce($cacheRequireFile);
-                       }
-               }
-               $cache = t3lib_div::makeInstance($cacheClassReference, $cacheIdentifier, $backend);
-
+               $cache = t3lib_div::makeInstance($cacheObjectName, $cacheIdentifier, $backend);
                if (!$cache instanceof t3lib_cache_frontend_Frontend) {
                        throw new t3lib_cache_exception_InvalidCache(
-                               '"' . $cacheObjectName . '" is not a valid cache.',
+                               '"' . $cacheObjectName . '" is not a valid cache frontend object.',
                                1216304300
                        );
                }
-
                if (is_callable(array($cache, 'initializeObject'))) {
                        $cache->initializeObject();
                }
 
                $this->cacheManager->registerCache($cache);
-
                return $cache;
        }
 
index 844dd59..0d6503a 100644 (file)
@@ -49,6 +49,15 @@ class t3lib_cache_Manager implements t3lib_Singleton {
         */
        protected $cacheConfigurations = array();
 
+       /**
+        * @var array Default cache configuration as fallback
+        */
+       protected $defaultCacheConfiguration = array(
+               'frontend' => 't3lib_cache_frontend_VariableFrontend',
+               'backend' => 't3lib_cache_backend_DbBackend',
+               'options' => array(),
+       );
+
        /**
         * @param t3lib_cache_Factory $cacheFactory
         * @return void
@@ -246,9 +255,24 @@ class t3lib_cache_Manager implements t3lib_Singleton {
         * @author Robert Lemke <robert@typo3.org>
         */
        protected function createCache($identifier) {
-               $frontend = $this->cacheConfigurations[$identifier]['frontend'];
-               $backend = $this->cacheConfigurations[$identifier]['backend'];
-               $backendOptions = $this->cacheConfigurations[$identifier]['options'];
+               if (isset($this->cacheConfigurations[$identifier]['frontend'])) {
+                       $frontend = $this->cacheConfigurations[$identifier]['frontend'];
+               } else {
+                       $frontend = $this->defaultCacheConfiguration['frontend'];
+               }
+
+               if (isset($this->cacheConfigurations[$identifier]['backend'])) {
+                       $backend = $this->cacheConfigurations[$identifier]['backend'];
+               } else {
+                       $backend = $this->defaultCacheConfiguration['backend'];
+               }
+
+               if (isset($this->cacheConfigurations[$identifier]['options'])) {
+                       $backendOptions = $this->cacheConfigurations[$identifier]['options'];
+               } else {
+                       $backendOptions = $this->defaultCacheConfiguration['options'];
+               }
+
                $this->cacheFactory->create($identifier, $frontend, $backend, $backendOptions);
        }
 }
index 6d440cb..53d7fbe 100644 (file)
@@ -117,21 +117,6 @@ $TYPO3_CONF_VARS = array(
                'reverseProxySSL' => '',                                // String: '*' or list of IP addresses of proxies that use SSL (https) for the connection to the client, but an unencrypted connection (http) to the server. If '*' all proxies defined in <a href="#SYS-reverseProxyIP">[SYS][reverseProxyIP]</a> use SSL.
                'reverseProxyPrefixSSL' => '',                          // String: prefix to be added to the internal URL (SCRIPT_NAME and REQUEST_URI) when accessing the server via an SSL proxy. This setting overrides <a href="#SYS-reverseProxyPrefix">[SYS][reverseProxyPrefix]</a>.
                'caching' => array(
-                       'cacheFrontends' => array(
-                               't3lib_cache_frontend_VariableFrontend' => 't3lib_cache_frontend_VariableFrontend',
-                               't3lib_cache_frontend_StringFrontend'   => 't3lib_cache_frontend_StringFrontend',
-                               't3lib_cache_frontend_PhpFrontend'      => 't3lib_cache_frontend_PhpFrontend',
-                       ),
-                       'cacheBackends' => array(
-                               't3lib_cache_backend_DbBackend'              => 't3lib_cache_backend_DbBackend',
-                               't3lib_cache_backend_FileBackend'            => 't3lib_cache_backend_FileBackend',
-                               't3lib_cache_backend_MemcachedBackend'       => 't3lib_cache_backend_MemcachedBackend',
-                               't3lib_cache_backend_PdoBackend'             => 't3lib_cache_backend_PdoBackend',
-                               't3lib_cache_backend_RedisBackend'           => 't3lib_cache_backend_RedisBackend',
-                               't3lib_cache_backend_ApcBackend'             => 't3lib_cache_backend_ApcBackend',
-                               't3lib_cache_backend_NullBackend'            => 't3lib_cache_backend_NullBackend',
-                               't3lib_cache_backend_TransientMemoryBackend' => 't3lib_cache_backend_TransientMemoryBackend',
-                       ),
                        'cacheConfigurations' => array(
                                'cache_hash' => array(
                                        'frontend' => 't3lib_cache_frontend_VariableFrontend',
index 67d6988..e944b43 100644 (file)
@@ -35,16 +35,6 @@ require_once 'backend/class.t3lib_cache_backend_mockbackend.php';
  */
 class t3lib_cache_FactoryTest extends tx_phpunit_testcase {
 
-       /**
-        * Sets up this testcase
-        *
-        * @return void
-        * @author Ingo Renner <ingo@typo3.org>
-        */
-       public function setUp() {
-               $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheBackends']['t3lib_cache_backend_MockBackend'] = 't3lib_cache_backend_MockBackend';
-       }
-
        /**
         * @test
         * @author Robert Lemke <robert@typo3.org>
index 635980b..f054799 100644 (file)
@@ -144,6 +144,107 @@ class t3lib_cache_ManagerTest extends tx_phpunit_testcase {
                $manager->flushCaches();
        }
 
+       /**
+        * @test
+        */
+       public function getCacheCreatesCacheInstanceWithGivenConfiguration() {
+               $manager = new t3lib_cache_Manager();
+
+               $cacheIdentifier = 'Test' . md5(uniqid(mt_rand(), TRUE));
+               $cacheObjectName = 'testCache';
+               $backendObjectName = 'testBackend';
+               $backendOptions = array('foo');
+
+               $configuration = array(
+                       $cacheIdentifier => array(
+                               'frontend' => $cacheObjectName,
+                               'backend' => $backendObjectName,
+                               'options' => $backendOptions,
+                       ),
+               );
+
+               $factory = $this->getMock('t3lib_cache_Factory', array('create'), array(), '', FALSE);
+               $factory->expects($this->once())->method('create')->with($cacheIdentifier, $cacheObjectName, $backendObjectName, $backendOptions);
+
+               $manager->injectCacheFactory($factory);
+               $manager->setCacheConfigurations($configuration);
+               $manager->getCache($cacheIdentifier);
+       }
+
+       /**
+        * @test
+        */
+       public function getCacheCreatesCacheInstanceWithFallbackToDefaultFrontend() {
+               $manager = new t3lib_cache_Manager();
+
+               $cacheIdentifier = 'Test' . md5(uniqid(mt_rand(), TRUE));
+               $backendObjectName = 'testBackend';
+               $backendOptions = array('foo');
+
+               $configuration = array(
+                       $cacheIdentifier => array(
+                               'backend' => $backendObjectName,
+                               'options' => $backendOptions,
+                       ),
+               );
+
+               $factory = $this->getMock('t3lib_cache_Factory', array('create'), array(), '', FALSE);
+               $factory->expects($this->once())->method('create')->with($cacheIdentifier, 't3lib_cache_frontend_VariableFrontend', $backendObjectName, $backendOptions);
+
+               $manager->injectCacheFactory($factory);
+               $manager->setCacheConfigurations($configuration);
+               $manager->getCache($cacheIdentifier);
+       }
+
+       /**
+        * @test
+        */
+       public function getCacheCreatesCacheInstanceWithFallbackToDefaultBackend() {
+               $manager = new t3lib_cache_Manager();
+
+               $cacheIdentifier = 'Test' . md5(uniqid(mt_rand(), TRUE));
+               $cacheObjectName = 'testCache';
+               $backendOptions = array('foo');
+
+               $configuration = array(
+                       $cacheIdentifier => array(
+                               'frontend' => $cacheObjectName,
+                               'options' => $backendOptions,
+                       ),
+               );
+
+               $factory = $this->getMock('t3lib_cache_Factory', array('create'), array(), '', FALSE);
+               $factory->expects($this->once())->method('create')->with($cacheIdentifier, $cacheObjectName, 't3lib_cache_backend_DbBackend', $backendOptions);
+
+               $manager->injectCacheFactory($factory);
+               $manager->setCacheConfigurations($configuration);
+               $manager->getCache($cacheIdentifier);
+       }
+
+       /**
+        * @test
+        */
+       public function getCacheCreatesCacheInstanceWithFallbackToDefaultBackenOptions() {
+               $manager = new t3lib_cache_Manager();
+
+               $cacheIdentifier = 'Test' . md5(uniqid(mt_rand(), TRUE));
+               $cacheObjectName = 'testCache';
+               $backendObjectName = 'testBackend';
+
+               $configuration = array(
+                       $cacheIdentifier => array(
+                               'frontend' => $cacheObjectName,
+                               'backend' => $backendObjectName,
+                       ),
+               );
+
+               $factory = $this->getMock('t3lib_cache_Factory', array('create'), array(), '', FALSE);
+               $factory->expects($this->once())->method('create')->with($cacheIdentifier, $cacheObjectName, $backendObjectName, array());
+
+               $manager->injectCacheFactory($factory);
+               $manager->setCacheConfigurations($configuration);
+               $manager->getCache($cacheIdentifier);
+       }
 }
 
 ?>
\ No newline at end of file