When using TYPO3-CLI or some TYPO3-independent processing, the frontend cache needs sometimes to be cleared.
Without using the caching framework, caches can be deleted directly in the database tables cache_pages, cache_pagesection and if needed, cache_hash.When using the caching framework, such as memcache or redis, we can not clear the cache directly in the database, we need to take another way to achieve this. The solution is simple - we need to initialize the TYPO3 caching framework and its frontend-caches. For TYPO3-CLI we don't need this step, because localconf.php is loaded and framework is initialised automatically, but for processing out of TYPO3 environment we must load the configuration first (usually by inclusion of localconf.php). Then we don't need to take care about the configuration or the type of cache, everythig is done by the caching framework.But how is it done?You can find the answer in the t3lib_cache library, which uses the cache manager and the cache factory to initialize the framework and the default frontend caches (page, pagesection and content hash). By using this class we have an initialized framework and we can use all the features the caching framework supplies.In the following example is in CLI mode the caching framework in localconf.php disabled, but the configuration for framework is loaded and special parameter "useCachingFramework_CLI" is set.
Example of clearing FE-cache for some pages in TYPO3-CLI):
if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['useCachingFramework_CLI']) { try { if (!t3lib_cache::isCachingFrameworkInitialized()) { t3lib_cache::initializeCachingFramework(); } // we can initialize caches repeatedly as well, duplicities are ignored ... t3lib_cache::initPageCache(); t3lib_cache::initPageSectionCache(); $pageCache = $GLOBALS['typo3CacheManager']->getCache('cache_pages'); $pageSectionCache = $GLOBALS['typo3CacheManager']->getCache('cache_pagesection'); foreach ($pageIds as $pageId) { $pageCache->flushByTag('pageId_' . $pageId); $pageSectionCache->flushByTag('pageId_' . $pageId); } } catch (Exception $ex) { // whatever } } else { $GLOBALS['TYPO3_DB']->exec_DELETEquery('cache_pages', 'page_id IN (' . implode(',', $pageIds) . ')'); $GLOBALS['TYPO3_DB']->exec_DELETEquery('cache_pagesection', 'page_id IN (' . implode(',', $pageIds) . ')'); }
More about the TYPO3 caching framework you can find here.
Have a lot of fun ...
Martin