0

Scenario

I'd like to invalidate a set of caches based on user id upon user login. I'd like to avoid creating new tags, and limit new code as much as possible. Presumably the way to do this is with cache tags.

The specific use case is to cache a user-specific web service response, per user, for the duration of a user session.

Are there any existing cache tags (or something to similar effect) which are invalidated upon successful user login, so that I don't have to implement hook_user_login() to manually invalidate the cache?

  • If you want to invalidate some caches, surely you'll already know what those are? Why would you be creating cache tags? What are you caching? – Clive May 9 at 14:26
  • 2
    This should not be done by invalidating cache tags, instead <The_thing_I_want_to_create> should add the current user as cache context. see drupal.org/docs/8/api/cache-api/cache-contexts – Hudri May 9 at 14:27
  • @Clive I'm caching some custom user data from a 3rd party service. I'm hoping not to have to define several other hooks or event subscribers to explicitly clear those caches. If there are existing instances of cache invalidation that I can use, by way of cache tags, that will save me some work. – aaronbauman May 9 at 14:28
  • 1
    Oh ok. By the looks of it, no, core doesn't invalidate any tags upon login – Clive May 9 at 14:48
  • 1
    No it goes straight into the database (UserStorage::updateLastLoginTimestamp()), and resets the cache by cid – Clive May 9 at 14:53
1

No, there aren't any cache tags invalidated upon login. And even if there were, they wouldn't be tied to your cache. Instead, you should act on hook_user_login(), and expire the cache yourself.

Cache invalidation isn't really something you would piggy back onto something non-relevant. Rather, you set up elements with cache tags/contexts etc, and then if necessary you add your own cache invalidation on relevant events.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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