TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Scheduler\Task;
|
||||
|
||||
use TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend;
|
||||
use TYPO3\CMS\Core\Cache\CacheManager;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Garbage collection of caching framework cache backends.
|
||||
*
|
||||
* This task finds all configured caching framework caches and
|
||||
* calls the garbage collection of a cache if the cache backend
|
||||
* is configured to be cleaned.
|
||||
* @internal This class is a specific scheduler task implementation is not considered part of the Public TYPO3 API.
|
||||
*/
|
||||
class CachingFrameworkGarbageCollectionTask extends AbstractTask
|
||||
{
|
||||
/**
|
||||
* Backend types that should be cleaned up,
|
||||
* set by additional field provider.
|
||||
*
|
||||
* @var array Selected backends to do garbage collection for
|
||||
*/
|
||||
public $selectedBackends = [];
|
||||
|
||||
/**
|
||||
* Execute garbage collection, called by scheduler.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
// Global sub-array with all configured caches
|
||||
$cacheConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] ?? null;
|
||||
if (is_array($cacheConfigurations)) {
|
||||
// Iterate through configured caches and call garbage collection if
|
||||
// backend is within selected backends in additional field of task
|
||||
foreach ($cacheConfigurations as $cacheName => $cacheConfiguration) {
|
||||
// The cache backend used for this cache
|
||||
$usedCacheBackend = $cacheConfiguration['backend'] ?? Typo3DatabaseBackend::class;
|
||||
if (in_array($usedCacheBackend, $this->selectedBackends, true)) {
|
||||
GeneralUtility::makeInstance(CacheManager::class)->getCache($cacheName)->collectGarbage();
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getTaskParameters(): array
|
||||
{
|
||||
return [
|
||||
'cache_backends' => implode(',', $this->selectedBackends),
|
||||
];
|
||||
}
|
||||
|
||||
public function setTaskParameters(array $parameters): void
|
||||
{
|
||||
$selectedBackends = $parameters['selectedBackends'] ?? $parameters['cache_backends'] ?? [];
|
||||
if (!is_array($selectedBackends)) {
|
||||
$selectedBackends = GeneralUtility::trimExplode(',', $selectedBackends, true);
|
||||
}
|
||||
$this->selectedBackends = $selectedBackends;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered caching framework backends
|
||||
*/
|
||||
public function getRegisteredBackends(array &$config): void
|
||||
{
|
||||
$backends = [];
|
||||
$cacheConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'];
|
||||
foreach ($cacheConfigurations ?? [] as $cacheConfiguration) {
|
||||
$backend = (string)($cacheConfiguration['backend'] ?? Typo3DatabaseBackend::class);
|
||||
if (!in_array($backend, $backends, true)) {
|
||||
$backends[] = $backend;
|
||||
}
|
||||
}
|
||||
foreach ($backends as $backend) {
|
||||
$config['items'][] = ['value' => $backend, 'label' => $backend];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user