This project implements a local cache for translations, mapping unique IDs to strings across various world languages.
- Local Translation Cache: Efficiently stores and retrieves localized strings using a combination of ID and
Locale. - Parallel Data Loading: The
refresh()method utilizes a thread pool to fetch translations for multiple locales in parallel via theLocalizationApiClient. - Thread-Safe Access:
- The
get()method is safe to call from different threads simultaneously. - The
apiClient.get()calls during a refresh are executed in multiple threads to speed up the process.
- The
- Concurrency Control: Prevents simultaneous execution of the
refresh()method. If one thread is already refreshing the cache, other concurrent calls torefresh()will exit immediately to avoid redundant work.
- Atomic Updates: Uses
AtomicReferenceto store the cache, ensuring that the entire cache is updated atomically and consistently. - Non-blocking Refresh Check: Uses
AtomicBooleanwithcompareAndSetto manage the "refreshing" state without traditional heavy locking. - Parallelism: Employs
CompletableFutureand a customExecutorServiceto perform concurrent API requests for different locales.
Call refresh(ids, locales) to update the local cache from the remote API.
localization.refresh(List.of(1, 2, 3), List.of(Locale.US, Locale.FRANCE));Use get(id, locale) to fetch a value from the cache.
String greeting = localization.get(1, Locale.US);