Php reads the memory sequence number to generate a unique ID.

Source: Internet
Author: User
This code is google for tens of millions. Please do not add it to favorites or use it. Thank you for your cooperation. The author only makes a record. Note: You need to enable extended extensionphp_shmop.dll without ** semaphores (Semaphore ). * This is a packaging class used to solve different implementation methods of the "semaphore" on different platforms. * Currently, this class is symbolic.

This code is google for tens of millions. Please do not add it to favorites or use it. Thank you for your cooperation. The author only makes a record. Note: You need to enable extended extension = php_shmop.dll without/** semaphores (Semaphore ). * This is a packaging class used to solve different implementation methods of the "semaphore" on different platforms. * Currently, this class is symbolic.

This code is google for tens of millions. Please do not add it to favorites or use it. Thank you for your cooperation.
The author only makes a record.
Note: Extended extension = php_shmop.dll must be enabled. <无>
/** Semaphore ). * This is a packaging class used to solve different implementation methods of the "semaphore" on different platforms. * Currently, this class is symbolic. It is actually a dry run on Windows platforms (and does not actually implement mutual exclusion ). */Class SemWrapper {private $ hasSemSupport; private $ sem; const SEM_KEY = 1; public function _ construct () {$ this-> hasSemSupport = function_exists ('sem _ get'); if ($ this-> hasSemSupport) {$ this-> sem = sem_get (self :: SEM_KEY) ;}} public function acquire () {if ($ this-> hasSemSupport) {return sem_acquire ($ this-> sem);} return true;} public function release () {if ($ this-> hasSemSupport) {return sem_release ($ this-> sem);} return true ;}}
/** Sequence number generator. */Class SeqGenerator {const SHM_KEY = 1;/*** initialize the sequence number generator. * This method is valid only when it is called for the first time after the server is started. * @ Param int $ start generates the starting value of the sequence number. * @ Return boolean true indicates success. */Static public function init ($ start = 1) {// mutex is implemented through semaphores to avoid access conflicts to shared memory $ sw = new SemWrapper; if (! $ Sw-> acquire () {return false;} // open the shared memory $ shm_id = shmop_open (self: SHM_KEY, 'n', 0644, 4 ); if (empty ($ shm_id) {// because the 'N' mode is used, if the shared memory cannot be enabled, you can think that the shared memory has been created, no need to initialize $ sw-> release (); return true;} // write the initial value $ size = shmop_write ($ shm_id, pack ('l ', $ start), 0); if ($ size! = 4) {shmop_close ($ shm_id); $ sw-> release (); return false;} // close the shared memory and release the semaphores shmop_close ($ shm_id ); $ sw-> release (); return true;}/*** generates the next sequence number. * @ Return int the sequence number */static public function next () {// mutex through semaphores to avoid access conflicts to shared memory $ sw = new SemWrapper; if (! $ Sw-> acquire () {return 0;} // open the shared memory $ shm_id = shmop_open (self: SHM_KEY, 'w', 0, 0 ); if (empty ($ shm_id) {$ sw-> release (); return 0;} // read the sequence number $ data = shmop_read ($ shm_id, 0, 4); if (empty ($ data) {$ sw-> release (); return 0 ;}$ arr = unpack ('l', $ data ); $ seq = $ arr [1]; // write the next sequence number to the shared memory $ size = shmop_write ($ shm_id, pack ('l', $ seq + 1), 0 ); if ($ size! = 4) {$ sw-> release (); return 0 ;}// close the shared memory and release the semaphores shmop_close ($ shm_id); $ sw-> release (); return $ seq ;}}
// Usage method $ seq = SeqGenerator: next (); var_dump ($ seq );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.