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

ac-redislock

v4.0.0

Published

This tool can be used to lock operations (e.g. make sure workers will fetch a job only once)

Downloads

162

Readme

AC RedisLock

This tool can be used to lock operations (e.g. make sure workers will fetch a job only once)

example workflow

Breaking changes version 4

Version 4 no longer supports callbacks! Please use async/await instead.

Breaking changes version 2

RedisLock will now be only initialized once - if parameters redis, logger, logLevel, suppressMismatch are already set (by first init call) they are re-used and no longer overwritten.

If you want the old behaviour, init again with parameter reInit=true.

Requirements

Requires an existing Redis connection.

Usage

Init function requires a valid redis instance.

  • redis - INSTANCE required Redis instance
  • logger - OPTIONAL INSTANCE logger instance (e.g. Winston)
  • logLevel - OPTIONAL STRING logLevel for the Redis operation
  • suppressMismatch - OPTIONAL BOOL - if true, release mismatches are not logged
  • reInit - OPTIONAL BOOL - if true, you can re-init ac-redisLock, otherwise existing values (from first init) will be used (the latter is the default behaviour)

Those options will init ac-redislock once. Calling it again will not have any effect as long as you do not use reInit=true.

await redisLock.lockKey(params)

LockKey has the following parameters

  • redisKey - STRING required name for the key
  • expires - OPTIONAL INT seconds after the lock is released, defaults to 10 seconds
  • value - OPTIONAL STRING value for this redisKey. Use it to compare/secure lock values

Lock key throws an error 423 (if lock is already active) or otherwise returns the lock value, which can be used in releaseLock function.

await redisLock.releaseLock(params)

ReleaseKey has the following parameters

  • redisKey - STRING required name for the key
  • value - if set will be compared with the redisKey value before releasing. If not matching, the function will return an error message
  • suppressMismatch - if true, no warning will be logged if the value does not match the stored one (see "multiple processes" section)

Examples

const redisLock = require('ac-redislock')

// create a Redis instance (using io-redis)
const app.redisInstance = new Redis(options)

await redisLock.init({
  redis: app.redisInstance 
})

const params = {
  redisKey: 'someMeaningfulKey',
}
try {
   // value for this lock -> set as params.value for releaseLock
  const value = await redisLock.lockKey(params)
}
catch(err) {
  // err can be 423 -> key is locked
}

// redisKey is deleted and lock released
await redisLock.releaseKey(params)

Multiple Processes

If you use this package on multiple instances and want to make sure only of those instances processes a job we recommend to set suppressMismatch in order to avoid error messages if a process tries to release a key even though it did not lock it.

Links

License

MIT License Copyright © 2009-present, AdmiralCloud, AdmiralCloud AG, Mark Poepping