Skip to content

Anton-Mrakin/LocalizationAsyncConcurrentCache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SalmonProject - Localization Cache

This project implements a local cache for translations, mapping unique IDs to strings across various world languages.

Key Features

  • 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 the LocalizationApiClient.
  • 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.
  • Concurrency Control: Prevents simultaneous execution of the refresh() method. If one thread is already refreshing the cache, other concurrent calls to refresh() will exit immediately to avoid redundant work.

Implementation Details

  • Atomic Updates: Uses AtomicReference to store the cache, ensuring that the entire cache is updated atomically and consistently.
  • Non-blocking Refresh Check: Uses AtomicBoolean with compareAndSet to manage the "refreshing" state without traditional heavy locking.
  • Parallelism: Employs CompletableFuture and a custom ExecutorService to perform concurrent API requests for different locales.

Usage

Refreshing the Cache

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));

Retrieving Translations

Use get(id, locale) to fetch a value from the cache.

String greeting = localization.get(1, Locale.US);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages