Skip to content

fulldev1123/Mize-Chain-Resource

Repository files navigation

Resource Chain - Exchange Rate Caching System

A multi-layered caching system that fetches exchange rates from OpenExchangeRates API with intelligent storage management.

Problem Statement

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.

Solution Overview

The system implements a three-tier caching strategy:

  1. Memory Cache (fastest, 1 hour expiration)
  2. File Cache (medium speed, 4 hours expiration)
  3. 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.

Setup

Prerequisites

Installation

  1. Clone or download the project

  2. Configure your API key

    Copy the example configuration:

    cp application.properties.example application.properties

    Edit application.properties and add your API key:

    openexchangerates.api.key=your_api_key_here

    Alternatively, use an environment variable:

    export OPENEXCHANGERATES_API_KEY=your_api_key_here
  3. Build and run

    mvn clean compile exec:java

How It Works

Core Components

ChainResource - Manages the storage chain and handles the lookup logic

Storage Interface - Defines the contract for all storage types:

  • get() - retrieve data
  • set() - store data
  • isExpired() - check if data is stale
  • isWritable() - check if storage supports writes

Three Storage Implementations:

  • MemoryStorage - Fast in-memory cache
  • FileSystemStorage - Persistent JSON file cache
  • WebServiceStorage - HTTP API client (read-only)

The Flow

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

Project Structure

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

Example Output

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
  ...

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages