Search⌘ K
AI Features

Solution: Unify Cache Clients

Explore how to unify in-memory and Redis-like cache clients by implementing a CacheAdapter class. Learn to standardize different interfaces and normalize synchronous and asynchronous methods to provide a consistent, awaitable API across various cache systems.

Solution explanation

  • Lines 2–11: We define the in-memory cache client.

    • It stores data in a simple object.

    • The API is synchronous, using setItem() and getItem().

    • It returns values directly, not promises.

  • Lines 14–31: We define a Redis-like asynchronous cache client.

    • Both set() and get() methods return promises.

    • Each simulates a short delay to mimic real network latency.

    • The structure mirrors a real async client, like ioredis or node-redis.

  • Lines 34–36: The CacheAdapter constructor stores the provided client.

...