4

I have set up access to some custom data by creating ContentEntity classes in my module, so I can query and display that data through Drupal entities. However, that data is also updated by a non-Drupal service, that changes some columns in the database directly. After the data is changed by that service I have to use "Clear all caches" in order to see any changes in Drupal.

How can I stop Drupal 8 from caching an entity type at all - so it always queries the fields fresh from the database?

I have tried annotating my Entity class with

render_cache = FALSE,
field_cache = FALSE

as these are the cache related options listed in the documentation (Structure of an Entity annotation) and suggested in How to avoid caching of fields in Entity View? However, this didn't have any noticeable effect in my scenario.

1
  • You could consider using the post render cache / lazy builder.
    – user21641
    Commented Aug 3, 2015 at 20:56

1 Answer 1

3

The flag you are looking for is persistent_cache, field_cache is no longer used, that documentation is a bit out of date. You're welcome to update it :)

See the API documentation of the EntityType class for a up to date list, maybe that documentation page should just link to that instead of trying to maintain a duplicate list.

That said, unless you have almost as many or more writes/changes to your entity type as loads, I'd considering changing your architecture and either update the entity type through the API or invalidate the changed entities. But there can be cases where that doesn't make sense, e.g. because of mass updates and if those entities aren't very frequently. But for example displaying them in views can be quite slow if they need to be loaded all the time.

Also, speaking of views, while render_cache disables the caching of viewing an entity, I don't think it disables views row-level caching. Maybe it should, you should consider opening a core issue for that if you need that, otherwise your rows will still return cached output even if the entity is changed.

3
  • 1
    To confirm this: all values listed as type "property" in the API doc can be set through annotation?
    – sleidig
    Commented Aug 5, 2015 at 7:38
  • Yes, exactly...
    – Berdir
    Commented Aug 5, 2015 at 7:40
  • Seems that the persistent_cache = FALSE property is not working on recent Drupal, so I filled the issue drupal.org/project/drupal/issues/3211444
    – Murz
    Commented Apr 30, 2021 at 5:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.