Laravel权限查询缓存

时间:2020-03-11 04:04:39

标签: laravel laravel-permission

我正在使用Laravel权限包(https://github.com/spatie/laravel-permission)来管理Laravel项目上的角色权限。

在应用程序的每个页面上,我都可以从Laravel调试器中看到以下两个查询正在运行。角色和权限没有经常更改,我想缓存这些查询。你能让我知道我该怎么做吗?

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\User'

select `permissions`.*, `model_has_permissions`.`model_id` as `pivot_model_id`, `model_has_permissions`.`permission_id` as `pivot_permission_id`, `model_has_permissions`.`model_type` as `pivot_model_type` from `permissions` inner join `model_has_permissions` on `permissions`.`id` = `model_has_permissions`.`permission_id` where `model_has_permissions`.`model_id` = 1 and `model_has_permissions`.`model_type` = 'App\Models\User'

1 个答案:

答案 0 :(得分:0)

您可以使用this package来缓存查询。

要对特定查询启用缓存,您可以执行以下操作:

DB::cacheFor(now()->addHours(24))->raw('YOUR QUERY GOES HERE');