# simpcache

> ![simpcache](https://socialify.git.ci/bayungrh/simpcache/image?description=1&descriptionEditable=A%20simple%20in-memory%20cache%20library%20for%20NodeJS&font=KoHo&forks=1&language=1&name=1&owner=1&pattern=Charlie%20Brown&pulls=1&stargazers=1&theme=Dark)

Latest version **1.0.4** (published 2021-12-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install simpcache
pnpm add simpcache
yarn add simpcache
bun add simpcache
```

## 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.4 |
| Published | 2021-12-30 |
| First published | 2021-12-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 10 |
| Dependencies | 0 |
| Unpacked size | 10.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | bayungrh |
| Maintainers | bayungrh |
| Keywords | cache, caching, nodecache, simple, memory, in-memory |

## Links

- npm: https://www.npmjs.com/package/simpcache
- Repository: https://github.com/bayungrh/simpcache
- Issues: https://github.com/bayungrh/simpcache/issues
- npm.io page: https://npm.io/package/simpcache

## 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.0.4 (latest) — 2021-12-30
- 1.0.3 — 2021-12-30
- 1.0.2 — 2021-12-29
- 1.0.0 — 2021-12-29

## README

![simpcache](https://socialify.git.ci/bayungrh/simpcache/image?description=1&descriptionEditable=A%20simple%20in-memory%20cache%20library%20for%20NodeJS&font=KoHo&forks=1&language=1&name=1&owner=1&pattern=Charlie%20Brown&pulls=1&stargazers=1&theme=Dark)

# ⚡️ simpcache

A simple in-memory cache library for NodeJS

# Installation
```bash
npm install simpcache --save
```

# Example
## Initialize
without options
```javascript
const cache = require('simpcache')();
```
with options
```javascript
const cache = require('simpcache')({
  persistence: true
});
```

### Options
- `persistence`: (*default*: `false`) If enabled, all cache will be stored on disk.
- `db`: (*default*: `cache.json`) if `persistence` is enabled cache will be stored on `cache.json` file.

## Example Code
```javascript
const cache = require('simpcache')({
  persistence: false // set `false` to use in-memory cache
});

// without expire time
cache.set('foo', 'bar');
console.log('foo', cache.get('foo'));
// @returns 'bar'

// with expire time 10sec, time in ms
cache.set('foo2', 'bar2', 10000);
console.log('foo2', cache.get('foo2'));
// @returns 'bar2'

// with timeout callback
cache.set('foo3', 'bar3', 10000, function (key, value) {
  console.log('foo3 deleted from cache');
});
// @returns 'foo3 deleted from cache' after cache expired.

// check cache exists
cache.has('foo');
// @returns true

// delete cache key
cache.del('foo');

// clear all cache
cache.flushAll();

// get keys from the cache
console.log('keys', cache.keys());
// @returns ['foo', 'foo2']
```

## Methods
- **set(key, value, expire, timeoutCallback)** `void`, set a new entry and value, and you can set the expiration time.
- **get(key)** `string` or `null`, get the entry's value from a given key.
- **has(key)** `boolean`, check key exists from cache.
- **del(key)** `void`, remove a given key from the cache.
- **keys()** `array`, returns the keys from the cache.
- **flushAll()** `void`, remove all data from cache.
- **ttl(key)** `number`, returns the remaining time to live of a key that has a timeout.

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