Search code examples
typo3typoscript

How to define custom cache lifetime for custom content elements?


Shouldn't TypoScript cache work for custom content elements like this:

tt_content {
    my_custom_element =< lib.contentElement
    my_custom_element {
        // tested also with stdWrap.cache
        cache {
            key = my_custom_element
            // 10 seconds to test
            lifetime = 10
        }
        templateName = MyCustomElement
        dataProcessing.10 = Example\Project\DataProcessing\FetchDataProcessor
        dataProcessing.10 {
            fetchUri = https://www.example.com/resource
            as = fetched_data
        }
    }
}

...or do I interpret it the wrong way?
https://docs.typo3.org/m/typo3/reference-typoscript/master/en-us/Functions/Cache.html
I would expect to define a custom cache lifetime for this content element.


Solution

  • Possible solution:

    tt_content {
        // COA_INT to ensure the block .my_custom_element isn't cached
        my_custom_element = COA_INT
        my_custom_element {
            10 =< lib.contentElement
            10 {
                // Re-add cache for the block .10
                cache {
                    // Use UID of content element when user can edit content of it
                    key = my_custom_element_{field:uid}
                    key.insertData = 1
                    // For test currently 10 seconds
                    lifetime = 10
                }
                templateName = MyCustomElement
                dataProcessing.10 = Example\Project\DataProcessing\FetchDataProcessor
                dataProcessing.10 {
                    fetchUri = https://www.example.com/resource
                    as = fetched_data
                }
            }
        }
    }
    

    BUT it has (huge) drawbacks:

    • The page HTML, where the content element is placed on, won't be cached by the browser.
    • And a static file cache extension won't work for this pages too.