Browse Source

feat: :sparkles: cache repo city, state, country

Denis 10 months ago
parent
commit
9980582e0a

+ 1 - 1
app/Http/Resources/CityResource.php

@@ -17,7 +17,7 @@ public function toArray(Request $request): array
             'state_id' => $this->state_id,
             'country_id' => $this->country_id,
             'state' => $this->state,
-            'country' => new CountryResource($this->country),
+            'country' => $this->country,
             'status' => $this->status,
             'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
             'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),

+ 1 - 1
app/Http/Resources/StateResource.php

@@ -16,7 +16,7 @@ public function toArray(Request $request): array
             'name' => $this->name,
             'code' => $this->code,
             'country_id' => $this->country_id,
-            'country' => new CountryResource($this->country),
+            'country' => $this->country,
             'status' => $this->status,
             'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
             'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),

+ 11 - 4
app/Repositories/CityRepository.php

@@ -5,25 +5,31 @@
 use App\Models\City;
 use App\DTO\CityDTO;
 use Illuminate\Database\Eloquent\Collection;
+use Illuminate\Support\Facades\Cache;
 
 class CityRepository implements CityRepositoryInterface
 {
     public function __construct(
-        protected City $model
+        protected City $model,
+        readonly protected string $cacheKey = 'cities.all',
+        readonly protected int $cacheTtl = 2592000 // one month in seconds
     ) {}
 
     public function all(): Collection
     {
-        return $this->model->with('state', 'country')->get();
+        return Cache::remember($this->cacheKey, $this->cacheTtl, function () {
+            return $this->model->with('state:id,name', 'country:id,name')->get();
+        });
     }
 
     public function find(int $id): ?City
     {
-        return $this->model->with('state', 'country')->find($id);
+        return $this->model->with('state:id,name', 'country:id,name')->find($id);
     }
 
     public function create(CityDTO $dto): City
     {
+        Cache::forget($this->cacheKey);
         return $this->model->create($dto->toArray());
     }
 
@@ -37,12 +43,13 @@ public function update(int $id, CityDTO $dto, array $fieldsToUpdate): City
         );
 
         $record->update($updateFields);
-
+        Cache::forget($this->cacheKey);
         return $record->fresh();
     }
 
     public function delete(int $id): bool
     {
+        Cache::forget($this->cacheKey);
         return $this->model->destroy($id) > 0;
     }
 }

+ 11 - 5
app/Repositories/CountryRepository.php

@@ -5,17 +5,21 @@
 use App\Models\Country;
 use App\DTO\CountryDTO;
 use Illuminate\Database\Eloquent\Collection;
+use Illuminate\Support\Facades\Cache;
 
 class CountryRepository implements CountryRepositoryInterface
 {
     public function __construct(
-        protected Country $model
-    ){
-    }
+        protected Country $model,
+        readonly protected string $cacheKey = 'country.all',
+        readonly protected int $cacheTtl = 2592000 // one month in seconds
+    ) {}
 
     public function all(): Collection
     {
-        return $this->model->all();
+        return Cache::remember($this->cacheKey, $this->cacheTtl, function () {
+            return $this->model->all();
+        });
     }
 
     public function find(int $id): ?Country
@@ -25,6 +29,7 @@ public function find(int $id): ?Country
 
     public function create(CountryDTO $dto): Country
     {
+        Cache::forget($this->cacheKey);
         return $this->model->create($dto->toArray());
     }
 
@@ -38,12 +43,13 @@ public function update(int $id, CountryDTO $dto, array $fieldsToUpdate): Country
         );
 
         $record->update($updateFields);
-
+        Cache::forget($this->cacheKey);
         return $record->fresh();
     }
 
     public function delete(int $id): bool
     {
+        Cache::forget($this->cacheKey);
         return $this->model->destroy($id) > 0;
     }
 }

+ 12 - 6
app/Repositories/StateRepository.php

@@ -5,26 +5,31 @@
 use App\Models\State;
 use App\DTO\StateDTO;
 use Illuminate\Database\Eloquent\Collection;
+use Illuminate\Support\Facades\Cache;
 
 class StateRepository implements StateRepositoryInterface
 {
     public function __construct(
-        protected State $model
-    ){
-    }
+        protected State $model,
+        readonly protected string $cacheKey = 'states.all',
+        readonly protected int $cacheTtl = 2592000
+    ) {}
 
     public function all(): Collection
     {
-        return $this->model->with('country')->get();
+        return Cache::remember($this->cacheKey, $this->cacheTtl, function () {
+            return $this->model->with('country:id,name')->get();
+        });
     }
 
     public function find(int $id): ?State
     {
-        return $this->model->with('country')->find($id);
+        return $this->model->with('country:id,name')->find($id);
     }
 
     public function create(StateDTO $dto): State
     {
+        Cache::forget($this->cacheKey);
         return $this->model->create($dto->toArray());
     }
 
@@ -38,12 +43,13 @@ public function update(int $id, StateDTO $dto, array $fieldsToUpdate): State
         );
 
         $record->update($updateFields);
-
+        Cache::forget($this->cacheKey);
         return $record->fresh();
     }
 
     public function delete(int $id): bool
     {
+        Cache::forget($this->cacheKey);
         return $this->model->destroy($id) > 0;
     }
 }