A multi-layered caching system that fetches exchange rates from OpenExchangeRates API with intelligent storage management.
Build a ChainResource class that manages multiple storage layers (memory, file, web service) to efficiently cache and retrieve data. When data is requested, the system checks each storage layer in order, and if found in a deeper layer, automatically propagates it back to faster layers for future access.
The system implements a three-tier caching strategy:
- Memory Cache (fastest, 1 hour expiration)
- File Cache (medium speed, 4 hours expiration)
- Web API (slowest, always fresh)
When you request exchange rates:
- First checks memory - if found and not expired, returns immediately
- If not in memory, checks file cache - if found, saves to memory and returns
- If not in file, calls the API - saves to both file and memory, then returns
This approach minimizes API calls while keeping data reasonably fresh.
- Java 21 or higher
- Maven 3.9+
- OpenExchangeRates API key (free at https://openexchangerates.org/)
-
Clone or download the project
-
Configure your API key
Copy the example configuration:
cp application.properties.example application.properties
Edit
application.propertiesand add your API key:openexchangerates.api.key=your_api_key_hereAlternatively, use an environment variable:
export OPENEXCHANGERATES_API_KEY=your_api_key_here -
Build and run
mvn clean compile exec:java
ChainResource - Manages the storage chain and handles the lookup logic
Storage Interface - Defines the contract for all storage types:
get()- retrieve dataset()- store dataisExpired()- check if data is staleisWritable()- check if storage supports writes
Three Storage Implementations:
MemoryStorage- Fast in-memory cacheFileSystemStorage- Persistent JSON file cacheWebServiceStorage- HTTP API client (read-only)
Request → Memory (expired?) → File (expired?) → API → Save to File → Save to Memory → Return
Each storage layer has an expiration time. When data is found in a deeper layer, it's automatically copied to all faster layers above it (cache warming).
src/main/java/com/mize/assessment/
├── ChainResource.java # Main chain logic
├── Main.java # Demo application
├── config/
│ └── AppConfig.java # Configuration loader
├── model/
│ └── ExchangeRateList.java # Data model
└── storage/
├── Storage.java # Interface
├── MemoryStorage.java # Memory cache
├── FileSystemStorage.java # File cache
└── WebServiceStorage.java # API client
Fetching exchange rates...
Base Currency: USD
Total Currencies: 172
Sample Rates:
AED: 3.6725
AFN: 67.4781
ALL: 82.4306
AMD: 383.0573
ANG: 1.7900
...