php 讀取memcache位元據_PHP教程

來源:互聯網
上載者:User
memcache作為一個資料中介層,經常用來做資料交換。


比如在某個系統內部我們規定如下的使用者狀態的資訊,每個使用者只需要存續52個位元組。
Key state#ID 如”state#10888”
Value : (二進位的資料 )
使用者識別碼 Uint32
Type 使用者類型 Uint8 :
State 使用者狀態 Uint8 :
伺服器 IP Uint32
最後線上時間 Uint64
Session ID的長度 Uint16

Session ID char[32]

總共52個位元組


那麼怎麼在php裡面得到通過memcache得到上面的這些資料呢?


儲存的資料裡面有二進位的0,字串是否會被截斷?


其實不會的!


下面進行測試


$mem = new Memcache();

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

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

var_dump($memstr);
會得到下面的輸出。可以看到 memstr 剛好是53個位元組。sessionId有個結束符
string(53) "頊括F>� R!8jWFmsIK41kBDkmlqC7m7QoWICQ8nzz7"


再進一步,我們把資料輸出到一個檔案,用winhex來查看資料的狀態


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


用winhex dd.txt 會看到十六進位資料。


ED9C01000001C0A800463EF60A520000000100386A57466D73494B34316B42446B6D6C7143376D37516F57494351386E7A7A3700


下面我就可以按位元組取資料了,主要是利用ord函數擷取位元組ASCII碼


$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});

//時間戳記只需要4個位元組,分配了8個位元組
$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.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477168.htmlTechArticlememcache作為一個資料中介層,經常用來做資料交換。 比如在某個系統內部我們規定如下的使用者狀態的資訊,每個使用者只需要存續52個位元組。...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.