# node-ts-cache

> Simple and extensible caching module supporting decorators

Latest version **4.4.0** (published 2021-10-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-ts-cache
pnpm add node-ts-cache
yarn add node-ts-cache
bun add node-ts-cache
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.4.0 |
| Published | 2021-10-11 |
| First published | 2017-03-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 24.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 50 |
| Author | Himmet Avsar |
| Maintainers | havsar |
| Keywords | node, nodejs, cache, typescript, ts, caching, memcache, memory-cache, redis-cache, redis, cache, file-cache, node-cache, ts-cache |

## Links

- npm: https://www.npmjs.com/package/node-ts-cache
- Repository: https://github.com/havsar/node-ts-cache
- Homepage: https://github.com/havsar/node-ts-cache#readme
- Issues: https://github.com/havsar/node-ts-cache/issues
- npm.io page: https://npm.io/package/node-ts-cache

## Dependencies (3)

- [debug](https://npm.io/package/debug.md) ^4.3.2
- [redis](https://npm.io/package/redis.md) ^3.1.2
- [bluebird](https://npm.io/package/bluebird.md) 3.7.2

## 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

- 4.4.0 (latest) — 2021-10-11
- 4.3.4 — 2021-10-08
- 4.2.3 — 2020-12-31
- 4.2.2 — 2020-12-31
- 4.2.1 — 2020-12-31
- 3.2.0 — 2019-10-24
- 3.1.0 — 2019-09-07
- 2.0.0 — 2018-12-15
- 1.3.0 — 2018-11-21
- 1.2.0 — 2018-11-19
- 1.1.0 — 2018-11-18
- 0.3.0 — 2017-10-30
- 0.2.4 — 2017-10-24
- 0.2.3 — 2017-10-24
- 0.2.2 — 2017-10-23
- … 11 more at https://npm.io/package/node-ts-cache/versions

## README

[![CI](https://github.com/havsar/node-ts-cache/actions/workflows/ci.yml/badge.svg)](https://github.com/havsar/node-ts-cache/actions/workflows/ci.yml)
[![CI](https://github.com/havsar/node-ts-cache/actions/workflows/ci.yml/badge.svg?branch=development)](https://github.com/havsar/node-ts-cache/actions/workflows/ci.yml)
[![npm](https://img.shields.io/npm/v/node-ts-cache.svg)](https://www.npmjs.org/package/node-ts-cache)
[![The MIT License](https://img.shields.io/npm/l/node-ts-cache.svg)](http://opensource.org/licenses/MIT)

[![NPM](https://nodei.co/npm/node-ts-cache.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/node-ts-cache/)

# node-ts-cache

Simple and extensible caching module supporting decorators.

# Install

```bash
npm install node-ts-cache
```

_Note: The underlying storage layer must be installed separately._

# Storages

| Storage                                                               	| Install                                         	|
|-----------------------------------------------------------------------	|-------------------------------------------------	|
| [memory](https://www.npmjs.com/package/node-ts-cache-storage-memory)  	| ```npm install node-ts-cache-storage-memory```  	|
| [node-fs](https://www.npmjs.com/package/node-ts-cache-storage-node-fs) 	| ```npm install node-ts-cache-storage-node-fs``` 	|
| [ioredis](https://www.npmjs.com/package/node-ts-cache-storage-ioredis) 	| ```npm install node-ts-cache-storage-ioredis``` 	|

# Usage

## With decorator

Caches function response using the given options.
Works with the above listed storages.
By default, uses all arguments to build an unique key.

`@Cache(container, options)`

-   `options`:
    -   `ttl`: _(Default: 60)_ Number of seconds to expire the cachte item
    -   `isLazy`: _(Default: true)_ If true, expired cache entries will be deleted on touch. If false, entries will be deleted after the given _ttl_.
    -   `isCachedForever`: _(Default: false)_ If true, cache entry has no expiration.
    -   `calculateKey(data => string)`: _(Default: JSON.stringify combination of className, methodName and call args)_
        -   `data`:
            -   `className`: The class name for the method being decorated
            -   `methodName`: The method name being decorated
            -   `args`: The arguments passed to the method when called

_Note: @Cache will consider the return type of the function. If the return type is a thenable, it will stay that way, otherwise not._

```ts
import { Cache, CacheContainer } from 'node-ts-cache'
import { MemoryStorage } from 'node-ts-cache-storage-memory'

const userCache = new CacheContainer(new MemoryStorage())

class MyService {
    @Cache(userCache, {ttl: 60})
    public async getUsers(): Promise<string[]> {
        return ["Max", "User"]
    }
}
```

## Directly

```ts
import { CacheContainer } from 'node-ts-cache'
import { MemoryStorage } from 'node-ts-cache-storage-memory'

const myCache = new CacheContainer(new MemoryStorage())

class MyService {
    public async getUsers(): Promise<string[]> {
        const cachedUsers = await myCache.getItem<string[]>("users")

        if (cachedUsers) {
            return cachedUsers
        }

        const newUsers = ["Max", "User"]

        await myCache.setItem("users", newUsers, {ttl: 60})

        return newUsers
    }
}
```

# Logging
This project uses [debug](https://github.com/visionmedia/debug) to log useful caching information.
Set environment variable **DEBUG=node-ts-cache** to enable logging.

# Development & Testing

This project follows the monorepo architecture using [lerna](https://github.com/lerna/lerna).
To start development and run tests for all the packages, run:

```bash
git clone git@github.com:havsar/node-ts-cache.git
cd node-ts-cache
npm i
npm run bootstrap
npm run test
```

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