# redis-toolbox

> Redis utilities that allows developers to utilize Redis for Session Management, Job Queue processing and so on.

Latest version **1.0.1** (published 2020-04-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install redis-toolbox
pnpm add redis-toolbox
yarn add redis-toolbox
bun add redis-toolbox
```

## 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.1 |
| Published | 2020-04-06 |
| First published | 2020-04-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 19 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| Author | Adonis Lee Villamor |
| Maintainers | adonisv79 |
| Keywords | redis, session, queue, worker |

## Links

- npm: https://www.npmjs.com/package/redis-toolbox
- Repository: https://github.com/adonisv79/redis-utils
- Homepage: https://github.com/adonisv79/redis-utils#readme
- Issues: https://github.com/adonisv79/redis-utils/issues
- npm.io page: https://npm.io/package/redis-toolbox

## Dependencies (3)

- [uuid](https://npm.io/package/uuid.md) 7.0.3
- [redis](https://npm.io/package/redis.md) 3.0.2
- [ioredis](https://npm.io/package/ioredis.md) 4.16.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.0.1 (latest) — 2020-04-06
- 1.0.0 — 2020-04-06
- 0.4.0 — 2020-04-06
- 0.3.0 — 2020-04-06
- 0.2.0 — 2020-04-06
- 0.1.0 — 2020-04-06

## README

# redis-toolbox
![Redis toolbox banner](https://adonisv79.github.io/redis-toolbox/images/banner.png)

Redis utilities that allows developers to utilize Redis for Session Management, Job Queue processing and so on.
## Installation
```
npm i redis-toolbox --save
```

***
# Session Tools
The session tools provided here allows you to turn your redis host into a session manager for anything. It is designed to be agnostic to any specific product type and are all promise based which can be used for Async/Await calls

## Sample Setup
```
import {
  RedisSessionManager, RedisSessionOptions, onRedisSessionErrorCallback, RedisSessionObject,
} from 'redis-toolbox';

const redisOptions: RedisSessionOptions = {
  host: process.env.REDIS_HOST,
  port: parseInt(process.env.REDIS_PORT || '80', 10),
  db: process.env.REDIS_DB ? parseInt(process.env.REDIS_DB, 10) : undefined,
  password: process.env.REDIS_PASS,
  sessionMaxTTL: process.env.USER_SESSION_MAX_TTL ? parseInt(process.env.USER_SESSION_MAX_TTL, 10) : 21600,
  sessionRefreshTTL: true,
  sessionInactiveTTL: process.env.USER_SESSION_IDLE_TTL ? parseInt(process.env.USER_SESSION_IDLE_TTL, 10) : 1800,
};

const onSessionError: onRedisSessionErrorCallback = async (err: Error): Promise<boolean> => {
  // do whatever async tasks like send metrics, alerts, etc.
  console.log(`Error on session: ${err.message}`);
  return false;
};

const session = new RedisSessionManager(redisOptions, onSessionError);
```
### Session related imports from the toolbox
On the sample above, we first imported the major components of a redis session.
* RedisSessionManager - The class that encapsulates the session management
* RedisSessionOptions - An object that extends the RedisOptions (ioredis)
* onRedisSessionErrorCallback - Defines a function that will be called whenever an error occurs in the session
* RedisSessionObject - Defines the data that gets stored for the session

### RedisSessionOptions 
Next we had defined the redis options. The rest of the configurations are from ioredis (host, port, etc.) what is added are the 3 session behavior properties
* sessionMaxTTL (integer)(default 6 hours) - The maximum time (in seconds) a redis session instance lives (TTL). once the session reaches this, its gone for good
* sessionRefreshTTL (boolean)(default false) - Defines if the session expires by being idle. This allows you to create long sessionMaxTTL but enforce an auto session kill when the user shows no activity for lets say 5-15 minutes
* sessionInactiveTTL (integer)(default 30 minutes) - if sessionRefreshTTL is true, this is applied as the max idle time a user can have before the session times out.

### onRedisSessionErrorCallback 
After that we need to implement the error callback defined by onRedisSessionErrorCallback . This allows you the chance to perform anything necessary before the module throws an Error. You can send alerts, metrics etc. on this function. this expects a return value however of boolean. Returning true acknowledges that you have handled the error yourself and telling the module to "do not bother throwing the error". sending false makes the module proceed to throw the error.

### RedisSessionManager instance
Finally, we create a new session instance from RedisSessionManager. It requires only 2 parameters which we did in the 2nd and 3rd step. The RedisSessionOptions object and the Error callback.

***

We can now start utilizing the session functions
```
const newsession: RedisSessionObject = await session.createSession();
const samesession: RedisSessionObject = await session.retrieveSession(newsession.sessionId);
const delta = { name: 'Adonis Lee Villamor', email: 'adonisv79@gmail.com' }
const isUpdated = await session.updateSession(samesession.sessionId, delta as any); //isUpdated will be true if success
const isDestroyed = await session.destroySession(samesession.sessionID);
```

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