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

locky

v4.0.1

Published

User / resource locking system.

Downloads

12

Readme

locky

Node.js CI

Fast resource locking system based on redis.

Install

npm install locky

Usage

import redis from "redis";
import { createClient } from "locky";

// Create a new locky client.
const locky = createClient({ redis: () => redis.createClient() });

// Lock the resource 'article:12' with the locker 20.
await locky.lock("article:12", 20);

// Refresh the lock TTL of the resource 'article:12'.
await locky.refresh("article:12");

// Unlock the resource 'article:12.
await locky.unlock("article:12");

// Get the locker of the resource 'article:12'.
await locky.getLocker("article:12");

createClient(options)

Create a new locky client with some options.

redis

Type: import('redis').ClientOpts | (() => import('redis').RedisClient)

If you specify an object, the properties will be used to call redis.createClient method.

createClient({
  redis: {
    port: 6379,
    host: "127.0.0.1",
    connect_timeout: 200,
  },
});

If you specify a function, it will be called to create redis clients.

import redis from "redis";

createClient({
  redis: () => redis.createClient(),
});

ttl

Type: number

Define the expiration time of the lock in ms. Defaults to null (no expiration).

const locky = createClient({ ttl: 2000 });

prefix

Type: string, default: "locky:"

Define the prefix of every keys used by locky.

const locky = createClient({ prefix: "something:" });

locky.startExpirateWorker()

Start an expiration worker, it means locky will emit "expire" events.

locky.lock(options, [callback])

Lock a resource for a locker.

If the resource was already locked, you can't lock it but by passing force: true.

const locked = await locky.lock({
  resource: "article:23",
  locker: 20,
  force: false,
});
// `locked` is `true` if lock has been taken, `false` if not

resource

Type: string | number

Which resource would you like to lock.

locker

Type: string | number

Which locker should lock the resource, can by any string.

force

Type: boolean

Should we take a lock if it's already locked?

locky.refresh(resource, [callback])

Refresh the lock ttl of a resource, if the resource is not locked, do nothing.

// Refresh the resource "article:23".
locky.refresh('article:23').then(...);

locky.unlock(resource, [callback])

Unlock a resource, if the resource is not locked, do nothing.

// Unlock the resource "article:23".
locky.unlock('article:23').then(...);

locky.getLocker(resource, [callback])

Return the locker of a resource, if the resource is not locked, return null.

// Return the locker of the resource "article:23".
locky.getLocker('article:23').then(...);

Events

"lock"

Emitted when a resource is locked.

locky.on("lock", (resource, locker) => {
  /* ... */
});

"unlock"

Emitted when a resource is unlocked.

locky.on("unlock", (resource) => {
  /* ... */
});

"expire"

Emitted when the lock on a resource has expired.

locky.on("expire", (resource) => {
  /* ... */
});

License

MIT