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

redlock-js

v1.1.2

Published

RedLock is a TypeScript library for acquiring and releasing locks using Redis, designed for use in Node.js applications.

Downloads

24

Readme

RedLock-JS

RedLock is a TypeScript library for acquiring and releasing locks using Redis, designed for use in Node.js applications.

Installation

Install the RedLock library using npm:

npm install redlock-js

Usage

To use RedLock in your Node.js application, follow these steps:

  1. Import the RedLock class:
import RedLock, { LockOptions } from 'redlock-js';
  1. Create an instance of RedLock with the required configuration:
const lockOptions: LockOptions = {
  redis: {
    host: 'localhost',
    port: 6379,
  },
  key: 'my-lock',
  interval: 100, // Optional, specify the retry interval in milliseconds
};

const redlock = new RedLock(lockOptions);
  1. Acquire and release locks as needed:
// Acquire a lock
await redlock.lock();

// Perform your critical section of code here

// Release the lock when done
await redlock.release();

Configuration Options

You can configure RedLock by providing options when creating an instance:

  • redis: Redis connection options.
  • cluster: If you're using a Redis cluster, provide cluster configuration.
  • key: The unique key for your lock.
  • interval (default: 100): Retry interval in milliseconds when attempting to acquire the lock.
  • expire (default: 300): Expiration time in seconds for the lock key. Setting an expiration time allows you to automatically release the lock after a certain duration, effectively locking a resource without needing an explicit release operation.

Additional Functionality

Automatic Cleanup

RedLock includes an automatic cleanup function that can be used to clean up leftover lock keys. You can use the clean method to scan and delete expired lock keys from the Redis database.

await redlock.clean();

By calling the clean method, you can ensure that expired lock keys are removed, preventing potential issues with stale locks.

Checking Lock Status

You can also check whether a lock is currently active using the isLocking method:

const lockingStatus = await redlock.isLocking();
console.log(`Is locking: ${lockingStatus}`);

The isLocking method returns a boolean indicating whether the lock is currently active or not.

Close the Redis Connection

To properly release resources and end the Redis connection, use the end method:

await redlock.end();

Calling the end method will gracefully close the connection to the Redis server. Additionally, when you call the release method, the lock will be released and the associated Redis connection will be closed.