# nestjs-cacheable

> a simple service level cache module in NestJs

Latest version **1.0.0** (published 2024-01-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-cacheable
pnpm add nestjs-cacheable
yarn add nestjs-cacheable
bun add nestjs-cacheable
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2024-01-25 |
| First published | 2022-04-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 14.5 KB |
| Known vulnerabilities | 0 (+3 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 18 |
| Author | caidesen |
| Maintainers | caidesen |

## Links

- npm: https://www.npmjs.com/package/nestjs-cacheable
- Repository: https://github.com/caidesen/nestjs-cacheable
- Homepage: https://github.com/caidesen/nestjs-cacheable#readme
- Issues: https://github.com/caidesen/nestjs-cacheable/issues
- npm.io page: https://npm.io/package/nestjs-cacheable

## Dependencies (5)

- [@nestjs/core](https://npm.io/package/@nestjs/core.md) ^10.3.0
- [cache-manager](https://npm.io/package/cache-manager.md) ^5.4.0
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^10.3.0
- [serialize-javascript](https://npm.io/package/serialize-javascript.md) ^6.0.0
- [@nestjs/cache-manager](https://npm.io/package/@nestjs/cache-manager.md) ^2.2.0

## Recent versions

- 1.0.0 (latest) — 2024-01-25
- 0.2.4 — 2023-02-13
- 0.2.3 — 2023-02-13
- 0.2.2 — 2022-07-30
- 0.2.1 — 2022-04-11
- 0.2.0 — 2022-04-10
- 0.1.0 — 2022-04-08

## README

# nestjs-cacheable

## Description

a simple service level cache module in NestJs.

Caching is a great technique for your app's performance. NestJs provides a CacheModule in @nestjs/common, it only
support controller level cache. In practice, often happens services inner function call, we also need service level
cache.

Before that, you can be handled manually inside a function in service, that is great, but tedious.

This package use decorator handle service level cache.

It all becomes simple!

## Installation

```bash
$ npm install nestjs-cacheable
$ yarn add nestjs-cacheable
```

## How to use

This package based on CacheModule, you mush import CacheModule in your application.
All cache action based on CacheModule and cache-manager, you can use any cache store, as long as them supported.

[CacheModule](https://docs.nestjs.com/techniques/caching)

```typescript
import { Module } from "@nestjs/common";
import { CacheModule } from '@nestjs/cache-manager';
@Module({
  imports: [
    CacheableModule.register(),
    CacheModule.register({ isGlobal: true }),
  ],
})
export class AppModule {}
```

```typescript
@Injectable()
export class UserService {
  // Cacheable will cache function return value
  @Cacheable({
    key: (id: number) => `username-${id}`,
    namespace: 'user',
    ttl: 5
  })
  async getUserName(id: number) {
    const res = await this.db.query({
      // ...
    });
    return res;
  }
  
  // CacheEvict will clear the cache at the end of function run.
  @CacheEvict({
    key: (id: number) => `username-${id}`,
    namespace: 'user',
  })
  async deleteUser(id: number) {
    await this.db.delete({
      // ...
    })
  }
}
```

## License

[MIT licensed](LICENSE).

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