redis-json
Advanced tools
Comparing version 2.1.0 to 2.2.2
@@ -0,1 +1,7 @@ | ||
# v2.3.0 | ||
* Added `clearAll` method, which clears all the cached Json | ||
# v2.2.0 | ||
* Support for redis prefix keys | ||
# v2.1.0 | ||
@@ -2,0 +8,0 @@ * Support for key expiry |
43
index.js
@@ -1,3 +0,3 @@ | ||
const flatten = require("flat"); | ||
const unflatten = require("flat").unflatten; | ||
const flatten = require('flat') | ||
const unflatten = flatten.unflatten | ||
@@ -11,6 +11,11 @@ class JSONCache { | ||
*/ | ||
constructor(redisClient) { | ||
constructor(redisClient, { prefix = 'jc:' } = {}) { | ||
this.redisClient = redisClient; | ||
this.prefix = prefix | ||
} | ||
getKey(key) { | ||
return `${this.prefix}${key}` | ||
} | ||
/** | ||
@@ -29,10 +34,6 @@ * Flattens the given json object and | ||
const flattened = flatten(obj); | ||
// Custom property to check | ||
// if the hashset has expired | ||
flattened._present = 1; | ||
await this.redisClient.hmset.call(this.redisClient, key, flattened); | ||
await this.redisClient.hmset.call(this.redisClient, this.getKey(key), flattened); | ||
if (options.expire) | ||
await this.redisClient.expire.call(this.redisClient, key, options.expire); | ||
await this.redisClient.expire.call(this.redisClient, this.getKey(key), options.expire); | ||
} | ||
@@ -46,3 +47,3 @@ | ||
* | ||
* @returns {Object} | ||
* @returns {Promise<Object>} | ||
*/ | ||
@@ -52,8 +53,5 @@ async get(key) { | ||
this.redisClient, | ||
key | ||
this.getKey(key) | ||
); | ||
// Remove the custom property | ||
delete flattened._present; | ||
return Object.keys(flattened).length ? unflatten(flattened) : undefined; | ||
@@ -69,7 +67,18 @@ } | ||
async rewrite(key, obj) { | ||
await this.redisClient.del.call(this.redisClient, key); | ||
return this.set(key, obj); | ||
await this.redisClient.del.call(this.redisClient, this.getKey(key)); | ||
await this.set(key, obj); | ||
} | ||
/** | ||
* Removes/deletes all the keys in the JSON Cache, | ||
* having the prefix. | ||
*/ | ||
async clearAll() { | ||
const keys = await this.redisClient.keys.call(this.redisClient, `${this.prefix}*`) | ||
// Multi command for efficiently all the keys at once | ||
await this.redisClient.multi(keys.map(k => ['del', k])).exec() | ||
} | ||
} | ||
module.exports = JSONCache; |
{ | ||
"name": "redis-json", | ||
"version": "2.1.0", | ||
"version": "2.2.2", | ||
"description": "A wrapper library to store JSON Objects in redis-hashsets and retrieve it back as JSON objects", | ||
@@ -15,3 +15,3 @@ "main": "index.js", | ||
"status": "git status", | ||
"prettify": "prettier --write index.js" | ||
"lint:fix": "npm run lint -- --fix" | ||
}, | ||
@@ -35,3 +35,3 @@ "repository": { | ||
"precommit": [ | ||
"prettify", | ||
"lint:fix", | ||
"lint", | ||
@@ -38,0 +38,0 @@ "coverage", |
# redis-json [![Build Status](https://travis-ci.com/AkashBabu/redis-json.svg?branch=master)](https://travis-ci.com/AkashBabu/redis-json) | ||
Nodejs library to store/retreive JSON Objects in RedisDB | ||
### Description | ||
## Description | ||
Every time `set` is called JSON object is flattened(embeded objects are converted to path keys) and then stored in Redis just like a normal hashset, on `get` the hashset is unflattened and converted back to the original JSON object. | ||
Under the hood it uses [flat](https://www.npmjs.com/package/flat) library for flattening and unflattening JSON objects | ||
### Installation | ||
## Installation | ||
> npm i redis-json -D | ||
### Usage | ||
## Usage | ||
@@ -20,3 +20,3 @@ ```js | ||
const jsonCache = new JSONCache(redis); | ||
const jsonCache = new JSONCache(redis, {prefix: 'cache:'}); | ||
@@ -52,4 +52,13 @@ const user = { | ||
### API | ||
## API | ||
### Constructor | ||
**JSONCache(redisClient, options)** | ||
*redisClient*: RedisClient instance(Preferred ioredis - cient). It support any redisClient instance that has `keys, multi, set, get & del` methods implemented | ||
*options.prefix*: Prefix for redis keys. Defaults to `jc:` (jsonCache) | ||
### Methods | ||
**set(key, jsobObject, options): \<Promise>** | ||
@@ -71,3 +80,3 @@ | ||
**~~resave~~ rewrite(key, jsonObj)** | ||
**~~resave~~ rewrite(key, jsonObj): \<Promise>** | ||
@@ -79,2 +88,7 @@ *key*: The redis key that whose value needs to be replaced with the new one. | ||
**clearAll(): \<Promise>** | ||
Clears/removes all the keys with the prefix from redis using `multi` command. | ||
Useful when trying to refresh the entire cache. | ||
## Mocha & Chai (Testing) | ||
@@ -81,0 +95,0 @@ > npm test |
7949
69
102