Home >Backend Development >PHP Tutorial >PHP reads memcache binary data_PHP tutorial

PHP reads memcache binary data_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:21:40984browse

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"

Going one step further, we output the data to a file and use winhex to check the status of the data


              file_put_contents('./dd.txt',$memstr);


Use winhex dd.txt to see hexadecimal data.


ED9C01000001C0A800463EF60A520000000100386A57466D73494B34316B42446B6D6C7143376D37516F57494351386E7A7A3700


Now I can get the data by byte, mainly using the ord function to get the byte ASCII code


            $type = ord($memstr{4});

                                   
             $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

            $lastactive = ord($memstr{13})*16777216+ord($memstr{12})*65536+ord($memstr{11})*256+ord($memstr{10});


$sessionid = substr($memstr,20,$ses_long);

http://www.bkjia.com/PHPjc/477168.html

truehttp: //www.bkjia.com/PHPjc/477168.htmlTechArticle As a data middle layer, memcache 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. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn