PHP read memcache binary data _php tutorial

Source: Internet
Author: User
Memcache, as a data middle layer, is often used for data exchange.


For example, within a system we specify the following user status information, each user only need to survive 52 bytes.
Key state#id such as "state#10888"
Value: (binary data)
User ID Uint32
Type user Types Uint8:
State user Status Uint8:
Server IP Uint32
Last Online time Uint64
Length of Session ID Uint16

Session ID Char[32]

A total of 52 bytes


So how to get the above data in PHP by Memcache?


If the stored data has binary 0, will the string be truncated?


No, actually!


Test the following


$mem = new Memcache ();

$mem->connect (' 192.168.0.69 ', 11211);

$memstr = $mem->get (' state#105709 ');

Var_dump ($MEMSTR);
will get the following output. You can see that the memstr is exactly 53 bytes. SessionID has a terminator.
String (53) "頊 including f> r!8jwfmsik41kbdkmlqc7m7qowicq8nzz7"


Further, we export the data to a file and use Winhex to view the status of the data


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


With Winhex dd.txt you will see hex data.


ed9c01000001c0a800463ef60a520000000100386a57466d73494b34316b42446b6d6c7143376d37516f57494351386e7a7a3700


So I can take 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 {) *16+ord ($memstr {18});

Timestamp requires only 4 bytes, 8 bytes Allocated
$lastactive = Ord ($memstr {*16777216+ord}) *65536+ord ($memstr {one}) *256+ord ($memstr {10});

$sessionid = substr ($memstr, $ses _long);

http://www.bkjia.com/PHPjc/477168.html www.bkjia.com true http://www.bkjia.com/PHPjc/477168.html techarticle Memcache, as a data middle layer, is often used for data exchange. For example, within a system we specify the following user status information, each user only need to survive 52 bytes. ...

  • 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.