Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #166198 Reply
    Cinzia
    Guest

    Hi,
    this code integrates W3 Total Cache’s “fragment cache” with a small helper, and then uses it to cache the current time for 60 seconds.

    // Fragment cache helper function
    function fragment_cache($key, $function) {
    $group = ‘capelli24’;
    $key = apply_filters(‘fragment_cache_prefix’, ‘fragment_cache_’) . $key;
    if (function_exists(‘w3tc_fragmentcache_start’)) {
    if (!w3tc_fragmentcache_start($key, $group)) {
    call_user_func($function);
    w3tc_fragmentcache_end($key, $group);
    }
    } else {
    call_user_func($function);
    }
    }

    // Register fragment cache group with 60s TTL
    add_action(‘w3tc_register_fragment_groups’, function() {
    if (function_exists(‘w3tc_register_fragment_group’)) {
    w3tc_register_fragment_group(‘capelli24’, [], 60);
    }
    });

    // Using the helper in a template to cache the current time
    <?php
    // Shows the current time, cached for 60 seconds using W3TC fragment cache
    fragment_cache(‘band’, function() { ?>
    <?php echo date(‘H:i:s’); ?>
    <?php }); ?>

    In the WordPress admin area, where page cache is disabled, this code works as expected: the time is updated every 60 seconds.

    On the public front end, where W3TC page cache is enabled, the time does not refresh every 60 seconds.
    I always see the same HTML that was generated when the page cache entry was created, and the PHP fragment is not re-executed while the page cache is warm.
    It seems that the full page cache is served before the fragment cache logic runs, so the fragment TTL is never applied on cached pages.

    I am currently testing this in a local development environment with the constant define(‘W3TC_PRO_DEV_MODE’, true); set.

    Could you please confirm if fragment caching is supposed to work together with page caching in this scenario, or if pages that use fragment_cache() must be excluded from page cache?
    If there is a recommended configuration to allow this fragment to respect its 60-second TTL on the front end, I would like to apply it.

    Thank you.

    Cinzia

    #166423 Reply

    Hello Cinzia

    Thank you for reaching out, and I am happy to help!

    Just to confirm, are you using a Fragment Cache extension in the W3 Total Cache to set this up, or are you trying to set up Page Fragment Caching?

    Thank you for the clarification!

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: Reply #166423 in W3 Total Cache fragment cache TTL not respected when page cache is enabled
Your information: