简体   繁体   English

在USER_INT函数中禁用TYPO3中的缓存

[英]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. 我正在TYPO3项目中工作,在该项目中我必须根据条件动态禁用缓存。 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. 我计划使用USER_INT函数,在该函数中将执行检查并在必要时禁用缓存。 The USER_INT function works flawlessly, it is being called on every page load. USER_INT函数可以完美工作,每次加载页面时都会调用它。

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. 它可以工作,但是由于检查相当复杂,因此我想使用PHP来确定是否应禁用缓存。

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. 如果您看一下pibase(AbstractPlugin)代码,您会发现应该完成$conf['useCacheHash']$conf['no_cache']设置。

https://api.typo3.org/typo3cms/current/html/_abstract_plugin_8php_source.html#l00190 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. 您在这里有一个catch 22,因为您的USER_INT是在生成页面缓存项之后执行的。 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. 它在内部的工作方式是首先呈现可缓存的所有内容,然后每个USER_INT在HTML源代码中输出一个标记,然后将该标记替换。 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. 因此,在这种情况下,如果只希望在某些情况下禁用页面缓存,则需要使用自定义的TypoScript条件,该条件只能在特殊情况下设置config.no_cache = 1 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 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. 请注意,仍然建议您改为将页面中不能缓存的部分创建为USER_INT对象。 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. 如果有一个用例,在某些情况下您需要禁用整个页面缓存,则表明可能对缓存框架和/或USER_INT工作方式有误解。 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. 如果您以USER_INT的身份创建此对象,则它将在主页面呈现之外呈现为非缓存形式。 https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/UserAndUserInt/Index.html https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/UserAndUserInt/Index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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