Created
November 27, 2020 15:50
-
-
Save nartoan/3b9ca0cfdf66bbbb0ac8e743453bde18 to your computer and use it in GitHub Desktop.
Revisions
-
nartoan created this gist
Nov 27, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ 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;