Problem Statement
Show how you would add read-through caching to a repository using Decorator.
Explanation
Wrap the repository with a decorator that checks cache first. On miss, it calls the inner repo, stores the result with a TTL, then returns it. The interface stays the same, so callers do not change.
You can stack more decorators for logging or rate limiting. Keep key design, TTL policy, and invalidation strategy explicit so staleness is predictable.
Code Solution
SolutionRead Only
class CachedRepo implements Repo{
get(k){ v=cache.get(k); if(v) return v; v=inner.get(k); cache.set(k,v,ttl); return v; }
}