From 3a5aeefebf2b021db1488067f91787810e5bcb20 Mon Sep 17 00:00:00 2001 From: Habibie Date: Thu, 19 Nov 2026 11:41:46 +0700 Subject: [PATCH 1/2] Fix: Cache listDesa terus diperpanjang dan tidak membedakan parameter $all - Tulis cache hanya setelah data diambil dari API agar TTL 24 jam tidak diperpanjang pada setiap cache hit - Kunci cache menyertakan kode kecamatan dan parameter $all (listDesa_{kodeKecamatan}_{all|default}) - Hasil kosong tetap tidak di-cache - Tambah test regresi TTL, pemisahan $all, dan hasil kosong Closes #1798 Co-Authored-By: Claude Opus 5.5 --- app/Services/DesaService.php | 21 +++-- tests/Feature/ExportDatabaseGabunganTest.php | 2 +- tests/Unit/Services/DesaServiceTest.php | 85 +++++++++++++++++++- 3 files changed, 97 insertions(+), 11 deletions(-) diff --git a/app/Services/DesaService.php b/app/Services/DesaService.php index f1182445e0..f05aba2aaf 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/tests/Feature/ExportDatabaseGabunganTest.php b/tests/Feature/ExportDatabaseGabunganTest.php index 4ccd540aed..0081366886 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 487d122d0a..50b78ef409 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'); + }); +}); From 76f54c7f1b1448160dd5b75dc7746fc052f204a0 Mon Sep 17 00:00:00 2001 From: Abah Roland Date: Thu, 26 Nov 2026 03:16:04 +0700 Subject: [PATCH 2/2] update catatan rilis --- catatan_rilis.md | 1 + 1 file changed, 1 insertion(+) diff --git a/catatan_rilis.md b/catatan_rilis.md index db2c4cde7f..5ab68a9036 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