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 🙏

© 2026 – Pkg Stats / Ryan Hefner

simple-redis-lock

v1.0.3

Published

Simple redis locks using promises and redis `SETNX`.

Readme

Redis Locks

Simple redis locks using promises and redis SETNX.

Install

npm install --save simple-redis-lock

Then require it

const simple_redis_lock = require('simple-redis-lock')

Simple Usage

// create a lock object with a lock name
const my_lock = simple_redis_lock('test_lock')

(async _ =>
{
  try {
    // acquire the lock
    await my_lock.acquire()
    console.log('Lock has been acquired. Doing stuff..')

    // release the lock
    await my_lock.release()
    console.log('Lock has been released. Done stuff!')
  } catch (e) {
    // this exception is either from the lock acquisition or release
    console.log('err', e)
  }
})()

// optionally hook into retries
my_lock.onRetry(() =>
{
  console.log(`Retrying... (${(+new Date - my_lock.begin_time) /1000} seconds elapsed)`)

  if ( +new Date - my_lock.begin_time > 2000 ) { // 2 seconds elapsed, abort
    // aborting the lock directly
    my_lock.abort()
  }
})

Available methods

LockObject.acquire()

Returns a promise that resolves when your lock has been acquired successfully, or rejects when the task is aborted.

Note: the acquire task will run forever by default, until aborted either with LockObject.abort() method or via ABORT_AFTER_MS option key.

LockObject.release()

Returns a promise that resolves when your lock key has been freed from redis. It would reject the promise if the key has not been deleted (see redis DEL)

LockObject.abort()

Allows you to abort the process directly and it will reject the LockObject.acquire() upon next acquisition attempt.

LockObject.onRetry(callback)

Hook into the retry process.

Configuration

You can pass the custom settings via the second argument passed to the object instantiation:

// create a lock object
let my_lock = simple_redis_lock('test_lock', {
  // options
  DELAY_MS: 50,
  ABORT_AFTER_MS: 5000, // 5 seconds
})

Here are the default settings:

// default configuration
const config = {
  REDIS_CONNECTION: null, // pass an array for the connection [ port, host ]
  NX_EXPIRES: 60 *60, // a default expiration lock keys (don't persist them in memory) defaults to 1 hour
  DELAY_MS: 200, // delay between retries
  KEY_PREFIX: 'lock.', // a prefix for the redis key
  ABORT_AFTER_MS: null, // optionally stop trying after X milliseconds elpased
}