npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redache

v1.1.2

Published

Easy redis cache library

Downloads

14

Readme

Redache

npm version Build Status codecov

Easy redis cache library.


Installation

Install Redache with your preferred package manager:

yarn add redache

# or

npm i --save redache

Then include it in your project:

# CommonJS way
const Redache = require('redache')

# ES6
import Redache from 'redache'

Configuration

The Redache constructor accepts an object containing the Redis configuration parameters.
This is an example of a minimal configuration, you can discover more about the params here: https://github.com/NodeRedis/node_redis#options-object-properties.

const cache = new Redache({
  host: '127.0.0.1',
  port: '6379',
  password: 'foobar',
  db: 0,
  tls: {} // Enable TLS, for example for DigitalOcean Redis
})

Usage

.set(key, value, ttl)

Store a value in Redis.

value can be as well an object or an array; in that case it will be JSON serialized.
ttl can be a human readable string (e.g. 2 days), a Date instance, a moment instance or a number (seconds).

// Store a value by human readable TTL
// It expires in 5 hours
await cache.set('key', 'value', '5 hours')

// Store a value by using a Date instance as TTL
// It expires the 1st Jan 2050
await cache.set('key', 'value', new Date(2050, 1, 1))

// Store a value by using a moment instance as TTL
// It expires in 1 month
await cache.set('key', 'value', moment().add(1, 'month'))

// Store a value by using a numeric TTL (seconds)
// It expires in 3600 seconds -> 1 hour
await cache.set('key', 'value', 3600)

.get(key, fallbackValue?, fallbackTtl?)

Retrieve a value from Redis by its key.

It will return null if not found, an object or array if the value was serialized or, if passed, the desired fallback value.

// Store for 5 hours
await cache.set('key', 'value', '5 hours')

// It return 'value'
const value = await cache.get('key')

// This will be encoded in Redis as '{"foo":"bar"}'
await cache.set('key', { foo: 'bar' }, '5 hours')

// It return { foo: 'bar' }
const value = await cache.get('key')

If you set fallbackValue along with fallbackTtl, if the given key is not found in Redis it will be created with the provided TTL and fallback and returned.

See the example below to better understand this.

// This returns null
const value = await cache.get('key')

// This returns 'this is my fallback'
const value = await cache.get('key', 'this is my fallback', '1 hour')

// This NOW returns 'this is my fallback', it has been set by the fallback above with a TTL of 3600 (1 hour)
const value = await cache.get('key')

The comparison below could also help you to understand it.

// This in one line
const value = await cache.get('key', 'this is my fallback', '1 hour')

// Is the same of
let value = await cache.get('key')
if (key === null) {
  value = 'this is my fallback'
  await cache.set('key', 'this is my fallback', '1 hour')
}

// Pretty simple, isn't it?

.forget(key)

Remove a stored value from Redis.

// Store a value for an hour
await cache.set('key', 'value', '1 hour')

// It returns 'value'
const value = await cache.get('key')

// Remove it
await cache.forget('key')

// It returns null
const isItRemoved = await cache.get('key')