Cleanup: Updated copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / tasks / class.tx_scheduler_cachingframeworkgarbagecollection.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 Christian Kuhn <lolli@schwarzbu.ch>
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 * Garbage collection of caching framework cache backends.
27 *
28 * This task finds all configured caching framework caches and
29 * calls the garbage collection of a cache if the cache backend
30 * is configured to be cleaned.
31 *
32 * @author Christian Kuhn <lolli@schwarzbu.ch>
33 * @package TYPO3
34 * @subpackage scheduler
35 */
36 class tx_scheduler_CachingFrameworkGarbageCollection extends tx_scheduler_Task {
37 /**
38 * Backend types that should be cleaned up,
39 * set by additional field provider.
40 *
41 * @var array Selected backends to do garbage collection for
42 */
43 public $selectedBackends = array();
44
45 /**
46 * Execute garbage collection, called by scheduler.
47 *
48 * @return void
49 */
50 public function execute() {
51 // Don't do anything if caching framework is not used at all
52 if (t3lib_cache::isCachingFrameworkInitialized()) {
53 // Global subarray with all configured caches
54 $cacheConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'];
55
56 if (is_array($cacheConfigurations)) {
57 // Iterate through configured caches and call garbage collection if
58 // backend is within selected backends in additonal field of task
59 foreach ($cacheConfigurations as $cacheName => $cacheConfiguration) {
60 // The cache backend used for this cache
61 $usedCacheBackend = $cacheConfiguration['backend'];
62
63 if (in_array($usedCacheBackend, $this->selectedBackends)) {
64 $this->callGarbageCollectionOfCache($cacheName, $cacheConfiguration);
65 }
66 }
67 }
68 }
69
70 return(TRUE);
71 }
72
73 /**
74 * Get an instance of cache and call garbage collection
75 *
76 * @param string Cache name
77 * @param array Cache configuration
78 */
79 protected function callGarbageCollectionOfCache($cacheName, array $cacheConfiguration) {
80 // Get existing cache instance or create a new one
81 try {
82 $cache = $GLOBALS['typo3CacheManager']->getCache($cacheName);
83 } catch (t3lib_cache_exception_NoSuchCache $exception) {
84 $GLOBALS['typo3CacheFactory']->create(
85 $cacheName,
86 $cacheConfiguration['frontend'],
87 $cacheConfiguration['backend'],
88 $cacheConfiguration['options']
89 );
90 $cache = $GLOBALS['typo3CacheManager']->getCache($cacheName);
91 }
92
93 // Call garbage collection of this cache
94 $cache->collectGarbage();
95 }
96 } // End of class
97
98 if (defined('TYPO3_MODE') && $GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/scheduler/tasks/class.tx_scheduler_cachingframeworkgarbagecollection.php']) {
99 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/scheduler/tasks/class.tx_scheduler_cachingframeworkgarbagecollection.php']);
100 }
101
102 ?>