Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Use PECL UUID (if present) instead of PHP UUID #29
On the note of high performance, there is a very nice PECL UUID generator, considerably better featured than PHP UUID class.
While PHP UUID is very useful for those who don't have/can't use PECL (and for pure class reasons), performance dictates otherwise, especially if we need to generate many UUIDs quickly and with very low system overhead.
add uuid.so to /etc/php.ini or /etc/php.d/uuid.ini
after that, fork out UUID generation to
Besides pure performance differences, using PECL class also generates strictly compliant Type 1 UUIDs, which use timestamp and MAC addressin generation (latter can be quite cumbersome to get in PHP directly without system calls)
In addition, PECL class provides a few very nifty functions, such as uuid_timestamp and uuid_mac to respectfully retrieve timestamp and generator's mac address from a type 1 uuid, as well as uuid_is_valid() and uuid_is_null methods, which allow one to validate uuids without using preg_match (again, for performance reasons).
a uuid wrapper sounds like a good option, at least either the pecl or ossp (http://pwet.fr/man/linux/fonctions_bibliotheques/ossp/uuid) modules can be opted for, or custom uuid generators properly interfaced.
have you used the shapeshifter library successfully? It was fine until trying to convert back from binary uuid loads to their string equivalents.
OSSP seems like it requires calling a CLI or using a C API? I'd rather not go there.
Shapeshifter worked well for me (tested it compared to PECL UUID). The only major drawback, of course (besides speed), is non-strict RFC compliance, which I solved by adding a MAC address option to the generator method, which packs a (known beforehand) MAC address as a 6-byte string and passes it to Shapeshifter's UUID::generate in place of the 3rd parameters. A little crude (alas, I don't know a good way to find MAC from PHP without calling exec which I avoid), but we use PECL UUID on all our clients, so it's really really a fallback for us, more so than a main generator
Regarding the to/from binary conversion - not sure what you mean. In our environment, I wrote a custom packer/unpacker for cassandra/human readable and back conversion, along something like this:
The result is safe for cassandra insertion (and preserves the ordering)
And the inverse method:
Using those 2 on the fly to convert column names for inserting/reading to/from cassandra removes any need for a third party binary packer dependence.
And the validation method too
neat, thanks :)
the ossp uuid library has a php module wrapper, basically 1:1 vs the C API including its pass by reference import/export. Very fast, but minus the pecl uuid helper functions (like uuid_is_valid, parse/unparse for example). I'll figure out a happy medium between all these options.