Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@coolgk/cache

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@coolgk/cache

a redis wrapper

  • 1.0.9
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

@coolgk/cache

a javascript / typescript module

npm install @coolgk/cache

a redis wrapper

Examples

import { Cache } from '@coolgk/cache';
import { createClient } from 'redis';
// OR
// const { Cache } = require('@coolgk/cache');
// const { createClient } = require('redis');

const client = createClient({
    host: 'localhost',
    port: 12869,
    password: '----'
});

const cache = new Cache({
    redisClient: client
});

cache.set('abc', {a: 1}, 1).then(console.log); // 'OK'

cache.get('abc').then(console.log); // { a: 1 }

setTimeout(() => {
    cache.get('abc').then(console.log); // null
    client.quit();
}, 1500);

cache.getSetIfNull(
    'abc',
    () => Promise.resolve('data'),
    10
).then((v) => {
    console.log(v); // { a: 1 }
});

Promise.all([
    cache.set('x', 'val x'),
    cache.set('y', 'val y'),
    cache.set('z', 'val z')
]).then(
    () => Promise.all([
        cache.get('x').then(console.log), // val x
        cache.get('y').then(console.log), // val y
        cache.get('z').then(console.log) // val z
    ])
).then(
    () => Promise.all([
        cache.delete('x'),
        cache.delete('y'),
        cache.delete('z')
    ])
).then(
    () => Promise.all([
        cache.get('x').then(console.log), // null
        cache.get('y').then(console.log), // null
        cache.get('z').then(console.log) // null
    ])
);

Cache

Kind: global class

new Cache(options)

ParamTypeDescription
optionsobject
[options.redisClient]objectredis client from redis.createClient() redisClient needs to be passed in so the same connection can be used elsewhere and get closed outside this class

cache.set(name, value, [expiry]) ⇒ promise

Kind: instance method of Cache

ParamTypeDefaultDescription
namestringname of the variable
value*value is always JSON.stringify'ed
[expiry]number0expire time in seconds. 0 = never expire

cache.get(name) ⇒ promise

Kind: instance method of Cache
Returns: promise - - cached value

ParamTypeDescription
namestringname of the variable

cache.delete(name) ⇒ promise

Kind: instance method of Cache

ParamTypeDescription
namestring | Array.<string>name(s) of the variable

cache.getSetIfNull(name, callback, [expiry]) ⇒ promise

get the cached value, if not set, resolve "callback()" and save the value then return it

Kind: instance method of Cache
Returns: promise - - cached value

ParamTypeDefaultDescription
namestringname of the variable
callbackfunctiona callback function which returns a value or a promise
[expiry]number0expire time in seconds. 0 = never expire

cache.command(command, ...params) ⇒ promise

Kind: instance method of Cache

ParamTypeDescription
commandstringredis command to run
...paramsarrayparams for the command

Keywords

FAQs

Package last updated on 14 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc