Skip to content

Instantly share code, notes, and snippets.

@nartoan
Created November 27, 2020 15:50
Show Gist options
  • Select an option

  • Save nartoan/3b9ca0cfdf66bbbb0ac8e743453bde18 to your computer and use it in GitHub Desktop.

Select an option

Save nartoan/3b9ca0cfdf66bbbb0ac8e743453bde18 to your computer and use it in GitHub Desktop.

Revisions

  1. nartoan created this gist Nov 27, 2020.
    56 changes: 56 additions & 0 deletions redis
    Original 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;