Local implementation of caching user permissions #2175
Unanswered
BrekiTomasson
asked this question in
Q&A
Replies: 1 comment 4 replies
-
Only time can answer that I also tried something similir without problems |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've got a project that relies quite heavily on permissions when it comes to routing, which elements to show in navigation, which options to return in
<select>
boxes on forms, etc, etc. Needless to say, any user navigating around the website will be creating dozens ofPermission
-related calls every minute, with Debug Bar regularly showing me things like this for a single page's load:Now; I know that part of the problem is that I'm doing permission checks in multiple places - some in Controllers, some in Middleware, some in Blade views using
@can/@endcan
or my own@admin/@endadmin
, but I still feel like there's some optimization that could be done here. I've read many of the conversations on the topic that have been held in the past, so I understand the problems inherent in this kind of thing being offered by the package itself, so I've been playing around with an implementation of my own that plays nicely with the setup that I've got, a Redis-based cache relying heavily on tags.Here's a method that I built on my
User
model:As you can see, I'm just returning the base value for iterables rather than attempting to cache them, mainly because calling
can()
on iterables are fairly rare in my implementation. However, when using a string, I'm caching the value for ten minutes, using a genericusers
tag and auser:61
tag (if the user in question hasusers.id = 61
, of course). I've already got Event Listeners thatCache::tags('user:' . $user->id)->flush()
whenever a user logs in and whenever a user's permissions or roles are changed, so I'm not expecting the cache to return stale results here.... That said; am I missing something here? Can I expect this to come back and bite me in the ass in some unexpected way? Happy to hear your thoughts and ideas on this one.
Beta Was this translation helpful? Give feedback.
All reactions