Giter Site home page Giter Site logo

damonhu / snowflakeswift Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 2.0 107 KB

Snowflake, use Swift. Generate unique incremental ID 基于`Swift`的雪花算法的实现,采用传统算法,生成递增的唯一ID

Home Page: https://blog.hudongdong.com/swift/1213.html

License: MIT License

Swift 100.00%
snowflake swift generator id id-generator snowflake-twitter vapor-swift

snowflakeswift's Introduction

SnowflakeSwift

Snowflake, use Swift. Generate unique incremental ID

基于Swift的雪花算法的实现,采用传统算法,生成递增的唯一ID。如果遇到时钟回拨,生成的为nil,让业务层面处理即可。

雪花算法twitter开源的用于分布式环境下生成唯一ID的算法,原始版本snowflake使用Scala开源,基于时间戳递增的属性。作为数据库的主键比UUID有着更快的读写。各个变量长度可以根据业务进行改进,该库是基于最原生的实现。

使用

SnowflakeSwift.swift文件拖入项目即可

//创建
let general = SnowflakeSwift(IDCID: 4, machineID: 30)
//生成唯一ID
let id = general.nextID()

通过id也可以得出生成的时间、机房ID、机器ID

let time = general.time(id: id)
let idcID = general.IDC(id: id)
let machineID = general.machine(id: id)

snowflakeswift's People

Contributors

damonhu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

yibyli jmovxia

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.