JosephSilber / bouncer

Laravel Eloquent roles and abilities.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Duplicate cache look ups

siamak2 opened this issue · comments

I'm using redis as cache driver and enabled cross-request caching:
Bouncer::cache();
I'm also using telescope to monitor everything. I noticed every time I use $user->can('something') Bouncer gets 4 keys from cache, two of them are duplicate. but this isn't the only duplicate. if I use $user->can('something') three times Bouncer gets 8 more keys which is exactly same as first four.
Here is the image of telescope showing duplicate cache look ups for using $user->can('something') three times:
image

I'm planning to use a lot of $user->can() and it's not a good thing to have n * 4 look ups on redis
Is there a way to also cache them for current request?

Hmm. That doesn't seem right. Will investigate.

If you want to help, could you create a repository with a minimal reproduction?


I misread before.

This is the way Bouncer is currently designed.

We could probably add another layer of local cache on top the cross-request cache. Will look into it.

Reproduction is easy. the user has role of superadmin and Bouncer::allow('superadmin')->everything();
I run $user->can( 'view-admin-dashboard' ) and the ability view-admin-dashboard exists in bouncer.
And of course I ran Bouncer::refresh() to clear the cache.

I used xdebug to trace the duplicate and found it. it's here:

public function getAbilities(Model $authority, $allowed = true)
{
$key = $this->getCacheKey($authority, 'abilities', $allowed);
if (is_array($abilities = $this->cache->get($key))) {
return $this->deserializeAbilities($abilities);
}

Perhaps caching result of $this->cache->get($key) could reduce duplicates.
By reduce duplicates, I mean bouncer still looks up same key with different ending f and a that is decided based on $allowed parameter and getAbilities() is called twice with different $allowed parameter. but this one is OK.
get cache:tag:silber-bouncer:key is used by laravel's cache tag and it's not bouncer's fault.