In this article, we explore how to extend an in-memory cache with configurable data persistence.
The goal is to design a flexible and scalable caching system that decouples storage mechanisms from the caching logic.
The article leverages the Adapter and Strategy patterns to create an extensible system that allows seamless integration of databases or services as needed.
A shared API abstraction, akin to ORM systems, ensures storage such as localStorage, IndexedDB, or even a remote database can work interchangeably with minimal code changes.
The cache is agnostic of storage details, and there is a flexible runtime selection of storage backend using the Strategy pattern.
Additionally, the article demonstrates best practices for singleton cache, async initialization, and lazy loading.
This design is robust and is available in the form of an npm package—adev-lru.
The article provides scope for further enhancements, such as optimizing sync logic for better performance.
The full source code for the adev-lru repository is available on GitHub.
The author welcomes contributions to make this caching system even better.