A TypeScript decorator library for caching Observable and Promise-returning methods with configurable storage strategies.
ts-cacheable is a TypeScript library that provides decorators to cache the results of methods returning Observables or Promises. It solves the problem of redundant asynchronous calls (like HTTP requests) by storing results based on method parameters, with configurable expiration and storage strategies. Originally built for Angular, it's now platform-agnostic and works in any TypeScript or JavaScript environment.
Developers building applications with repetitive asynchronous calls, especially those using Angular, RxJS, or Promise-based APIs who need performance optimization through caching.
Developers choose ts-cacheable for its simple decorator-based API, flexible storage strategies (including custom backends), and fine-grained cache control without modifying core logic. It reduces boilerplate compared to manual caching implementations.
Observable/Promise Cache Decorator
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
The @Cacheable() decorator caches method returns without modifying business logic, as shown in the getUsers() example where HTTP calls are automatically memoized based on parameters.
Supports in-memory, localStorage, and custom backends via IStorageStrategy, allowing persistence or integration with systems like Redis, though async strategies are limited to promises.
Offers configurable options like maxAge, sliding expiration, and cache busting with CacheBuster decorators, enabling fine-tuned cache management for diverse use cases.
The cacheModifier subject allows dynamic updates to cached data, demonstrated in the example where cached responses can be modified without refetching.
Async storage strategies (e.g., for Redis) are only supported for Promise-returning methods, not Observables, requiring workarounds for RxJS-based applications.
Heavily relies on TypeScript decorators, making it incompatible with environments lacking decorator support or projects using alternative coding paradigms.
The ICacheConfig interface has numerous options (e.g., cacheResolver, cacheHasher) that can be complex to set up correctly for simple caching needs.