npm.io
1.2.0 • Published 6 years ago

@patagoniantech/redis-helper

Licence
ISC
Version
1.2.0
Deps
2
Size
46 kB
Vulns
0
Weekly
0

redis-helper

Redis Helper.

Usage

Using RedisHelperConnection

import RedisHelperConnection from '@patagoniantech/redis-helper';

const REDIS_HOST = process.env.REDIS_HOST || '127.0.0.1';
const REDIS_PORT = Number(process.env.REDIS_PORT) || 6379;
const REDIS_CACHE_EXPIRE = 5; // in seconds

const connection = new RedisHelperConnection({
  config: {
    host: REDIS_HOST,
    port: REDIS_PORT,
  },
});

const redisHelper = connection.create('test', REDIS_CACHE_EXPIRE);

const a = await redisHelper.tryGet('a', 123);
console.log('Check if a=123', a);

const b = await redisHelper.tryGet('a', 321);
console.log('Check if b=123', b);

const d = await redisHelper.del('a');
console.log('deleted records:', 1);

// Close Redis connection
await connection.close();
Using IO Redis instance
import Redis from 'ioredis';
import { RedisHelper } from '@patagoniantech/redis-helper';

const REDIS_HOST = process.env.REDIS_HOST || '127.0.0.1';
const REDIS_PORT = Number(process.env.REDIS_PORT) || 6379;
const REDIS_CACHE_EXPIRE = 5; // in seconds

const connection = new Redis({
  config: {
    host: REDIS_HOST,
    port: REDIS_PORT,
  },
});

const redisHelper = new RedisHelper(connection, 'testPrefix', REDIS_CACHE_EXPIRE);

const a = await redisHelper.tryGet('a', 123);
console.log('Check if a=123', a);

const b = await redisHelper.tryGet('a', 321);
console.log('Check if b=123', b);

const d = await redisHelper.del('a');
console.log('deleted records:', 1);

// Close Redis connection
await connection.close();

Doc

See docs/index.html.

Keywords