Clarification on ts-cacheable Cache Busting Policy

I’m considering using the ts-cacheable library (github - angelnikolov/ts-cacheable) for a personal project, and I need some clarification regarding its cache management policy, specifically concerning method calls with different parameters.

The documentation states:

If we call this method by service.getUser(1), its return value will be cached and returned, up until the method is called with a different parameter. Then the old cache will be busted and a new one will take its place.

This suggests that if I call sayHelloWorldTo('Chandler') and then sayHelloWorldTo('Monica'), the cached entry for ‘Chandler’ would be replaced by the one for ‘Monica’. A subsequent call to sayHelloWorldTo('Chandler') would then bust the ‘Monica’ entry and create a new one for ‘Chandler’.

Is there a way to cache multiple results from the same method call? My goal is to cache up to 20 unique results for a method. I’d expect the following sequence of calls to persist results in the cache without overwriting each other:

sayHelloWorldTo('Phoebe');
sayHelloWorldTo('Ross');
sayHelloWorldTo('Chandler');
sayHelloWorldTo('Phoebe');
sayHelloWorldTo('Joey');
sayHelloWorldTo('Monica');

Could you confirm if this is the intended behavior and if there’s a configuration option or decorator that would allow for multiple cached entries per method?


The function and its decorator are like the following:

@PCacheable({
        ...
})
private async sayHelloWorldTo(name: string): Promise<string> {
   ...
}

Answer:

  • Yes, the behavior you described is intentional: ts-cacheable by default caches only one result per method, and calling with a new argument evicts the previous cache, even for different inputs Reddit+15GitHub+15GitHub+15.
  • There’s no built-in way to cache multiple distinct inputs simultaneously unless you explicitly configure it.

How to support caching multiple parameterized calls (e.g. up to 20 names)

Use the maxCacheCount option in the decorator or global config to allow multiple cached entries:

@PCacheable({
  maxCacheCount: 20,
  // optional: custom cacheHasher if needed per input combination
})
async sayHelloWorldTo(name: string): Promise<string> {
  return `Hello ${name}!`;
}

This allows up to 20 distinct name inputs to be cached (e.g. "Phoebe", "Ross", "Chandler", etc.) without evicting older entries prematurely GitHubGitHub+3NPM+3GitHub+3.


Why it works:

  • maxCacheCount lets the library store multiple unique responses keyed by distinct parameters.
  • Without it, the default behavior retains only the most recent call, evicting older cache entries when new ones arrive GitHub+1.

Summary Table

Scenario Behavior by Default Behavior with maxCacheCount
sayHelloWorldTo('Phoebe') caches Phoebe result caches Phoebe result
sayHelloWorldTo('Ross') evicts Phoebe, caches Ross adds Ross, keeps Phoebe
…repeat for different names up to limit still only one kept accumulates multiple entries
Exceeding maxCacheCount N/A evicts oldest cache entry

Final notes:

  • Default behavior evicts previous call when new parameters appear.
  • To preserve multiple parameterized cache entries, use maxCacheCount.
  • Optionally use cacheHasher to customize how parameter combinations map to keys.
  • You can also set a global default with GlobalCacheConfig.maxCacheCount.

Let me know if you’d like an example with cacheHasher or custom eviction logic!