├── README.md ├── clib ├── makefile └── uuid.c └── lib └── resty └── uuid.lua /README.md: -------------------------------------------------------------------------------- 1 | 调用系统的UUID模块生成的由32位16进制(0-f)数组成的的串,本模块进一步压缩为62进制。 2 | 3 | 生成8位UUID 4 | local resty_uuid = require "resty.uuid" 5 | 6 | local id = resty_uuid:gen8() --output 3uNy6ZzI 7 | 8 | 9 | 生成20为UUID 10 | local resty_uuid = require "resty.uuid" 11 | 12 | local id = resty_uuid:gen20() -2qYArunu73bABy2RqY3M 13 | 14 | 正如你所想,生成的UUID越长,理论冲突率就越小,请根据业务需要自行斟酌。 15 | 基本思想为把系统生成的16字节(128bit)的UUID转换为62进制(a-zA-Z0-9),同时根据业务需要进行截断。 16 | 17 | -------------------------------------------------------------------------------- /clib/makefile: -------------------------------------------------------------------------------- 1 | CC := gcc 2 | LD := ld 3 | CFLAGS := -fPIC -fno-stack-protector 4 | LIB := -luuid 5 | LDFLAGS := -shared 6 | SOURCE := $(wildcard *.c) 7 | OBJS := $(patsubst %.c,%.o,$(SOURCE)) 8 | TARGET_LIB := libuuidx.so 9 | 10 | all:$(OBJS) 11 | @echo $(OBJS) 12 | $(LD) $(LDFLAGS) $(LIB) -o $(TARGET_LIB) $(OBJS) 13 | @rm *.o -rf 14 | 15 | %.o:%.c 16 | @echo Compiling $< ... 17 | @$(CC) -c $(CFLAGS) $< -o $*.o 18 | 19 | .PHONY: clean 20 | 21 | clean: 22 | rm *.so *.o -rf 23 | -------------------------------------------------------------------------------- /clib/uuid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static char chars[] = { 7 | 'a','b','c','d','e','f','g','h', 8 | 'i','j','k','l','m','n','o','p', 9 | 'q','r','s','t','u','v','w','x', 10 | 'y','z','0','1','2','3','4','5', 11 | '6','7','8','9','A','B','C','D', 12 | 'E','F','G','H','I','J','K','L', 13 | 'M','N','O','P','Q','R','S','T', 14 | 'U','V','W','X','Y','Z' 15 | }; 16 | 17 | void uuid(char *result, int len) 18 | { 19 | unsigned char uuid[16]; 20 | char output[24]; 21 | const char *p = (const char*)uuid; 22 | 23 | uuid_generate(uuid); 24 | memset(output, 0, sizeof(output)); 25 | 26 | int i, j; 27 | for (j = 0; j < 2; j++) 28 | { 29 | unsigned long long v = *(unsigned long long*)(p + j*8); 30 | int begin = j * 10; 31 | int end = begin + 10; 32 | 33 | for (i = begin; i < end; i++) 34 | { 35 | int idx = 0X3D & v; 36 | output[i] = chars[idx]; 37 | v = v >> 6; 38 | } 39 | } 40 | //printf("%s\n", output); 41 | len = (len > sizeof(output)) ? sizeof(output) : len; 42 | memcpy(result, output, len); 43 | } 44 | 45 | void uuid8(char *result) 46 | { 47 | uuid(result, 8); 48 | result[8] = '\0'; 49 | } 50 | 51 | void uuid20(char *result) 52 | { 53 | uuid(result, 20); 54 | result[20] = '\0'; 55 | } 56 | 57 | /* 58 | void uuid24(char *result) 59 | { 60 | unsigned char uuid[16]; 61 | char output[28]; 62 | const char *p = (const char*)uuid; 63 | 64 | uuid_generate(uuid); 65 | memset(output, 0, sizeof(output)); 66 | 67 | int i, j; 68 | for (j = 0; j < 4; j++) 69 | { 70 | unsigned v = *(unsigned*)(p + j*4); 71 | int begin = j * 6; 72 | int end = begin + 6; 73 | 74 | for (i = begin; i < end; i++) 75 | { 76 | int idx = 0X3D & v; 77 | output[i] = chars[idx]; 78 | v = v >> 5; // <----it is different here 79 | } 80 | } 81 | 82 | memcpy(result, output, 24); 83 | } 84 | 85 | int main(int argc, char ** argv) 86 | { 87 | char out[32] = {'\0'}; 88 | 89 | uuid24(out); 90 | printf("%s\n", out); 91 | 92 | return 0; 93 | } 94 | */ 95 | -------------------------------------------------------------------------------- /lib/resty/uuid.lua: -------------------------------------------------------------------------------- 1 | module("resty.uuid", package.seeall) 2 | 3 | _VERSION = '0.01' 4 | 5 | local ffi = require "ffi" 6 | local ffi_new = ffi.new 7 | local ffi_str = ffi.string 8 | 9 | ffi.cdef[[ 10 | void uuid8(char *out); 11 | void uuid20(char *out); 12 | ]] 13 | 14 | local libuuid = ffi.load("libuuidx") 15 | 16 | function gen8() 17 | if libuuid then 18 | local result = ffi_new("char[9]") 19 | libuuid.uuid8(result) 20 | return ffi_str(result) 21 | end 22 | end 23 | 24 | function gen20() 25 | if libuuid then 26 | local result = ffi_new("char[21]") 27 | libuuid.uuid20(result) 28 | return ffi_str(result) 29 | end 30 | end 31 | 32 | -- to prevent use of casual module global variables 33 | getmetatable(resty.uuid).__newindex = function (table, key, val) 34 | error('attempt to write to undeclared variable "' .. key .. '": ' 35 | .. debug.traceback()) 36 | end 37 | --------------------------------------------------------------------------------