Zizaco / entrust

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

Duplicate query Roles #780

Open yurikzt opened 7 years ago

yurikzt commented 7 years ago

Fix, please duplicate query Roles in EntrustUserTrait

protected $roles;

//Big block of caching functionality.
    public function cachedRoles() {
        if ($this->roles instanceof Collection)
            return $this->roles;
        $userPrimaryKey = $this->primaryKey;
        $cacheKey = 'entrust_roles_for_user_' . $this->$userPrimaryKey;
        if (Cache::getStore() instanceof TaggableStore) {
            return $this->roles = Cache::tags(Config::get('entrust.role_user_table'))->remember($cacheKey, Config::get('cache.ttl'), function () {
                return $this->roles()->get();
            });
        } else
            return $this->roles = $this->roles()->get();
    }
rudashi commented 7 years ago

Add to file EntrustUserTrait and EntrustRoleTrait

use Illuminate\Database\Eloquent\Collection;

and for EntrustRoleTrait

protected $perms;

//Big block of caching functionality.
public function cachedPermissions()
{
    if ($this->perms instanceof Collection) {
        return $this->perms;
    }
    $rolePrimaryKey = $this->primaryKey;
    $cacheKey = 'entrust_permissions_for_role_' . $this->$rolePrimaryKey;
    if (Cache::getStore() instanceof TaggableStore) {
    //    return Cache::store('array')->tags(Config::get('entrust.permission_role_table'))->remember($cacheKey, Config::get('entrust.cache.ttl', 60), function () {
        return Cache::tags(Config::get('entrust.permission_role_table'))->remember($cacheKey, Config::get('entrust.cache_ttl', 60), function () {
            return $this->perms()->get();
        });
    } else {
        return $this->perms = $this->perms()->get();
    }
}
tatarysh commented 5 years ago

set: CACHE_DRIVER=array in .env file

and add 'ttl' in confg/cache.php

return [
/***/
    'ttl' => 60,
/***/
];
tatarysh commented 3 years ago

@karnasoneji wrong place.

image

Don't forget to clear cache config.

karnasoneji commented 3 years ago

thanks @tatarysh 👍