Add Field cache for 'Currently Open/Closed' Status formatter

Created on 17 July 2016, almost 8 years ago
Updated 14 April 2023, about 1 year ago

Hi all,

Basically what's happening is if the time is 3-6PM, at 6:01, display is sometimes displaying Open and sometimes displaying Closed. I'm assuming that is due to caching since if I rebuild cache it all gets sorted out.

I don't want to disable all caching since it is a massive performance boost, so what should happen next? Is there a way to disable caching specifically for this field instead? Ultimately, I plan on using the BigPipe functionality so if one field is not cached it shouldn't affect too much. I just don't want to disable caching on the whole display which is all static except for this one field.

FWIW, I am using a custom field formatter that I copy-pasted from the original, with a change made to the output into a table since it easier to CSS. Is there a tag or something that could just be added somewhere to disable caching? Some googling reveals "#post_render_cache" may be useful, but I can't tell if this has been finalized.

In the meantime, is it possible to do the following logic?:
If Current Status is enabled, then disable caching.

I have attached 2 images for reference. Current time here is Sunday 4:00PM so it should be OPEN.

🐛 Bug report
Status

Fixed

Version

1.6

Component

Code - formatter

Created by

🇹🇼Taiwan sgp913

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇫🇮Finland jhuhta

    I'm sorry to tell but the caching is still not quite right: please reopen the issue. (Found this issue again because my workaround patch stopped applying.)

    The max-age calculation works, and the status flag kind of works - but only if the caching layers are not doing their things. In a project, we have the Page Cache and Dynamic Page Cache on, then Varnish and a CDN in front. To my understanding, Varnish is repeating what the Page Cache knows after an additional page load, and maybe there's no mechanism for them to update their knowledge about the max-age being spent already at a moment of time.

    But to my surprise, the problem actually is reproducible on a local too, just having the Drupal caches on!

    Steps to reproduce:
    * have all Drupal caching on
    * set the closing time to be something that happens soon and let the time pass
    * reload page. The status flag should change to "closed" but it doesn't. Neither for authenticated nor unauthenticated requests.

    The content updates on reload if page_cache & dynamic_page_cache modules are disabled, or if the node is re-saved with no changes, or drush cr is run.

    For us to be able to use this open/closed formatter at all, we really need to have a mechanism to update its content dynamically. Hence, my patch rerolled here against the current dev / 1.8. You can test the patch by retrying the steps above. You might see the old value flashing briefly before it gets updated.

  • 🇳🇱Netherlands johnv

    @jhuhta, thanks for your feedback.
    Normally drupal best practice says to open a new issue. But let us decide on that later.

    I havesome questions, but they are nicely anwered in your post: anonymous user vs. authenticated user? Both mentioned caching modules activated or not:
    -- Internal Dynamic Page Cache - "Caches pages, including those with dynamic content, for all users."
    -- Internal Page Cache - "Caches pages for anonymous users and can be used when external page cache is not available."

    Drupal core handles both differently, and has some other problems, so reasons for the problem might be:
    - The module calculates a wrong expiration timetimestamp (This should be corrected in the module)
    - The expiration timestamp is not propagated correctly from field level to page level (This should not be a problem of this module,and must be solved in core)
    - The caching for anonymous user is flawed. As work-around, a cron job for table cache_render is added in 🐛 Fix Field cache for anonymous users using Cron job in Status formatter Closed: won't fix . Regarding this, please check 🐛 Cron for cache handling breaks badly Fixed where we are actively working on it.

    The problem for anonymous users should also be solved: display the node, wait for the time to expire, run cron, re-display the node.
    Is that the case, too?
    There should be no problem with authenticated users (at least in my basic test system). So, I am surprised by that test result.

    I understand your solution is to move the open/closed status to JS. In the past, I discarded that solution, since all errors were due to calculation errors in the office_hours module.

    At this point, I am happy with your solution, since it is better than the cron job solution.
    I will review it. I think it is beter to open a new issue "Handle Status formatter in JS"
    Please check the other issues.

    Alos, it might be needed to refresh the complete formatter, since the module now supports exception dates and seasons.

  • 🇫🇮Finland jhuhta

    Thank you for your reply! As per your advice, I opened a follow up issue: Fix Field cache for anonymous users using JS callback in Status formatter Fixed . Let's continue the discussion there about my potential solution.

    By removing just one of the *_cache modules, I was able to get the status working for either anonymous or authenticated users, but I don't have the exact results here anymore.

    I'm not yet familiar with the new Seasons feature, and we have implemented the opening time exceptions in a custom way long time ago.

Production build 0.69.0 2024