Laravel に限った話でもないけど、 Lravel 5.4 で https://github.com/Zizaco/entrust/ の 5.2.x-dev を使っている時、 Query Log が大量だったら config/cache.php に ttl を設定した方が良いかも知れない。(というか、多分設定すべき。)
Zizaco/entrust
Zizaco/entrust を使うと、以下のように Permission を有するユーザーかどうかなど、確認できて便利。
if (Auth::user()->can('some-permission')) {
// the user has some permission
} else {
// the user has not some permission
}
何が問題か
前提: 現時点(2017-08-16) の Zizaco/entrust 5.2.x-dev で確認している
例えば、上記のような if 文を一覧表示中で使ったりすると、当然だが QueryLog が大量に出る。
foreach ($items as $item) {
echo '<p>'.Auth::user()->name.' can '.(Auth::user()->can('some-entity-read')).' access to '.$item->name.'</p>';
}
例えば、 count($items);// 500
だったりすると、500回以下のような sql が発行される。
select `roles`.*, `role_user`.`user_id` as `pivot_user_id`, `role_user`.`role_id` as `pivot_role_id` from `roles` inner join `role_user` on `roles`.`id` = `role_user`.`role_id` where `role_user`.`user_id` = '1'
大変。
どうするべきか
以下を付け加える。(60 の部分は任意値)
config/cache.php
// snip
'ttl' => 60,
// snip
ここを見ると、以下のようになってる。
return Cache::tags(Config::get('entrust.permission_role_table'))->remember($cacheKey, Config::get('cache.ttl'), function () {
return $this->perms()->get();
});
なお
https://github.com/Zizaco/entrust/commit/2a73bc28a096c51a8351b8d4b3954327d56db61b これで修正されるっぽい。