# sfn-cache

> Simple Friendly Node.js Cache based on Redis.

Latest version **0.1.7** (published 2018-05-25) · MIT license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.7 |
| Published | 2018-05-25 |
| First published | 2017-11-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 14.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ayon Lee |
| Maintainers | hyurl |
| Keywords | sfn, cache |

## Links

- npm: https://www.npmjs.com/package/sfn-cache
- Repository: https://github.com/hyurl/sfn-cache
- Issues: https://github.com/Hyurl/sfn-cache/issues
- npm.io page: https://npm.io/package/sfn-cache

## Dependencies (3)

- [redis](https://npm.io/package/redis.md) ^2.8.0
- [@types/redis](https://npm.io/package/@types/redis.md) ^2.8.6
- [parse-redis-url](https://npm.io/package/parse-redis-url.md) 0.0.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

- 0.1.7 (latest) — 2018-05-25
- 0.1.6 — 2018-05-21
- 0.1.5 — 2018-05-05
- 0.1.4 — 2018-01-15
- 0.1.3 — 2017-12-08
- 0.1.2 — 2017-11-12
- 0.1.1 — 2017-11-12
- 0.1.0 — 2017-11-11

## README

# SFN-Cache

**Simple Friendly Node.js Cache.**

This package relies on Redis, so you must have a Redis server started first.

## Install

```sh
npm install sfn-cache --save
```

## Import

```javascript
const Cache = require("sfn-cache");
```

## Example

```javascript
(async() => {
    var cache = null;
    try {
        var cache = new Cache(),
            data = await cache.set("a", [1, 2, 3], 10000),
            _data = await cache.get("a");
        console.log("Initial data:", data, "\n");
        console.log("Data retrieved from cache:", _data);
    } catch (e) {
        console.log(e);
    }
    
    if(cache)
        cache.close();
})();
```

## API

- `new Cache(dsn?: string)` Creates a cache channel with a redis URL.
- `new Cache(options?: { [x: string]: any })`
    - `options` All available options for `redis.createClient()`.

- `cache.set(key: string, value: any, ttl?: number): Promise<any>` Stores or 
    updates a value.
    - `ttl` Time-To-Live (in milliseconds).

- `cache.get(key: string): Promise<any>` Retrieves a value by a given key.
- `cache.delete(key: string): Promise<void>` Deletes a key from the cache.
- `destroy(): Promise<void>` Clears the cache entirely, the cache will be 
    closed after calling this method.
- `close(): void` Closes the cache channel, if you don't close the channel, 
    the program will hang until the redis server closes the connection.
- `cache.connected` If the channel is open, it will be `true`.
- `cache.closed` Opposite to `connected`.

### new Cache()

```javascript
// Simplest way:
var cache = new Cache();

// Pass a redis URL:
var cache = new Cache("redis://127.0.0.1:6379");

// Pass an object:
var cache = new Cache({
    host: "127.0.0.1",
    port: 6379,
    password: "my-p@ssw0rd",
    database: 1,
    prefix: "my-cache:"
});

// Pass a redis client:
const redis = require("redis");
var client = redis.createClient();
var cache = new Cache(client);
```

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