feat: cache queries where possible

This commit is contained in:
b1ek 2024-08-30 18:18:50 +10:00
parent 4b9adbd74b
commit 9a2ca7e184
Signed by: blek
GPG Key ID: 14546221E3595D0C
5 changed files with 16 additions and 5 deletions

View File

@ -36,7 +36,7 @@ BROADCAST_CONNECTION=log
FILESYSTEM_DISK=local
QUEUE_CONNECTION=database
CACHE_STORE=database
CACHE_STORE=redis
CACHE_PREFIX=
MEMCACHED_HOST=127.0.0.1

View File

@ -35,6 +35,11 @@ class Filters
}
}
public function hash(): string
{
return hash('sha256', json_encode($this));
}
public function apply(Builder $builder)
{
foreach ($this->filters as $filter) {

View File

@ -9,6 +9,8 @@ class AuthorizedRequest extends RestRequest
*/
public function authorize(): bool
{
return auth()->check();
return cache()->remember('isSessionAuthorized'.session()->id(), 5, function(){
return auth()->check();
});
}
}

View File

@ -62,12 +62,16 @@ class UserService
public function listAll(?Filters $filters = null): array
{
if (is_null($filters)) {
return User::all()->toArray();
return cache()->remember('usersListAll', 15, function() {
return User::all()->toArray();
});
}
$builder = User::query();
$filters->apply($builder);
return $builder->get()->toArray();
return cache()->remember('usersListFilter'.$filters->hash(), 15, function() use ($builder) {
return $builder->get()->toArray();
});
}
public function getOneById(string $id): ?User

View File

@ -15,7 +15,7 @@ return [
|
*/
'default' => env('CACHE_STORE', 'database'),
'default' => env('CACHE_STORE', 'redis'),
/*
|--------------------------------------------------------------------------