Skip to content

Thoughtful Performance

Dot it early

Concurrent requests

Concurrent processes

Run code concurrently

Http timeout

For packages who use Guzzle and not the Http Facade, we can also bind an instance of the Guzzle Client with a specific configuration.

use GuzzleHttp\Client
App::bind(Client:class, fn () => new Client([
'connect_timeout' => 2,
'timeout' => 5,
]))

Dot it later

Dispatch after response

Hit the cache

Use cache for long db queries

Here is how to implement this :

  1. Create a macro
Schedule::macro('warm', function ($classes) {
return $this->call(function () use ($classes) {
foreach($classes as $class) {
App::make($class)->warm();
}
});
});
  1. Create a class responsible to warm each data we want
class TotalSales
{
public function warm()
{
Cache::put(
key: 'sales',
ttl: Interval::hour()->minutes(10),
callback: $this->resolve(),
)
}
public function resolve()
{
return Transaction::sum('amount');
}
public function __invoke()
{
return Cache::remember(
key: 'sales',
ttl: Interval::hour(),
callback: $this->resolve(...),
);
}
}
  1. Use it where you need
public function home(TotalSales $sales)
{
return View::make('home', [
'sales' => $sales(),
])
}

All content written above is coming from the “Thoughtfull Performance” talk given at Laracon EU 2024 by Tim MacDonald.