由于要用到固定長度的隨機字符串。
首先是一段PHP代碼:
- $str_md5=md5(uniqid());
- $rand = mt_rand(1, 28);
- $str1=substr($str_md5,$rand,6);
- $rand = mt_rand(1, 28);
- $str2=substr($str_md5,$rand,6);
- $rand = mt_rand(1, 28);
- $str3=substr($str_md5,$rand,6);
- $code=substr($str1.$str2.$str3,0,8);
生成180000個隨機字符串,圖中是按照重復數量倒序排列,可以看到基本都有重復的。不過也是比較理想的。
由于想提升一下自己的C語言能力,所以用C重新寫了一下隨機生成字符串。
其中用到了隨機數函數srand(),rand();
不過折騰一兩個小時,隨機數還是有問題。并發訪問時時間可能幾乎為同時,那么srand給的種子時間可以視為相同的。這樣就導致了,產生的隨機數也是一樣的。從而產生的隨機字符串也是一樣的。循環輸出隨機字符串,幾乎都是一模一樣的。
后來想到了ukey,這個擴展可以實現唯一的ID,那么訪問都產生唯一的ID,是不是可以將這個ID作為種子時間。答案是肯定的。
上圖是產生的隨機字符串,可以自定義長度。也同樣可以輸出只有數字的字符串。相較PHP所產生的隨機字符串重復率更低且速度更快。
- PHP_FUNCTION(get_random__num_str)
- {
- int length=8;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &length) == FAILURE)
- {
- length=8;
- }
- length++;
- int flag, i;
- char* string;
- __uint64_t timestamp = realtime();
- __uint64_t retval;
- int len;
- char buf[128];
- if (timestamp == 0ULL) {
- RETURN_FALSE;
- }
- spin_lock(lock, pid);
- if (context->last_timestamp == timestamp) {
- context->sequence = (context->sequence + 1) & context->sequence_mask;
- if (context->sequence == 0) {
- timestamp = skip_next_millis();
- }
- } else {
- context->sequence = 0; /* Back to zero */
- }
- context->last_timestamp = timestamp;
- retval = ((timestamp - context->twepoch) << context->timestamp_left_shift)
- | (context->datacenter_id << context->datacenter_id_shift)
- | (worker_id << context->worker_id_shift)
- | context->sequence;
- spin_unlock(lock, pid);
- //printf('%ld',retval);
- srand((unsigned)retval);
- //srand((unsigned) time(NULL ));
- if ((string = (char*) emalloc(length)) == NULL )
- {
- //myLog("Malloc failed!flag:14/n");
- RETURN_NULL() ;
- }
- for (i = 0; i < length - 1; i++)
- {
- flag = rand() % 3;
- switch (flag)
- {
- case 0:
- string[i] = '1' + rand() % 5;
- break;
- case 1:
- string[i] = '2' + rand() % 7;
- break;
- case 2:
- string[i] = '0' + rand() % 10;
- break;
- default:
- string[i] = '9';
- break;
- }
- }
- string[length - 1] = '/0';
- RETURN_STRINGL(string,length,0);
- }
- PHP_FUNCTION(get_random_str)
- {
- int length=8;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &length) == FAILURE)
- {
- length=8;
- }
- length++;
- int flag, i;
- char* string;
- __uint64_t timestamp = realtime();
- __uint64_t retval;
- int len;
- char buf[128];
- if (timestamp == 0ULL) {
- RETURN_FALSE;
- }
- spin_lock(lock, pid);
- if (context->last_timestamp == timestamp) {
- context->sequence = (context->sequence + 1) & context->sequence_mask;
- if (context->sequence == 0) {
- timestamp = skip_next_millis();
- }
- } else {
- context->sequence = 0; /* Back to zero */
- }
- context->last_timestamp = timestamp;
- retval = ((timestamp - context->twepoch) << context->timestamp_left_shift)
- | (context->datacenter_id << context->datacenter_id_shift)
- | (worker_id << context->worker_id_shift)
- | context->sequence;
- spin_unlock(lock, pid);
- //printf('%ld',retval);
- srand((unsigned)retval);
- //srand((unsigned) time(NULL ));
- if ((string = (char*) emalloc(length)) == NULL )
- {
- //myLog("Malloc failed!flag:14/n");
- RETURN_NULL() ;
- }
- for (i = 0; i < length - 1; i++)
- {
- flag = rand() % 3;
- switch (flag)
- {
- case 0:
- string[i] = 'A' + rand() % 26;
- break;
- case 1:
- string[i] = 'a' + rand() % 26;
- break;
- case 2:
- string[i] = '0' + rand() % 10;
- break;
- default:
- string[i] = 'x';
- break;
- //Vevb.com
- }
- }
- string[length - 1] = '/0';
- RETURN_STRINGL(string,length,0);
- }
上圖是PHP生成18W隨機字符串所用的時間
上圖是C擴展生成18W隨機字符串所用的時間
所用的服務器都是1G內存 雙核的阿里云服務器。
只要在ukey中加入上如代碼就可以生產隨機字符串和隨機長度數字字符串,PHP唯一ID生成擴展ukey。
php.ini的配置項:
- [ukey]
- ukey.datacenter = integer
- ukey.worker = integer
- ukey.twepoch = uint64
datacenter配置項是一個整數, 用于設置數據中心;
worker配置項是一個整數, 用于設置數據中心的機器序號;
twepoch配置項是一個64位的整數, 用于設置時間戳基數, 此值越大, 生成的ID越小;
安裝:
- $ cd ./ukey
- $ phpize
- $ ./configure
- $ make
- $ sudo make install
Ukey提供3個有用的函數:
ukey_next_id() -- 用于生成唯一ID
ukey_to_timestamp(ID) -- 用于將ID轉換成時間戳
ukey_to_machine(ID) -- 用于將ID轉換成機器信息
使用實例:
- <?php
- $id = ukey_next_id();
- echo $id;
- $timestamp = ukey_to_timestamp($id);
- echo date('Y-m-d H:i:s', $timestamp);
- $info = ukey_to_machine($id)
- var_dump($info);
- ?>
以上就是本文的全部內容,希望對大家的學習有所幫助。
新聞熱點
疑難解答