[BUGFIX] Caching framework: Reduce code duplication in db backend
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_cache.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2008-2011 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * A cache handling helper class
27 *
28 * @author Ingo Renner <ingo@typo3.org>
29 * @package TYPO3
30 * @subpackage t3lib
31 */
32 class t3lib_cache {
33
34 /**
35 * @var boolean
36 */
37 protected static $isCachingFrameworkInitialized = FALSE;
38
39 /**
40 * Initializes the caching framework by loading the cache manager and factory
41 * into the global context.
42 *
43 * @return void
44 */
45 public static function initializeCachingFramework() {
46 if (!self::isCachingFrameworkInitialized()) {
47 $GLOBALS['typo3CacheManager'] = t3lib_div::makeInstance('t3lib_cache_Manager');
48 $GLOBALS['typo3CacheManager']->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
49 $GLOBALS['typo3CacheFactory'] = t3lib_div::makeInstance('t3lib_cache_Factory', 'production', $GLOBALS['typo3CacheManager']);
50 self::$isCachingFrameworkInitialized = TRUE;
51 }
52 }
53
54 /**
55 * initializes the cache_pages cache
56 *
57 * @return void
58 * @author Ingo Renner <ingo@typo3.org>
59 */
60 public static function initPageCache() {
61 try {
62 $GLOBALS['typo3CacheManager']->getCache('cache_pages');
63 } catch (t3lib_cache_exception_DuplicateIdentifier $e) {
64 // do nothing, a cache_pages cache already exists
65 }
66 }
67
68 /**
69 * initializes the cache_pagesection cache
70 *
71 * @return void
72 * @author Ingo Renner <ingo@typo3.org>
73 */
74 public static function initPageSectionCache() {
75 try {
76 $GLOBALS['typo3CacheManager']->getCache('cache_pagesection');
77 } catch (t3lib_cache_exception_DuplicateIdentifier $e) {
78 // do nothing, a cache_pagesection cache already exists
79 }
80 }
81
82 /**
83 * initializes the cache_hash cache
84 *
85 * @return void
86 * @author Ingo Renner <ingo@typo3.org>
87 */
88 public static function initContentHashCache() {
89 try {
90 $GLOBALS['typo3CacheManager']->getCache('cache_hash');
91 } catch (t3lib_cache_exception_DuplicateIdentifier $e) {
92 // do nothing, a cache_hash cache already exists
93 }
94 }
95
96 /**
97 * Determines whether the caching framework is initialized.
98 * The caching framework could be disabled for the core but used by an extension.
99 *
100 * @return boolean
101 */
102 public function isCachingFrameworkInitialized() {
103 if (!self::$isCachingFrameworkInitialized
104 && isset($GLOBALS['typo3CacheManager']) && $GLOBALS['typo3CacheManager'] instanceof t3lib_cache_Manager
105 && isset($GLOBALS['typo3CacheFactory']) && $GLOBALS['typo3CacheFactory'] instanceof t3lib_cache_Factory
106 ) {
107 self::$isCachingFrameworkInitialized = TRUE;
108 }
109
110 return self::$isCachingFrameworkInitialized;
111 }
112
113 /**
114 * Enables the caching framework for the core caches like cache_pages, cache_pagesection and cache_hash.
115 * This method can be called by extensions in their ext_localconf.php. Calling it later would not work,
116 * since rendering is already started using the defined caches.
117 *
118 * @return void
119 */
120 public function enableCachingFramework() {
121 if (!defined('TYPO3_UseCachingFramework')) {
122 $GLOBALS['TYPO3_CONF_VARS']['SYS']['useCachingFramework'] = 1;
123 } elseif (!TYPO3_UseCachingFramework) {
124 throw new RuntimeException(
125 'The caching framework was already defined to be disabled and cannot be changed. ' .
126 'Please put your call to t3lib_cache::enableCachingFramework() into ext_localconf.php.',
127 1253273131
128 );
129 }
130 }
131 }
132
133
134 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_cache.php'])) {
135 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_cache.php']);
136 }
137
138 ?>