Thread-Safe Cache

Difficulty: Medium · Topics: Design, Concurrency, Cache, Locking

Difficulty: Medium  |  Tags: Concurrency, Design, Cache, Locking

Description

Design a thread-safe, in-memory cache with expiration support (TTL). Multiple threads should be able to read and write concurrently without data corruption.

Implement:

ThreadSafeCache<K, V>
  put(key, value, ttlMs)    — store with time-to-live
  get(key) → V | null       — return value if not expired
  remove(key)               — remove entry
  size() → int              — number of non-expired entries
  clear()                   — remove all entries

Examples

cache = new ThreadSafeCache()

cache.put("token", "abc123", 5000)   // expires in 5 seconds
cache.get("token")                    // returns "abc123"

// ... 6 seconds later ...
cache.get("token")                    // returns null (expired)

Concurrent scenario:

// Thread 1                    // Thread 2
cache.put("x", 1, 10000)      cache.put("y", 2, 10000)
cache.get("y")  // → 2         cache.get("x")  // → 1

// No data corruption, no ConcurrentModificationException

Constraints

Hints

Hint 1

Use ConcurrentHashMap with entries storing the value + expiration timestamp.

Hint 2

For lazy expiration: on get, check if System.currentTimeMillis() > expiresAt. If so, remove and return null.

Follow-up

Practice this problem

Related problems