Zizaco / entrust

Role-based Permissions for Laravel 5
MIT License
6.05k stars 1.29k forks source link

Entrust permission caching #107

Open titleistfour opened 10 years ago

titleistfour commented 10 years ago

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

gcphost commented 10 years ago

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

mcka commented 10 years ago

+1

Zizaco commented 10 years ago

Good idea. I'm gonna do this

rasmusdencker commented 10 years ago

Hey @Zizaco! What is the status on this?

mohamedazher commented 9 years ago

+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.

censhen commented 9 years ago

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

rasmusdencker commented 9 years ago

@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

censhen commented 9 years ago

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

screenager commented 9 years ago

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();
    }
    ..
scottconnerly commented 8 years ago

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.

andrewelkins commented 8 years ago

Please do!

On Thu, Nov 19, 2015 at 1:34 PM Scott Connerly notifications@github.com 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.

ejeanneaubc commented 8 years ago

Hi All !

This evolution is it being developed?

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

freel132 commented 8 years ago

What is situation here?

racibaz commented 8 years ago

+1

zein-adi commented 7 years ago

+1

Sentences commented 7 years ago

+1

harrisonbennett commented 7 years ago

+1

elnurxf commented 7 years ago

+1

andcl commented 7 years ago

+1

Sweeeeep commented 7 years ago

+1

zein-adi commented 7 years ago

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.

PKeidel commented 7 years ago

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.

LeNiglo commented 7 years ago

@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 ?

digitlimit commented 5 years ago

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