...

/

Solution: Cache Data Using Selectable Backends

Solution: Cache Data Using Selectable Backends

Implement interchangeable caching strategies and delegate storage operations through a configurable CacheService context.

We'll cover the following...

Solution explanation

  • Lines 4–16: We define the MemoryCache strategy.

    • Maintains a simple in-memory object store for quick lookups.

    • .get() returns a cached value or null.

    • .set() directly mutates the store.

    • Ideal for development or temporary caching.

  • Lines 18–37: We define the FileCache strategy.

    • Uses a JSON file (cache.json) to simulate persistent storage.

    • On construction, ensures the file exists.

    • Each .get() and .set() call reads and writes synchronously for simplicity.

    • This models a production-like persistent cache backend.

  • Lines 39–47: We ...