feat: cache queries where possible
This commit is contained in:
parent
4b9adbd74b
commit
9a2ca7e184
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -9,6 +9,8 @@ class AuthorizedRequest extends RestRequest
|
|||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return cache()->remember('isSessionAuthorized'.session()->id(), 5, function(){
|
||||
return auth()->check();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,12 +62,16 @@ class UserService
|
|||
public function listAll(?Filters $filters = null): array
|
||||
{
|
||||
if (is_null($filters)) {
|
||||
return cache()->remember('usersListAll', 15, function() {
|
||||
return User::all()->toArray();
|
||||
});
|
||||
}
|
||||
$builder = User::query();
|
||||
$filters->apply($builder);
|
||||
|
||||
return cache()->remember('usersListFilter'.$filters->hash(), 15, function() use ($builder) {
|
||||
return $builder->get()->toArray();
|
||||
});
|
||||
}
|
||||
|
||||
public function getOneById(string $id): ?User
|
||||
|
|
|
@ -15,7 +15,7 @@ return [
|
|||
|
|
||||
*/
|
||||
|
||||
'default' => env('CACHE_STORE', 'database'),
|
||||
'default' => env('CACHE_STORE', 'redis'),
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
Loading…
Reference in New Issue