You are tasked with building a lightweight, in-memory Mini Redis API using Express. The application should act as a key-value store supporting string operations, Time-To-Live (TTL) expiration, and Least Recently Used (LRU) capacity eviction.
Currently, the server routing, storage engine, and command handlers are non-functional or incomplete. None of the test cases are passing.
Requirements
Implement the API endpoints in src/cacheEngine.js and src/app.js to satisfy the following requirements:
1. Basic Key-Value Operations
POST /api/set: Accepts { "key": string, "value": string, "ttl": number (optional) }.
- Stores the key-value pair.
- If optional
ttl (in seconds) is provided, sets an expiration timer for that key.
- Returns status
200 with { "success": true, "key": string }.
- Returns status
400 if key or value is missing/invalid.
GET /api/get/:key:
- If the key exists and is not expired, returns status
200 with { "key": string, "value": string }.
- Reading a key marks it as recently used (updates LRU order).
- If the key does not exist or has expired, returns status
404 with { "error": "Key not found" }.
DELETE /api/del/:key:
- Removes the key from the cache.
- Returns status
200 with { "success": true, "deleted": true } if key existed, or { "success": false, "deleted": false } if key was not found.
2. TTL & Expiration Handling
POST /api/expire: Accepts { "key": string, "seconds": number }.
- Sets/updates expiration timeout on an existing key.
- Returns status
200 with { "success": true } if key exists.
- Returns status
404 with { "error": "Key not found" } if key does not exist or is already expired.
GET /api/ttl/:key:
- Returns remaining TTL in seconds: status
200 with { "key": string, "ttl": number }.
- If key exists but has no expiration, return
{ "key": string, "ttl": -1 }.
- If key does not exist or is expired, return status
404 with { "error": "Key not found" }.
3. LRU Eviction Policy
- The cache has a fixed capacity limit configured via environment variable
CACHE_CAPACITY (default to 3 if not set or invalid).
- When a new key is inserted via
/api/set and capacity is reached, the Least Recently Used (LRU) non-expired key must be evicted.
- Updating an existing key's value via
/api/set updates its value/TTL and marks it as recently used, but does not increase total count or trigger eviction.
- Any access (
GET /api/get/:key) marks the key as recently used.
Instructions
- Edit
src/cacheEngine.js to build the underlying LRUCache data structure and expiration logic.
- Edit
src/app.js to hook up the HTTP routes to your cache engine.
- Ensure all tests in
tests/redis.test.js pass.
Files to Edit
src/cacheEngine.js
src/app.js