High Load Laravel Cache Requests

Laravel’s caching mechanism works fine when executed:

$users = User::remember(10)->get(); 

but when executed:

 $users = User::with('posts','addresses')->remember(10)->get(); 

it does not cache the entire set of requests, in particular the connection request (downloadable download).

Is there a way to cache all requests that run in the above example? Thanks!

+5
source share
2 answers

You can do it inline:

 User::with(['posts' => function ($q) { $q->remember(10); }, 'addresses' => function ($q) { $q->remember(10); }])->remember(10)->get(); 

or in the definition of a relationship:

 public function posts() { return $this->hasMany('Post')->remember(10); } 
+4
source

You cannot cache eagler loading requests this way. You have 2 choices - cache: remember engine:

 $users = Cache::remember('custom_cache_key', 10, function() { return User::with('posts', 'addresses')->get(); }); 

or build a single query using the query builder:

 ...->select(...)->join(...)->where(...)->remember(...) 
+2
source

Source: https://habr.com/ru/post/1203211/


All Articles