# preetiguptaeee24/cachevault-java-swing-multithreading architecture

> Architecture diagram of the GitHub repository preetiguptaeee24/cachevault-java-swing-multithreading, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/preetiguptaeee24/cachevault-java-swing-multithreading
- Repository: https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading

## Overview

#### Purpose and Entry Points
CacheVault is a Java in-memory cache demonstrating bounded LRU eviction, optional TTL expiration, and lookup statistics. It has two executable entry points:
- CLI launcher: `src/Main.java`
- Swing GUI launcher: `src/MainGUI.java`

Both interfaces operate on the same cache implementation rather than exposing a networked service or persistence layer.

#### Interface Layer
The UI adapters collect cache operations and present cache state/statistics:
- Interactive command REPL: `src/ui/CacheVaultCLI.java`
- Desktop Swing application: `src/ui/CacheVaultGUI.java`

Supported control flow is put/get/remove plus cache inspection and stats. UI threads are consumers of the cache API; they should not own cache policy or expiry logic.

#### Core LRU Engine
`src/core/LRUCache.java` is the primary bounded-cache engine. It combines:
- A hash map from key to linked-list node for direct lookup.
- A recency-ordered doubly linked list for constant-time promotion and least-recently-used eviction.

`src/core/Node.java` represents list nodes. Sentinel head/tail nodes simplify insertion/removal boundaries. Reads promote entries; writes update or insert entries; capacity pressure removes the tail-side least-recently-used entry.

#### TTL Extension and Expiry Lifecycle
`src/expiry/ExpiringLRUCache.java` layers expiration behavior over the LRU cache:
- `get` performs lazy expiry validation so expired values are never returned.
- A scheduled background sweeper actively removes stale entries that are not subsequently read.

`src/expiry/CacheEntry.java` wraps cached values with expiry metadata/timestamps. TTL is optional, so non-expiring entries continue through the normal LRU path.

#### Statistics Boundary
`src/stats/CacheStats.java` tracks hit and miss counters and derives hit ratio. It is separate from storage/eviction mechanics, allowing both UI modes and expiry-aware lookups to report shared cache behavior without coupling presentation to cache internals.

#### Concurrency and Runtime
The application runs on the JVM using standard Java concurrency primitives:
- Core cache operations are synchronized to protect map/list mutations across UI access and expiry sweeping.
- `AtomicLong` counters support thread-safe statistics.
- `ScheduledExecutorService` drives active TTL cleanup.

There is no external datastore, HTTP API, cloud infrastructure, or third-party service dependency. All cached data is process-local and lost on exit.

Main entry points: [`src/expiry/ExpiringLRUCache.java`](https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/expiry/ExpiringLRUCache.java) (Expiry-Aware Cache), [`src/core/LRUCache.java`](https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/core/LRUCache.java) (LRU Cache), [`src/ui/CacheVaultCLI.java`](https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/ui/CacheVaultCLI.java) (Command REPL), [`src/ui/CacheVaultGUI.java`](https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/ui/CacheVaultGUI.java) (Swing Application)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["Launchers &amp; UI"]
  node_main["CLI Launcher<br/>Java entry point<br/>[Main.java]"]
  node_main_gui["GUI Launcher<br/>Java entry point<br/>[MainGUI.java]"]
  node_cli["Command REPL<br/>CLI adapter<br/>[CacheVaultCLI.java]"]
  node_gui["Swing Application<br/>desktop UI adapter<br/>[CacheVaultGUI.java]"]
end

subgraph group_core["Cache Core"]
  node_lru_cache["LRU Cache<br/>bounded cache engine<br/>[LRUCache.java]"]
  node_index[("Key Index<br/>hash map")]
  node_recency_list["Recency List<br/>doubly linked list"]
  node_node["List Node<br/>[Node.java]"]
end

subgraph group_expiry["TTL Extension"]
  node_expiring_cache["Expiry-Aware Cache<br/>TTL cache layer"]
  node_cache_entry["Cache Entry<br/>value wrapper<br/>[CacheEntry.java]"]
  node_sweeper["Scheduled Sweeper<br/>ScheduledExecutorService"]
end

node_stats["Cache Statistics<br/>AtomicLong counters<br/>[CacheStats.java]"]
node_jvm{{"JVM Process Memory<br/>runtime boundary"}}

node_main -->|"launches"| node_cli
node_main_gui -->|"launches"| node_gui
node_cli -->|"cache operations"| node_expiring_cache
node_gui -->|"cache operations"| node_expiring_cache
node_cli -->|"reports"| node_stats
node_gui -->|"reports"| node_stats
node_expiring_cache -->|"normal LRU path"| node_lru_cache
node_expiring_cache -->|"wraps values"| node_cache_entry
node_sweeper -->|"removes stale entries"| node_expiring_cache
node_expiring_cache -->|"records lookup outcomes"| node_stats
node_lru_cache -->|"direct key lookup"| node_index
node_lru_cache -->|"promotes and evicts"| node_recency_list
node_recency_list -->|"links entries"| node_node
node_lru_cache -.->|"stores in memory"| node_jvm
node_sweeper -.->|"runs in process"| node_jvm

click node_main "https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/Main.java"
click node_main_gui "https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/MainGUI.java"
click node_cli "https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/ui/CacheVaultCLI.java"
click node_gui "https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/ui/CacheVaultGUI.java"
click node_expiring_cache "https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/expiry/ExpiringLRUCache.java"
click node_cache_entry "https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/expiry/CacheEntry.java"
click node_lru_cache "https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/core/LRUCache.java"
click node_node "https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/core/Node.java"
click node_stats "https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/stats/CacheStats.java"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_main_gui,node_cli,node_gui toneBlue
class node_lru_cache,node_index,node_recency_list,node_node toneAmber
class node_expiring_cache,node_cache_entry,node_sweeper toneMint
class node_stats,node_jvm toneNeutral
```

## Components

### Launchers & UI

- [CLI Launcher](https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/Main.java): Java entry point · `src/Main.java`
- [GUI Launcher](https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/MainGUI.java): Java entry point · `src/MainGUI.java`
- [Command REPL](https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/ui/CacheVaultCLI.java): CLI adapter · Collects put/get/remove, inspection, and stats commands. · `src/ui/CacheVaultCLI.java`
- [Swing Application](https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/ui/CacheVaultGUI.java): desktop UI adapter · Presents cache operations, state, and statistics. · `src/ui/CacheVaultGUI.java`

### Cache Core

- [LRU Cache](https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/core/LRUCache.java): bounded cache engine · Synchronized lookup, promotion, update, insertion, and LRU eviction. · `src/core/LRUCache.java`
- **Key Index**: hash map · Maps keys directly to recency-list nodes.
- **Recency List**: doubly linked list · Maintains most-recent to least-recent ordering.
- [List Node](https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/core/Node.java): linked-list node · Sentinel head/tail nodes simplify list boundaries. · `src/core/Node.java`

### TTL Extension

- [Expiry-Aware Cache](https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/expiry/ExpiringLRUCache.java): TTL cache layer · Validates expiry on reads and supports active stale-entry cleanup. · `src/expiry/ExpiringLRUCache.java`
- [Cache Entry](https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/expiry/CacheEntry.java): value wrapper · Carries cached value and optional expiry metadata. · `src/expiry/CacheEntry.java`
- **Scheduled Sweeper**: ScheduledExecutorService · Actively removes expired entries in the background.

### Other components

- [Cache Statistics](https://github.com/preetiguptaeee24/cachevault-java-swing-multithreading/blob/main/src/stats/CacheStats.java): AtomicLong counters · Tracks hits, misses, and derived hit ratio. · `src/stats/CacheStats.java`
- **JVM Process Memory**: runtime boundary · All cached data is process-local and lost on exit.

## Connections

- CLI Launcher → Command REPL: launches
- GUI Launcher → Swing Application: launches
- Command REPL → Expiry-Aware Cache: cache operations
- Swing Application → Expiry-Aware Cache: cache operations
- Command REPL → Cache Statistics: reports
- Swing Application → Cache Statistics: reports
- Expiry-Aware Cache → LRU Cache: normal LRU path
- Expiry-Aware Cache → Cache Entry: wraps values
- Scheduled Sweeper → Expiry-Aware Cache: removes stale entries
- Expiry-Aware Cache → Cache Statistics: records lookup outcomes
- LRU Cache → Key Index: direct key lookup
- LRU Cache → Recency List: promotes and evicts
- Recency List → List Node: links entries
- LRU Cache → JVM Process Memory: stores in memory
- Scheduled Sweeper → JVM Process Memory: runs in process
