Implement Python LRU cache with varargs
Problem Description
Implement an LRU (Least Recently Used) cache in Python. This cache should be exposed as a decorator, cache(maxsize), designed to memoize the results of a target function.
Core Functionality
- Argument Handling: The cache must correctly handle positional arguments,
*args, and**kwargs. Crucially, semantically equivalent function calls (e.g.,f(1, b=2)andf(1, 2)) must map to the same unique cache key. - Unhashable Arguments: Implement a strategy to handle unhashable arguments (e.g., lists, dictionaries, custom objects) by canonicalizing them into a deterministic, hashable representation for use as cache keys.
- LRU Eviction: The cache must correctly implement the Least Recently Used eviction policy when
maxsizeis reached. - Utility Methods: Provide the following operations:
clear(): Empties the cache.cache_info(): Returns statistics about the cache (e.g., hits, misses, current size, max size).
Follow-up: Persistence
Extend the cache with persistence capabilities:
save(filepath): Serializes the current state of the cache (keys and values) to a file at the givenfilepath.load(filepath): Deserializes the cache state from a file at the givenfilepath, restoring its contents.- Serialization Format: Serialization must use JSON. Python's
picklemodule is explicitly disallowed. - Design Considerations for Persistence:
- Clearly define your strategy for encoding cache keys and values into a JSON-serializable format.
- Address how to handle function return values that are inherently non-JSON-serializable when saving/loading.
- Propose a mechanism for handling versioning of the serialized cache data to ensure forward/backward compatibility.
AI interview prep
Core dilemma
Balancing efficient, deterministic key generation for complex, potentially unhashable function arguments with the need for robust, versioned JSON serialization of arbitrary Python objects (both keys and values) for persistence, all within the constraints of an LRU eviction policy.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement Python LRU cache with varargs
Problem Description
Implement an LRU (Least Recently Used) cache in Python. This cache should be exposed as a decorator, cache(maxsize), designed to memoize the results of a target function.
Core Functionality
- Argument Handling: The cache must correctly handle positional arguments,
*args, and**kwargs. Crucially, semantically equivalent function calls (e.g.,f(1, b=2)andf(1, 2)) must map to the same unique cache key. - Unhashable Arguments: Implement a strategy to handle unhashable arguments (e.g., lists, dictionaries, custom objects) by canonicalizing them into a deterministic, hashable representation for use as cache keys.
- LRU Eviction: The cache must correctly implement the Least Recently Used eviction policy when
maxsizeis reached. - Utility Methods: Provide the following operations:
clear(): Empties the cache.cache_info(): Returns statistics about the cache (e.g., hits, misses, current size, max size).
Follow-up: Persistence
Extend the cache with persistence capabilities:
save(filepath): Serializes the current state of the cache (keys and values) to a file at the givenfilepath.load(filepath): Deserializes the cache state from a file at the givenfilepath, restoring its contents.- Serialization Format: Serialization must use JSON. Python's
picklemodule is explicitly disallowed. - Design Considerations for Persistence:
- Clearly define your strategy for encoding cache keys and values into a JSON-serializable format.
- Address how to handle function return values that are inherently non-JSON-serializable when saving/loading.
- Propose a mechanism for handling versioning of the serialized cache data to ensure forward/backward compatibility.