How preetiguptaeee24/cachevault-java-swing-multithreading works
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: ExpiringLRUCache.java Expiry-Aware Cache · LRUCache.java LRU Cache · CacheVaultCLI.java Command REPL · CacheVaultGUI.java Swing Application
Components 13
Launchers & UI
- CLI Launcher
src/Main.java - GUI Launcher
src/MainGUI.java - Command REPL · Collects put/get/remove, inspection, and stats commands.
src/ui/CacheVaultCLI.java - Swing Application · Presents cache operations, state, and statistics.
src/ui/CacheVaultGUI.java
Cache Core
TTL Extension
- Expiry-Aware Cache · Validates expiry on reads and supports active stale-entry cleanup.
src/expiry/ExpiringLRUCache.java - Cache Entry · Carries cached value and optional expiry metadata.
src/expiry/CacheEntry.java - Scheduled Sweeper · Actively removes expired entries in the background.
Other components
- Cache Statistics · Tracks hits, misses, and derived hit ratio.
src/stats/CacheStats.java - JVM Process Memory · All cached data is process-local and lost on exit.
Connections 15
- 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