# @t.voslar/ts-cache-decorator

> Cache decorators with different strategies.

Latest version **2.3.0** (published 2026-09-17) · 0 weekly downloads

## Install

```sh
npm install @t.voslar/ts-cache-decorator
pnpm add @t.voslar/ts-cache-decorator
yarn add @t.voslar/ts-cache-decorator
bun add @t.voslar/ts-cache-decorator
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.3.0 |
| Published | 2026-09-17 |
| First published | 2019-08-02 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 39.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | t.voslar |
| Keywords | cache, decorator, typescript, redis |

## Links

- npm: https://www.npmjs.com/package/@t.voslar/ts-cache-decorator
- Repository: https://github.com/voslartomas/ts-cache-decorators
- Homepage: https://github.com/voslartomas/ts-cache-decorators#readme
- Issues: https://github.com/voslartomas/ts-cache-decorators/issues
- npm.io page: https://npm.io/package/@t.voslar/ts-cache-decorator

## Dependencies (4)

- [hasha](https://npm.io/package/hasha.md) ^3.0.0
- [ioredis](https://npm.io/package/ioredis.md) ^4.14.0
- [@types/ioredis](https://npm.io/package/@types/ioredis.md) ^4.0.13
- [memcached-elasticache](https://npm.io/package/memcached-elasticache.md) ^1.1.1

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 2.3.0 (latest) — 2026-09-17
- 2.2.1 — 2021-03-10
- 2.2.0 — 2020-03-21
- 2.1.6 — 2020-03-21
- 2.1.5 — 2020-03-21
- 2.1.4 — 2020-03-21
- 2.1.3 — 2020-03-21
- 2.1.2 — 2020-03-21
- 2.1.1 — 2020-03-21
- 2.1.0 — 2020-03-21
- 2.0.2 — 2020-03-21
- 2.0.1 — 2020-03-21
- 2.0.0 — 2020-03-21
- 1.4.1 — 2020-02-11
- 1.4.0 — 2019-08-10
- … 6 more at https://npm.io/package/@t.voslar/ts-cache-decorator/versions

## README

# ts-cache-decorators


## Install
```
npm i @t.voslar/ts-cache-decorator
```

## Configuration

Choose one of the supported storages (MemcacheElastiCache or RedisStorage) or create your new storage which implements IStorage.

```typescript
const cacheStorage = new MemcachedElastiCacheStorage('localhost:11211', {})
useStorage(cacheStorage)
```

Now you can use @Cache decorator
```typescript
@Cache({ type: 'normal', cacheKey: 'getUser', ttl: 3 })
async getUser (userId: string): Promise<User> {
```

Above caching will take cacheKey + parameters (which is just userId in this case) and will create cache key, which will expire after 3 seconds.

If you need to filter some parameters you can use filterParams param.
```typescript
@Cache({ type: 'normal', cacheKey: 'getUser', ttl: 3, filterParams: ['otherParam'] })
async getUser (userId: string, otherParam: boolean): Promise<User> {
```

### Concurrent misses

Calls that miss the same key at the same time are collapsed into one: the first
caller runs the method, the rest await its result. Without that, a burst of
traffic against a cold or just-expired key runs the underlying work once per
caller - the cache only starts absorbing load after the first call has finished
writing, which is the opposite of what you want at exactly that moment.

```typescript
// getUser runs once, not five times
await Promise.all([
  service.getUser('1'),
  service.getUser('1'),
  service.getUser('1'),
  service.getUser('1'),
  service.getUser('1')
])
```

This is per-process. It deduplicates within one Node instance, not across a
cluster, so with N replicas the worst case is N concurrent computations rather
than N x (concurrent requests per replica). Going further needs a distributed
lock, which is a heavier thing to put in the read path of a cache.

A failed call is not cached and does not wedge the key - every waiter on that
call rejects, and the next caller retries.

### Cache API

| Param | Value | Description |
| ---- | ---- | --- |
| type | 'normal' | Type of cache. |
| cacheKey | string | Caching key. |
| ttl  | number  | Time to live.  |
| filterParams  | string[]  | Parameters which should not be inside cache key.  |

---
_Source: https://npm.io/package/@t.voslar/ts-cache-decorator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
