# flexible-cache

> A customizable & flexible cache for your next app

Latest version **1.1.0** (published 2023-08-18) · ISC license · 0 weekly downloads

## Install

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

## 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.1.0 |
| Published | 2023-08-18 |
| First published | 2023-08-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Nathan TILLIER |
| Maintainers | nathanti |
| Keywords | cache, lru |

## Links

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

## Dependencies (1)

- [lru-cache](https://npm.io/package/lru-cache.md) ^10.0.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

- 1.1.0 (latest) — 2023-08-18
- 1.0.2 — 2023-08-18
- 1.0.1 — 2023-08-18
- 1.0.0 — 2023-08-18

## README

# Flexible Cache

A flexible LRU cache made with the excellent [lru-cache](https://github.com/isaacs/node-lru-cache) package.


This package presents an alternative caching methodology that simplifies the caching process. It facilitates the storage of values using primary and secondary keys, eliminating the need for explicit key specification.
The objective is to is to simplify the complexities of key management, enabling a focus on constructing applications.

### Key Features:

- **Effortless Storage**:  You no longer need to manage and pass keys when storing values. The library handles the caching process based on predefined rules.

- **Primary and Secondary Keys**: Store values based on primary keys for efficient retrieval. Additionally, you can use secondary keys, in order to have a flexible caching strategy.

- **Automatic Management**: The use of ``The library efficiently manages cache eviction based on usage patterns, ensuring that valuable memory resources are optimized.

### Install
```
npm i flexible-cache
```

### Usage:

```javascript
const FlexibleCache = require('flexible-cache');

const cache = new FlexibleCache({

  // you choose a primary key for the cache
  primaryKey: 'id',

  // you can add as many secondary keys as you want
  secondaryKeys: ['name'],

  // you need to set a maximum size for the cache (you can have more options for the cache here: https://github.com/isaacs/node-lru-cache)
  max: 500,

  updateAgeOnGet: true,
});

// adding a value in the cache is as simple as that :)
cache.set({
  id: '123',
  name: 'john'
});

// { id: '123', name: 'john' }
cache.get('123');
// or
cache.get('john', 'name');

// delete the entry in the cache
cache.delete('123');
```

### Options
**primaryKey**: the primary key for the cache
**secondaryKeys**: the secondary keys
**...**: All the remaining options are for the `lru-cache`, and are listed [here](https://github.com/isaacs/node-lru-cache).

> **Note**
> The `dispose` option of the LRU cache can't be used for now

### API

**.set(value)**
Add a value to the cache

**.get(key, secondaryKey?)**
Retrieves a value. By default, the provided key is the primaryKey, but you can choose another key by providing a secondaryKey listed in the options.

**.delete(key, secondaryKey?)**
Removes a value from the cache.

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