Problem/Motivation
As an example, you have the node entity type with three bundles: article, blog, and page.
JSON API provides three API resources for those: jsonapi/node/article, jsonapi/node/blog and jsonapi/node/page.
The entity list cache tag for node, node_list, will automatically be added to each of those responses but I think it's wasteful. Any operation on an article node will clear caches for blog and page. There's no need for that.
Proposed resolution
Stop adding the entity type's list cache tag to responses. Rather, use the tag that takes the bundle in to account, ie, {entity_type_id}_list:{bundle}.
Remaining tasks
- Decide if this is a good idea and worth doing.
- Evaluation any potential downsides such as other list cache tags not making it in the the response that may be required.
- Write a patch.
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | drupal-3090131-16.patch | 1.41 KB | leon kessler |
| #7 | 3090131-1.patch | 1.38 KB | Anonymous (not verified) |
Comments
Comment #2
gabesulliceThis is a good idea @mstef, the blocker is #2145751: Introduce ENTITY_TYPE_list:BUNDLE cache tag and add it to single bundle listing (see esp. comment #70).
Comment #3
gabesulliceWOOT! #2145751: Introduce ENTITY_TYPE_list:BUNDLE cache tag and add it to single bundle listing finally landed!
Comment #4
wim leersComment #5
Anonymous (not verified) commentedComment #6
wim leersGreat! Looking forward to your patch, @wouter.adem, and thanks for stepping up! 🥳🙏
Comment #7
Anonymous (not verified) commentedHere's a patch with the proposed fix. Needs automated tests.
Comment #9
arla commentedWith the patch in #7, the
node_listtag is indeed removed from/jsonapi/node/<bundle>. However, it still appears in/jsonapi/node/<bundle>?filter[status]=1. Not sure why or what other parameters cause this.Comment #12
edischLogistically how do we prevent stale relationship data with this? I think that would be especially relevant to ?include queries where potentially data from other bundles is included on the parent bundle response.
I'd love to see this implemented but I'm having trouble wrapping my head around how to properly reconcile the cache. Hardest problems in comp-sci I guess...
Comment #13
bbralaThe cache is already varied by
?include, if references are changes shouldn't the parent caches also been cleared normally? Therefor there isn't much issue here?I think if we have some proper tests this could be verified.
Comment #14
berdirThis issue isn't about being correct, it's about having fewer invalidations while still being correct. Invalidating the overall list cache tag is the easiest option, but it is possibly also to aggressive. Imagine you have a news site, then a news is probably going to be updated every few minutes and node_list is invalidated very frequently. But you might also have dossier/topics or something, and those only change once every few days. So a jsonapi query to list all dossiers does _not_ need to be invalidated every time a news is updated. You know it will only ever list dossiers, so you can just add the node_list:dossier cache tag.
I don't know the jsonapi code very well, but I imagine that this might be much easier to implement than the sister issue for the views module.
Comment #15
gabesulliceI think this is a non-issue. Relationship resources don't use the
*_listcache tag since new entities are not automatically added to relationship routes—they must be referenced by the parent entity in order to appear. For that reason, relationship routes use the referencing entity's cache tag. For example, if article:42 references related articles and a new article gets created, it will not automatically appear in article:42's list of related articles. However, when someone does explicitly add it, the related route will be invalidated when thenode:42cache tag is invalidated.The same applies here since collections with include queries have the cache tags from each included entity.
tl;dr: relationship routes and the
includequery param are irrelevant because theENTITY_TYPE_listcache tags only pertain to "automatic" listings, not lists of referenced content.Comment #18
leon kessler commentedJust tried out this patch and needed to make a small change. As we are using JSON:API Cross Bundles, and I noticed on these routes you would get a cachtag of
node_list:(i.e. a colon was added but no bundle).This new patch adds a check to see whether a valid bundle exists.
I also couldn't replicate the issue from #9, when I added filters to the query when using the patch, I still got the
node_list:bundle_nametag.Comment #19
bbralaGreat you had a look, could you please add an interdiff? That always helps understanding the changes a bit better.
See this page in the documentation
Comment #20
leon kessler commentedYes sorry, I tried to create an interdiff but got this whitespace error that I don't have time to look into right now.
interdiff: Whitespace damage detected in inputThe difference is:
Was changed to
Comment #21
leon kessler commentedIf anyone is interested, I created a custom module for our project that generates cache tags based on JSON:API filter values. (Using bundle specific list cache tags did not go far enough for our requirements).
https://github.com/ministryofjustice/prisoner-content-hub-backend/tree/m...
Comment #22
wim leers@Leon Kessler: do you mind publishing this on drupal.org as a module? 🤞😊
Comment #23
leon kessler commentedI was planning to. But it's a bit bare bones at the moment, only currently supports entity reference fields being filtered by uuid. But I could release as a module in development.
Comment #25
leon kessler commentedI've now posted this as a full project: https://www.drupal.org/project/jsonapi_filter_cache_tags
Note that tests are currently failing in D10 (they pass on D9), haven't had time to look into it yet.
Also, it only currently supports filtering on entity reference fields, no other field types.
The module was very much created for our own needs, and probably needs a bit of work to get to a stable release.
Comment #27
ghost of drupal pastIsn't this a duplicate now? https://www.drupal.org/node/3107058Nevermind, the issue title/summary misled me.Comment #28
ghost of drupal past