Created
November 27, 2020 15:50
-
-
Save nartoan/3b9ca0cfdf66bbbb0ac8e743453bde18 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const redis = require("redis"); | |
| const { promisify } = require("util"); | |
| const store = redis.createClient({ | |
| port: process.env.REDIS_PORT, | |
| host: process.env.REDIS_HOST, | |
| }); | |
| store.on('connect', () => { | |
| console.log('Client connected to redis...') | |
| }) | |
| store.on('error', (err) => { | |
| console.log(err.message) | |
| }) | |
| const client = { | |
| store, | |
| getAsync: key => { | |
| const getAsync = promisify(store.get).bind(store); | |
| return getAsync(key); | |
| // .finally(console.log({ action: 'READ', key })); | |
| }, | |
| setAsync: (key, val) => { | |
| const getAsync = promisify(store.set).bind(store); | |
| return getAsync(key, val); | |
| // .finally(console.log({ action: 'SET', key, val })); | |
| }, | |
| hgetSync: (hash, field) => { | |
| const hgetAsync = promisify(store.hget).bind(store); | |
| return hgetAsync(hash, field).then(result => result && JSON.parse(result)); | |
| // .finally(console.log({ action: 'READ HASH', hash, field })); | |
| }, | |
| hsetSync: (hash, field, value) => { | |
| let valueStore = value; | |
| if (typeof value === "object") { | |
| valueStore = JSON.stringify(value); | |
| } | |
| const hsetAsync = promisify(store.hset).bind(store); | |
| return hsetAsync(hash, field, valueStore); | |
| // .finally(console.log({ action: 'SET HASH', hash, field, value })); | |
| }, | |
| hincrbySync: (hash, field, increment) => { | |
| const hincrbyAsync = promisify(store.hincrby).bind(store); | |
| return hincrbyAsync(hash, field, increment); | |
| // .finally(console.log({ action: 'HINCRBY HASH', hash, field, increment })); | |
| }, | |
| hgetalllSync: (hash) => { | |
| const hgetallAsync = promisify(store.hgetall).bind(store); | |
| return hgetallAsync(hash) | |
| // .finally(console.log({ action: 'READ HASH', hash })); | |
| }, | |
| } | |
| module.exports = client; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment