mycommon-utils 0.2.1

Common utilities library for database operations, Redis caching and system utilities
Documentation
use std::sync::{Mutex, OnceLock};
use uuid::Uuid;

pub struct GeneratorId;

static SNOWFLAKE_BUCKET: OnceLock<Mutex<snowflake::SnowflakeIdBucket>> = OnceLock::new();

fn get_snowflake_bucket() -> &'static Mutex<snowflake::SnowflakeIdBucket> {
    SNOWFLAKE_BUCKET.get_or_init(|| Mutex::new(snowflake::SnowflakeIdBucket::new(1, 1)))
}

impl GeneratorId {
    pub fn uuid_v4() -> String {
        Uuid::new_v4().as_simple().to_string()
    }

    //生成一个12位的唯一字符串
    pub fn uuid_v4_12() -> String {
        let uuid = Uuid::new_v4().to_string().replace("-","");
        uuid[0..12].to_string()
    }
    //生成雪花id
    pub fn snowflake_id() -> i64 {
        let mut bucket = get_snowflake_bucket().lock().unwrap();
        bucket.get_id()
    }
}