Home >Backend Development >PHP Tutorial >PHP reads memcache binary data_PHP tutorial
Memcache, as a data middle layer, is often used for data exchange.
For example, within a certain system, we stipulate the following user status information. Each user only needs to persist 52 bytes.
Key state#ID such as "state#10888"
Value: (binary data)
User ID Uint32
Type User type Uint8 :
State User state Uint8:
Server IP Uint32
Last online time Uint64
Length of Session ID Uint16
Session ID char[32]
52 bytes in total
So how do you get the above data through memcache in php?
There are binary 0s in the stored data. Will the string be truncated?
Actually no!
Test below
$mem = new Memcache();
$mem->connect('192.168.0.69',11211);
$memstr= $mem->get('state#105709');
var_dump($memstr);
You will get the following output. You can see that memstr is exactly 53 bytes. sessionId has a terminator
string(53) "枼库F>� R!8jWFmsIK41kBDkmlqC7m7QoWICQ8nzz7"
$state = ord($memstr{5});
$ip = ord($memstr{6}).'.'.ord($memstr{7}).'.'.ord($memstr{8}).'.'.ord($memstr{9}) ;
$ses_long = ord($memstr{19})*16+ord($memstr{18});
//The timestamp only requires 4 bytes and 8 bytes are allocated
$sessionid = substr($memstr,20,$ses_long);
http://www.bkjia.com/PHPjc/477168.html