diff --git a/app/Services/DesaService.php b/app/Services/DesaService.php index f1182445e..f05aba2aa 100644 --- a/app/Services/DesaService.php +++ b/app/Services/DesaService.php @@ -55,15 +55,19 @@ public function dataDesa(string $slug) public function listDesa($all = false) { if ($this->useDatabaseGabungan()) { + $cacheKey = $this->listDesaCacheKey((bool) $all); + // gunakan cache untuk mempercepat load data melalui api - $dataDesa = Cache::get('listDesa'); - // jika kosong, maka request ulang ke API - if (! $dataDesa || $dataDesa->isEmpty()) { - $dataDesa = $this->desa(['filter[kode_kec]' => $this->kodeKecamatan, 'all' => $all]); + $dataDesa = Cache::get($cacheKey); + if ($dataDesa && $dataDesa->isNotEmpty()) { + return $dataDesa; } + + $dataDesa = $this->desa(['filter[kode_kec]' => $this->kodeKecamatan, 'all' => $all]); + + // hasil kosong tidak di-cache agar kegagalan API tidak tertahan selama 24 jam if ($dataDesa->isNotEmpty()) { - // simpan ke cache selama 24 jam - Cache::put('listDesa', $dataDesa, 60 * 60 * 24); + Cache::put($cacheKey, $dataDesa, 60 * 60 * 24); } return $dataDesa; @@ -72,6 +76,11 @@ public function listDesa($all = false) return DataDesa::all(); } + public function listDesaCacheKey(bool $all = false): string + { + return 'listDesa_' . $this->kodeKecamatan . '_' . ($all ? 'all' : 'default'); + } + /** * Get Unique Desa */ diff --git a/catatan_rilis.md b/catatan_rilis.md index db2c4cde7..5ab68a903 100644 --- a/catatan_rilis.md +++ b/catatan_rilis.md @@ -21,6 +21,7 @@ Terimakasih [isi disini] yang telah berkontribusi langsung mengembangkan aplikas 10. [#1795](https://github.com/OpenSID/OpenDK/issues/1795) Fix: Impor Toilet & Sanitasi berhasil tetapi redirect error 500 (nama route salah). 11. [#1796](https://github.com/OpenSID/OpenDK/issues/1796) Menghapus Jabatan Camat/Sekretaris membuat seluruh aplikasi error 500. 12. [#1797](https://github.com/OpenSID/OpenDK/issues/1797) Persentase belanja barang/jasa salah dan division by zero di AnggaranRealisasiChartService. +13. [#1798](https://github.com/OpenSID/OpenDK/issues/1798) Perbaikan Cache listDesa terus diperpanjang dan tidak membedakan parameter $all. #### TEKNIS diff --git a/tests/Feature/ExportDatabaseGabunganTest.php b/tests/Feature/ExportDatabaseGabunganTest.php index 4ccd540ae..008136688 100644 --- a/tests/Feature/ExportDatabaseGabunganTest.php +++ b/tests/Feature/ExportDatabaseGabunganTest.php @@ -387,7 +387,7 @@ $cachedData = collect([ (object) ['desa_id' => '111', 'nama' => 'Cached Desa'] ]); - Cache::put('listDesa', $cachedData, 60 * 60 * 24); + Cache::put((new DesaService())->listDesaCacheKey(), $cachedData, 60 * 60 * 24); // Mock API (tidak seharusnya dipanggil karena ada cache) Http::fake([ diff --git a/tests/Unit/Services/DesaServiceTest.php b/tests/Unit/Services/DesaServiceTest.php index 487d122d0..50b78ef40 100644 --- a/tests/Unit/Services/DesaServiceTest.php +++ b/tests/Unit/Services/DesaServiceTest.php @@ -187,7 +187,7 @@ config(['api_key_database_gabungan' => 'test-key']); // Clear cache first - Cache::forget('listDesa'); + Cache::forget((new DesaService())->listDesaCacheKey()); // Mock API response $apiResponse = [ @@ -241,7 +241,7 @@ ] ]); - Cache::put('listDesa', $cachedData, 60 * 60 * 24); + Cache::put((new DesaService())->listDesaCacheKey(), $cachedData, 60 * 60 * 24); $service = new DesaService(); $desaList = $service->listDesa(); @@ -411,7 +411,7 @@ config(['api_key_database_gabungan' => 'test-key']); // Clear cache first - Cache::forget('listDesa'); + Cache::forget((new DesaService())->listDesaCacheKey()); // Mock API response $apiResponse = [ @@ -512,4 +512,81 @@ return str_contains($request->url(), 'api/v1/wilayah/desa') && str_contains(urldecode($request->url()), 'filter[nama_desa]=desa sukamaju'); }); -}); \ No newline at end of file +}); +describe('cache listDesa database gabungan', function () { + beforeEach(function () { + config([ + 'sinkronisasi_database_gabungan' => '1', + 'api_server_database_gabungan' => 'https://api.example.com', + 'api_key_database_gabungan' => 'test-key', + ]); + + $service = new DesaService(); + Cache::forget($service->listDesaCacheKey(false)); + Cache::forget($service->listDesaCacheKey(true)); + }); + + $responseDesa = fn (string $kode, string $nama) => Http::response([ + 'data' => [ + ['attributes' => ['kode_desa' => $kode, 'nama_desa' => $nama]], + ], + ], 200); + + it('tidak memperpanjang ttl cache saat cache hit', function () use ($responseDesa) { + Http::fake(['https://api.example.com/api/v1/wilayah/desa*' => $responseDesa('001', 'Desa A')]); + + $service = new DesaService(); + + $service->listDesa(); + Http::assertSentCount(1); + + $this->travel(23)->hours(); + $service->listDesa(); + Http::assertSentCount(1); + + // Total 25 jam sejak data diambil: cache harus sudah kedaluwarsa + $this->travel(2)->hours(); + $service->listDesa(); + Http::assertSentCount(2); + }); + + it('memisahkan cache berdasarkan parameter all', function () use ($responseDesa) { + Http::fake(function ($request) use ($responseDesa) { + return str_contains($request->url(), 'all=1') + ? $responseDesa('002', 'Desa Semua') + : $responseDesa('001', 'Desa Default'); + }); + + $service = new DesaService(); + + $default = $service->listDesa(false); + $semua = $service->listDesa(true); + + expect($default->first()->nama)->toBe('Desa Default') + ->and($semua->first()->nama)->toBe('Desa Semua'); + Http::assertSentCount(2); + + // Pemanggilan ulang memakai cache masing-masing + expect($service->listDesa(false)->first()->nama)->toBe('Desa Default') + ->and($service->listDesa(true)->first()->nama)->toBe('Desa Semua'); + Http::assertSentCount(2); + }); + + it('tidak menyimpan hasil kosong ke cache', function () { + Http::fake(['https://api.example.com/api/v1/wilayah/desa*' => Http::response(['data' => []], 200)]); + + $service = new DesaService(); + + expect($service->listDesa())->toBeEmpty() + ->and(Cache::has($service->listDesaCacheKey(false)))->toBeFalse(); + }); + + it('menyertakan kode kecamatan dan parameter all pada kunci cache', function () { + config(['profil.kecamatan_id' => '53.06.01']); + + $service = new DesaService(); + + expect($service->listDesaCacheKey(false))->toBe('listDesa_530601_default') + ->and($service->listDesaCacheKey(true))->toBe('listDesa_530601_all'); + }); +});