Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions app/Services/DesaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down
1 change: 1 addition & 0 deletions catatan_rilis.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/ExportDatabaseGabunganTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
85 changes: 81 additions & 4 deletions tests/Unit/Services/DesaServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -512,4 +512,81 @@
return str_contains($request->url(), 'api/v1/wilayah/desa')
&& str_contains(urldecode($request->url()), 'filter[nama_desa]=desa sukamaju');
});
});
});
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');
});
});
Loading