打造先进的内存KV数据库-6 PHP支持

PHP

php作为使用极广的程序设计语言,monkey数据库对php的支持是必须的~

代码实现

//test.php
<?php
class MonkeyDB
{
    private $socket;

    private function read()
    {
        $data = "";
        $total = 0;
        $t = fread($this->socket,1024);
        for($i = 0;$i < 4;$i++)
        {
            $total *= 256;
            $total += ord($t[$i]);
        }
        $data = substr($t,4);
        $total -= 1024;
        while($total > 0)
        {
            $buf = fgets($this->socket,1024);
            $data .= $buf;
            $total -= 1024;
        }
        return $data;
    }

    private function write($string)
    {
        $total = strlen($string) + 4;
        fwrite($this->socket,strrev(pack("L",$total)).$string);
    }

    public function __construct($addr)
    {
        set_time_limit(0);
        ob_implicit_flush();
        $this->socket = stream_socket_client("tcp://{$addr}:1517");
        if(!$this->socket)
        {
            throw new Exception("monkey 连接失败!");
        }
    }

    public function __destruct()
    {
        fclose($this->socket);
    }

    //data: string  serialize if necessary
    public function set($key,$data)
    {
        if(!$this->socket)
        {
            throw new Exception("monkey 连接失败!");
        }
        $this->write("set {$key} ".($data));
        $this->read();
    }

    //return: string
    public function get($key)
    {
        if(!$this->socket)
        {
            throw new Exception("monkey 连接失败!");
        }
        $this->write("get {$key}");
        $data = $this->read();
        return ($data);
    }

    public function remove($key)
    {
        if(!$this->socket)
        {
            throw new Exception("monkey 连接失败!");
        }
        $this->write("remove {$key}");
        $this->read();
    }

    public function createDB($dbName)
    {
        if(!$this->socket)
        {
            throw new Exception("monkey 连接失败!");
        }
        $this->write("createdb {$dbName}");
        $this->read();
    }

    public function switchDB($dbName)
    {
        if(!$this->socket)
        {
            throw new Exception("monkey 连接失败!");
        }
        $this->write("switchdb {$dbName}");
        $data = $this->read();
    }

    public function dropDB($dbName)
    {
        if(!$this->socket)
        {
            throw new Exception("monkey 连接失败!");
        }
        $this->write("dropdb {$dbName}");
    }

    //return string
    public function listDB()
    {
        if(!$this->socket)
        {
            throw new Exception("monkey 连接失败!");
        }
        $this->write("listdb ");
        $data = $this->read();
        return $data;
    }
}

 $monkey = new MonkeyDB("127.0.0.1");
 for($i = 0;$i < 100000;$i++)
    $monkey->set("{$i}","{$i}");

经测试使用php调用,在我的256K缓存CPU,8G笔记本内存可以达到15000req/s每个请求1K数据左右的速度,TCP传输也占用了大量时间,考虑之后进行改进,可以支持批量数据处理,不过单线程速度已经是一流的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值