# tiered-cache

> Simple tiered cache.

Latest version **0.2.1** (published 2024-09-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install tiered-cache
pnpm add tiered-cache
yarn add tiered-cache
bun add tiered-cache
```

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2024-09-17 |
| First published | 2023-11-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Surya Oktafendri |
| Maintainers | f2face |
| Keywords | cache, cache management |

## Links

- npm: https://www.npmjs.com/package/tiered-cache
- npm.io page: https://npm.io/package/tiered-cache

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

- 0.2.1 (latest) — 2024-09-17
- 0.2.0 — 2023-11-15
- 0.1.0 — 2023-11-01

## README

# Tiered-Cache

## Example

> You will need `ioredis` package to use `adapters.RedisCache`.

```javascript
import { TieredCache, adapters } from 'tiered-cache';
import { Redis } from 'ioredis';

(async () => {
    const redisInstance = new Redis('redis://localhost:6379');
    const redisCacheKey = 'test:cache1';
    const redisCacheTtl = 60;
    const redisCacheResetTtl = true; // This will reset cache TTL every time the cached data is retrieved. (Default: false)

    const cache = new TieredCache()
        .appendTier(
            // 1st tier (lower-tier)
            new adapters.RedisCache({
                redis: redisInstance,
                cacheKey: redisCacheKey,
                cacheTtl: redisCacheTtl,
                resetTtl: redisCacheResetTtl,
            })
        )
        .appendTier(new adapters.FileCache('./cache-file.txt')) // 2nd tier (upper-tier)
        .setOrigin(() => {
            // Primary data source
            return Buffer.from('Lorem ipsum dolor sit amet');
        });

    const data = await cache.get();

    console.log(data?.toString());

    await redisInstance.quit();
})();
```

## Custom adapter

This library includes built-in `FileCache` and `RedisCache` adapters.

If you need to work with a different type of cache, you can create your own custom adapter by implementing the `Cache` interface with `get` and `set` methods.

```javascript
const cache = new TieredCache()
    .appendTier({
        // Custom adapter
        get: async () => {
            // Retrieve data from the database.
        },
        set: async (data) => {
            // Store data into the database.
        },
    })
    .setOrigin(() => {
        // Primary data source
        return 'Lorem ipsum dolor sit amet';
    });

const data = await cache.get();

console.log(data);
```

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