简体   繁体   中英

Disable Cache in TYPO3 in a USER_INT function

I am working on a TYPO3 project where I have to dynamically disable caching based on a condition. It is a very specific usecase, that will not happen a lot.

I planned to use a USER_INT function, where I would perform the check and disable the cache if necessary. The USER_INT function works flawlessly, it is being called on every page load.

The thing is, I can not disable the cache, or at least I do not know how.

The code, I have right now:

page = PAGE
page {
    typeNum = 0
    adminPanelStyles = 0
    11 = USER_INT
    11.userFunc = [COMPANY_NAMESPACE]\PageHandler->checkCache

And in the function I perform the check:

public function checkCache($content,$conf){
    global $TSFE; 

    $id = $TSFE->id;

    if($this->checkIfDisableCache($id)){
        //$TSFE->set_no_cache(); // <---- first I tried this one
        $TSFE->no_cache=true; // <-----after a while I got despoerate and tried to disable it directly
    }
}

I also tried to play with the config, it did not work. The funny thing is, if I set it directly in typoscript:

config.no_cache = 1

it works, but since the check is rather complex, I want to use PHP to determine, if the cache should be disabled.

I know I am doing something wrong, I just don't know what. Any help would be appretiated :)

if you look at the pibase (AbstractPlugin) code you will see that probably setting $conf['useCacheHash'] and $conf['no_cache'] should be done.

https://api.typo3.org/typo3cms/current/html/_abstract_plugin_8php_source.html#l00190

I don't think either of the previous answers really explain the situation. You have sort of a catch-22 here, in that your USER_INT is executed after the page cache entry has been generated. The way it works internally is everything that can be cached gets rendered first, and every USER_INT then outputs a marker in the HTML source which gets replaced afterwards. This way the cache can contain the version with markers and those can be rendered without having to render the whole page.

So what you need to do in this case if you want the page cache to be disabled only in some conditions, is to use a custom TypoScript condition that is capable of setting config.no_cache = 1 only under special circumstances. That way you prevent generating a cache entry if the condition is met, but preserve full caching and cached output for every other request.

https://docs.typo3.org/typo3cms/TyposcriptSyntaxReference/TypoScriptParserApi/CustomConditions/Index.html

Note that it is still recommended that you instead create the parts of your page that must not be cached, as USER_INT objects. Having a use case where you in some cases need to disable the entire page cache indicates a possible misunderstanding of how the caching framework and/or USER_INT works. Hopefully the above explains those parts a bit.

If you create this object as USER_INT, it will be rendered non-cached, outside the main page-rendering. https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/UserAndUserInt/Index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM