Registry / storage / ts-cacheable

ts-cacheable

JSON →
library1.0.19jsnpmunverified

ts-cacheable (v1.0.19) is a TypeScript library providing decorators for caching Observable and Promise return values from class methods. It uses an in-memory cache by default and supports configurable cache busting, parameter-based cache keys, max age, sliding expiration, and async resolution. The library was originally designed for Angular (as ngx-cacheable) but is now platform-agnostic. It ships with TypeScript type definitions. Release cadence is low; last release was in 2021.

npm install ts-cacheable
INSTALL
IMPORT
SIG · TS-CACHEABLE
T
ts-cacheable
storagejavascriptv1.0.19
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Cacheable
✓ import { Cacheable } from 'ts-cacheable'
✗ const { Cacheable } = require('ts-cacheable')
ESM-only; CommonJS require may not work as expected in some bundlers. Named export.
PCacheable
✓ import { PCacheable } from 'ts-cacheable'
✗ import { Cacheable } from 'ts-cacheable'
PCacheable is for Promise-returning methods. Common mistake is using Cacheable (Observable) on a Promise method.
CacheBuster
✓ import { CacheBuster } from 'ts-cacheable'
✗ import CacheBuster from 'ts-cacheable'
CacheBuster is a named export, not default. Used to bust caches when a method is called.
ICacheConfig
✓ import { ICacheConfig } from 'ts-cacheable'
TypeScript type import for configuring Cacheable decorator. No common mistake.

Shows Cacheable decorator on Observable-returning methods with in-memory caching and parameter-based cache keys.

import { Cacheable } from 'ts-cacheable'; import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; @Injectable({ providedIn: 'root' }) export class UserService { constructor(private http: HttpClient) {} @Cacheable() getUsers() { return this.http.get('/users'); } @Cacheable({ maxAge: 30000, slidingExpiration: true }) getUser(id: number) { return this.http.get(`/users/${id}`); } } // Usage const service = inject(UserService); service.getUsers().subscribe(); // makes HTTP call service.getUsers().subscribe(); // returns cached result
Debug
Known issues
breakingThe package was renamed from ngx-cacheable to ts-cacheable. Projects using the old name will break.
fix
Replace all imports from 'ngx-cacheable' with 'ts-cacheable'.
affects: >=0.2.0
deprecatedThe old decorator name 'Cacheable' is still available but may be deprecated in future versions.
fix
Use 'Cacheable' for Observable, 'PCacheable' for Promise. The old 'Cacheable' works for both but is less explicit.
affects: >=1.0.0
gotchaUsing Cacheable on a Promise-returning method without PCacheable may cause type errors or unexpected behavior.
fix
Use PCacheable for Promise-returning methods instead of Cacheable.
affects: >=1.0.0
gotchaCacheable requires TypeScript's experimentalDecorators to be enabled in tsconfig.json.
fix
Set "experimentalDecorators": true in tsconfig.json.
affects: >=0.1.0
gotchaThe default cache is in-memory only; it does not persist across page reloads.
fix
If persistence is needed, implement a custom storage strategy using the cacheHasher and ICachePair interface.
affects: >=1.0.0
breakingVersion 1.0.0 changed the cache key hashing algorithm, breaking existing caches on upgrade.
fix
Clear any persisted in-memory caches after upgrade; they will be rebuilt.
affects: >=0.2.0 <1.0.0
deprecatedThe ICacheConfig.shouldCacheDecider is not well documented and its behavior might change.
fix
Test thoroughly and consider using cacheResolver instead for custom cache logic.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot use decorators unless 'experimentalDecorators' is enabled
TypeScript configuration missing experimentalDecorators.
fix
Set "experimentalDecorators": true in tsconfig.json and restart the TS server.
TypeError: Cannot read property 'get' of undefined
Using Cacheable decorator on a method of a class that isn't instantiated via dependency injection (e.g., calling static method or manually instantiated class).
fix
Ensure the class is instantiated properly, or use the decorator only on methods within an Angular service or similar DI context.
Argument of type 'Observable<...>' is not assignable to parameter of type 'PromiseLike<...>'
Using Cacheable on a Promise-returning method instead of PCacheable.
fix
Use PCacheable decorator for Promise-returning methods.
Cannot find module 'ts-cacheable' or its corresponding type declarations.
Package not installed or TypeScript cannot resolve module.
fix
Run 'npm install ts-cacheable' and ensure tsconfig.json includes node_modules in paths or types.
Error encountered in cache pipeline for method 'yourMethodName': Maximum call stack size exceeded
Circular cache busting or recursive method call.
fix
Avoid calling the same cached method inside itself; use cacheBusterObserver to break recursion.
Upgrade
Version history
1.0.19latest on npm
Audit
Dependencies
tsliboptionalRequires tslib for helper functions when using importHelpers in TypeScript
Agent activity
29 hits · last 30 days
node
26
Amazon
1
OpenAI (training)
1
Resources
ts-cacheable — npm install ts-cacheable · libregistry