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

redis-snowflake-id

v1.1.2

Published

Node.js implementation of Twitter Snowflake ID Generator, base on redis.

Downloads

7

Readme

redis-snowflake-id

Node.js 版本的 Twitter 雪花算法(Snowflake) 发号器。基于 redis 存储发号器自增序号

安装

npm install redis-snowflake-id -S

yarn add redis-snowflake-id

使用

let options = {
  // redis 配置,详见 https://www.npmjs.com/package/redis
  redis: {
    host: '127.0.0.1',
  },
  // 世纪,用于减少生成的id数字大小,单位:毫秒,如:1300000000000
  epoch: 0,
  // redis 的hash键名
  hash_key: 'REDIS_SNOWFLAKE_ID',
};

// 创建发号器实例
let idgen = new RedisSnowflakeId(options);

// 生成id
let id = await idgen.next();
console.log('id', id);

// 根据id解析生成的时间戳(毫秒)
let time = await idgen.parse(id);
console.log('time', time);

// 生成不同业务类型的id,可选值:0~255,默认:0
let machineId = 3;
let id2 = await idgen.next(machineId);
console.log('id2', id2);

原理

雪花算法(snowflake)是将时间戳、业务id、自增序列分别转为二进制,然后拼接到一起,再转回10进制,得到唯一id。

本项目根据实际情况将时间戳补全到41位,业务id补全到8位,自增序列补全到14位,如果业务id不够用,可自行调整

0 00000000000000000000000000000000000000000 00000000  00000000000000
0 + 41位时间戳 + 8位业务id + 14位自增序列