laravel-permission: Cache not working

Have installed version 3.18 with Laravel 8 and can’t seem to get the cache working. Through debugbar I can see that every page makes several queries related to this permissions/roles.

Have used file, redis and database drivers. Haven’t changed anything in the config/permission.php file. Using the provided API/methods to create/edit/sync roles & permissions, so not doing it directly through the database.

When I test for cache using Cache::get('spatie.permission.cache'), it returns null.

Can confirm that my cache/driver is working by doing Cache::put('test_name', 'test_value') I can get the test_value.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 6
  • Comments: 18 (3 by maintainers)

Most upvoted comments

Hi guys,

@damchey, @anwardote, @a-ssassi-n, @xperator, @arislanhaikal, @yeaggercom

cache won’t get created - If you create Permissions or Roles also cache won’t get created when you assign permissions or role to user

but cache will get created when you actually read

For example:

dump(Cache::get('spatie.permission.cache')); // returns null

dump($user->hasPermissionTo('edit-articles')); // creates cache and returns true/false

dd(Cache::get('spatie.permission.cache')); // returns cache

the package works exactly as it should be 👍🏻

@ajuchacko91 I don’t think we’re supposed to directly read from cache. Are we? We’re calling functions and methods provided by this library, and I believe it should be implemented inside each one of these functions to try to read from cache whenever possible.

Same issues here. In the docs it says there is no need to do any extra steps to enable caching as it is enabled by default. By I don’t see any caching in action and I see lots of queries made to DB for the permission/role stuff

Using

software version
laravel-permission 4.0.1
laravel/framework 8.41
predis/predis 1.1.7

It seems to be solved as the cache is filled.

The only three queries that get run every page load for me (after the cache is warm) are: select column_name as `column_name` from information_schema.columns where table_schema = 'redacted' and table_name = 'roles' and select column_name as `column_name` from information_schema.columns where table_schema = 'redacted' and table_name = 'permissions' which are run when the Role and Permission models run the parent::__construct($attributes) call.

And then: select `roles`.*, `model_has_roles`.`model_id` as `pivot_model_id`, `model_has_roles`.`role_id` as `pivot_role_id`, `model_has_roles`.`model_type` as `pivot_model_type` from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` where `model_has_roles`.`model_id` = 1 and `model_has_roles`.`model_type` = 'App\Models\System\User' is run every time to get the user’s roles from here: src/Traits/HasRoles.php:180

I think this is all expected behavior.

@damchey have you updated/can you update? If so, can you confirm it’s now working for you?