entrust icon indicating copy to clipboard operation
entrust copied to clipboard

Entrust permission caching

Open titleistfour opened this issue 10 years ago • 24 comments

Hi,

On sites with a large number of permissions and roles, and pages where you need to check those to display content, it seems that this incurs quite a few database calls.

Does Entrust have any support for caching to reduce the calls back to the database?

Thanks, Jay

titleistfour avatar Mar 05 '14 18:03 titleistfour

I was wondering the same. Caching the permission calls would certainly help reduce queries that arent needed all the time.

gcphost avatar May 12 '14 23:05 gcphost

+1

mcka avatar Jun 11 '14 18:06 mcka

Good idea. I'm gonna do this

Zizaco avatar Jul 18 '14 05:07 Zizaco

Hey @Zizaco! What is the status on this?

rasmusdencker avatar Oct 08 '14 08:10 rasmusdencker

+1 Would be glad if the queries were optimised/cached. My app makes quite a few ajax calls and I can see Entrust firing quite a lot of similar queries for each of them. Considering roles don't change all that often, caching them would be ideal. May be those caches can be invalidated when new roles are added/updated.

mohamedazher avatar Feb 12 '15 14:02 mohamedazher

+1 I'm looking for the cache too. Is there any similar solution available now? This would be quite useful. Thanks

censhen avatar Mar 23 '15 07:03 censhen

@censhen I'm working on a package. I am currently using it in several of my own projects as I don't know the exact functional pitfalls yet. You can add me on Skype if you're interested in testing or contributing; rasmusdencker

rasmusdencker avatar Mar 23 '15 07:03 rasmusdencker

@dondencker Thanks for your info. Your package sounds interesting. But currently I'm working on another project. I may contact you after that project.

censhen avatar Mar 31 '15 08:03 censhen

The best thing you can do is eager loading the roles attribute, and overwriting the hasRole() method :

protected $with = ['roles'];
..
use HasRole {
  hasRole as traitHasRole;
}
..
public function hasRole($name) {
    $roles = $this->roles;
    if (!$roles) {
      $roles = $this->roles()->get();
    }
    ..

screenager avatar Jul 30 '15 14:07 screenager

The structure of the queries in Traits right now are not conducive to caching. They're currently using Lazy Loading. Eager Loading is much easier to cache. Optimizing these would require a decent bit of restructuring.

I'm going to take a stab at it, but don't hold your breath.

scottconnerly avatar Nov 19 '15 21:11 scottconnerly

Please do!

On Thu, Nov 19, 2015 at 1:34 PM Scott Connerly [email protected] wrote:

The structure of the queries in Traits right now are not conducive to caching. They're currently using Lazy Loading. Eager Loading is much easier to cache. Optimizing these would require a decent bit of restructuring.

I'm going to take a stab at it, but don't hold your breath.

— Reply to this email directly or view it on GitHub https://github.com/Zizaco/entrust/issues/107#issuecomment-158204726.

andrewelkins avatar Nov 19 '15 22:11 andrewelkins

Hi All !

This evolution is it being developed?

Thank's a lot this is a good plugin. With cache, it's will be perfect !

ejeanneaubc avatar Apr 29 '16 14:04 ejeanneaubc

What is situation here?

freel132 avatar Aug 22 '16 06:08 freel132

+1

racibaz avatar Sep 20 '16 11:09 racibaz

+1

zein-adi avatar Nov 26 '16 01:11 zein-adi

+1

Sentences avatar Dec 05 '16 12:12 Sentences

+1

harrisonbennett avatar Dec 09 '16 13:12 harrisonbennett

+1

elnurxf avatar Feb 10 '17 10:02 elnurxf

+1

andcl avatar Mar 14 '17 11:03 andcl

+1

Sweeeeep avatar Mar 18 '17 22:03 Sweeeeep

Actually guys, its already been implemented. You can use it by adding key to config/cache.php, add folowing:

'ttl' => 30 //In minutes

Please note that, you'll need cache driver that support tagging like redis or i believe memcahe as well.

I don't know if it's official yet, but i'm using it and it works pretty well IMO. Cut down my site loading from 1s to 500ms on Windows.

zein-adi avatar Mar 19 '17 10:03 zein-adi

Hi, is it possible that the caching doesn't refresh when a new role/permission is attached to/removed from a user/role?

I'm using $user->attachRole($role); but it takes 'cache.ttl' minutes to show up in the GUI.

PKeidel avatar Apr 13 '17 10:04 PKeidel

@PKeidel , i think so. I have a similar issue with $role->perms()->toggle($perms); I tried to fix it by using :

Cache::tags(Config::get('entrust.permission_role_table'))->flush();

But it doesn't work. Any workaround or similar issues ?

LeNiglo avatar Jun 23 '17 18:06 LeNiglo

I have 'ttl' => 30 //In minutes in config/cache.php and caching works perfectly. However when new user is created, in my own case via registration form and a certain role is assigned automatically, role doesn't take effect until I manually run php artisan cache:clear

How do I refresh cached user roles

digitlimit avatar Jan 07 '19 15:01 digitlimit