https://github.com/mamantoha/snowflake
Snowflake ID generator implementation in Crystal programming language
https://github.com/mamantoha/snowflake
crystal snowflake
Last synced: 5 months ago
JSON representation
Snowflake ID generator implementation in Crystal programming language
- Host: GitHub
- URL: https://github.com/mamantoha/snowflake
- Owner: mamantoha
- License: mit
- Created: 2024-02-25T12:50:50.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-02T09:47:25.000Z (over 1 year ago)
- Last Synced: 2025-04-04T09:34:36.988Z (12 months ago)
- Topics: crystal, snowflake
- Language: Crystal
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Snowflake
[](https://github.com/mamantoha/snowflake/actions/workflows/crystal.yml)
[](https://github.com/mamantoha/snowflake/releases)
[](https://github.com/mamantoha/snowflake/blob/main/LICENSE)
Snowflake ID generator implementation in Crystal programming language, which is designed to generate unique,
time-based identifiers using a 64-bit structure. These identifiers consist of a timestamp component,
a machine or process ID, and a sequence number to ensure uniqueness within the same millisecond.
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
snowflake:
github: mamantoha/snowflake
```
2. Run `shards install`
## Usage
```crystal
require "snowflake"
machine_id = 1_u64 # Assign a unique machine/process ID
generator = Snowflake.new(machine_id)
snowflake_id = generator.generate_id
puts snowflake_id
# To convert a Snowflake ID back to a UTC timestamp
utc_time = Snowflake.id_to_utc(snowflake_id)
puts utc_time
```
## Contributing
1. Fork it ()
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
## Contributors
- [Anton Maminov](https://github.com/your-github-user) - creator and maintainer