Merge "[+FEATURE] implement ObjectManager::isRegistered()"
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Service / CacheService.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Extbase Team
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 * Cache clearing helper functions
27 *
28 * @package Extbase
29 * @subpackage Service
30 */
31 class Tx_Extbase_Service_CacheService implements t3lib_Singleton {
32
33 /**
34 * This is a wrapper for the global constant "TYPO3_UseCachingFramework" to make this class testable
35 * @var boolean
36 */
37 protected $useCachingFramework = TYPO3_UseCachingFramework;
38
39 /**
40 * Clears the page cache
41 *
42 * @param mixed $pageIdsToClear (int) single or (array) multiple pageIds to clear the cache for
43 * @return void
44 */
45 public function clearPageCache($pageIdsToClear = NULL) {
46 if ($pageIdsToClear !== NULL && !is_array($pageIdsToClear)) {
47 $pageIdsToClear = array(intval($pageIdsToClear));
48 }
49
50 $this->flushPageCache($pageIdsToClear);
51 $this->flushPageSectionCache($pageIdsToClear);
52 }
53
54 /**
55 * Flushes cache_pages or cachingframework_cache_pages.
56 *
57 * @param array $pageIdsToClear pageIds to clear the cache for
58 * @return void
59 */
60 protected function flushPageCache($pageIds = NULL) {
61 if ($this->useCachingFramework) {
62 $pageCache = $GLOBALS['typo3CacheManager']->getCache('cache_pages');
63
64 if ($pageIds !== NULL) {
65 foreach ($pageIds as $pageId) {
66 $pageCache->flushByTag('pageId_' . $pageId);
67 }
68 } else {
69 $pageCache->flush();
70 }
71 } elseif ($pageIds !== NULL) {
72 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
73 'cache_pages',
74 'page_id IN (' . implode(',', $GLOBALS['TYPO3_DB']->cleanIntArray($pageIds)) . ')'
75 );
76 } else {
77 $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery('cache_pages');
78 }
79 }
80
81 /**
82 * Flushes cache_pagesection or cachingframework_cache_pagesection.
83 *
84 * @param array $pageIdsToClear pageIds to clear the cache for
85 * @return void
86 */
87 protected function flushPageSectionCache($pageIds = NULL) {
88 if ($this->useCachingFramework) {
89 $pageSectionCache = $GLOBALS['typo3CacheManager']->getCache('cache_pagesection');
90
91 if ($pageIds !== NULL) {
92 foreach ($pageIds as $pageId) {
93 $pageSectionCache->flushByTag('pageId_' . $pageId);
94 }
95 } else {
96 $pageSectionCache->flush();
97 }
98 } elseif ($pageIds !== NULL) {
99 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
100 'cache_pagesection',
101 'page_id IN (' . implode(',', $GLOBALS['TYPO3_DB']->cleanIntArray($pageIds)) . ')'
102 );
103 } else {
104 $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery('cache_pagesection');
105 }
106 }
107 }
108 ?>