Explore if user.roles.anonymous and user.roles.authenticated cache contexts can be combined

Created on 30 November 2023, 6 months ago
Updated 7 December 2023, 6 months ago

Problem/Motivation

Follow up from πŸ› system_page_attachments varies by authenticated user role but does not add said cache context Needs review

@catch

user.roles.anonymous and user.roles.authenticated feel like they are the same cache context, should we try to standardise on one, and if so which?

@smustgrave

Are they the same though? They certainly overlap but don't they have distinct differences. Would it make sense to keep these 2 and add a third that's shared?

Believe this warrants it's own ticket for general discussion.

Steps to reproduce

NA

Proposed resolution

TBD

Remaining tasks

Agree if anything needs to be done

User interface changes

NA

API changes

NA

Data model changes

TBD

Release notes snippet

TBD

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component
CacheΒ  β†’

Last updated 1 day ago

Created by

πŸ‡ΊπŸ‡ΈUnited States smustgrave

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

Comments & Activities

  • Issue created by @smustgrave
  • πŸ‡¬πŸ‡§United Kingdom catch

    If user.roles.anonymous is 1, then user.roles.authenticated is always 0, and vice versa.

    However, if one element adds user.roles.anonymous and another adds user.roles.authenticated, they could lead to additional cache redirects where a single cache entry would have been fine.

    I am not sure the best way to handle this though. Maybe we could deprecate both and move to a user.is_authenticated or similar?

  • πŸ‡§πŸ‡ͺBelgium kristiaanvandeneynde Antwerp, Belgium

    Keep in mind that even if we introduce 'user.is_authenticated', which I support, we can't stop people from using user.roles:anonymous or user.roles:authenticated as the user/roles cache context supports any role ID as its derivative value. We could, however, automatically convert them to user.is_authenticated, perhaps in CacheContextsManager?

    Pros would be that old code keeps working, people don't have to know about this special case for these 2 user roles and we can gradually move all of core's usage to user.is_authenticated.

    Just trying to avoid the obvious foot-gun by introducing a new solution and then leaving old solutions in, meaning we now increased the problem from 2 to 3 possible cache contexts saying the same thing, leading to even more unnecessary redirects.

  • πŸ‡©πŸ‡ͺGermany Feuerwagen Bonn πŸ‡©πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Restoring title. I don't think the change was intentional.

  • πŸ‡§πŸ‡ͺBelgium kristiaanvandeneynde Antwerp, Belgium

    Another thing to keep in mind is that if we have 'user.roles' and 'user.roles:authenticated' they end up folded as 'user.roles'. If we were to add 'user.is_authenticated' to that, we'd actually make performance worse rather than improve it. So if we end up filtering out user.roles:authenticated and user.roles:anonymous, we should only do so after we have folded the contexts.

  • πŸ‡¬πŸ‡§United Kingdom catch

    Could we add user.roles.is_authenticated to the roles cache context - it would not be very semantic but it would enable all of the folding to happen in one place that way.

  • πŸ‡§πŸ‡ͺBelgium kristiaanvandeneynde Antwerp, Belgium

    To be fair, while it does look semantically awkward, it would be technically correct, which is the best kind of correct. Whether or not you are authenticated is always translated into a specific user role in Drupal, so folding that info into user.roles when present makes sense.

    The only thing we might still want to do is optimize user.roles:authenticated and user.roles:anonymous away into user.roles.is_authenticated. We could do that in CacheContextsManager but part of me dislikes that we would put specific code into an otherwise abstract class.

  • πŸ‡¬πŸ‡§United Kingdom catch

    Ahh I see we can't do the folding in the UserRolesCacheContext ourselves because it just doesn't work like that...

    We could trigger a deprecation if anonymous or authenticated is passed in, and encourage is_authenticated instead. And maybe do the optimization in CacheContextsManager as a temporary bc layer. Then in a future release, throw an exception if anon/auth is passed on telling people to use is_authenticated.

  • πŸ‡§πŸ‡ͺBelgium kristiaanvandeneynde Antwerp, Belgium

    We could trigger a deprecation if anonymous or authenticated is passed in, and encourage is_authenticated instead. And maybe do the optimization in CacheContextsManager as a temporary bc layer. Then in a future release, throw an exception if anon/auth is passed on telling people to use is_authenticated.

    Sounds like a solid plan.

  • πŸ‡§πŸ‡ͺBelgium Wim Leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    #8++

  • πŸ‡§πŸ‡ͺBelgium kristiaanvandeneynde Antwerp, Belgium

    I'm up for tackling this one once my schedule clears up a bit. Currently still focusing on Access Policy API

Production build 0.69.0 2024