我正在尝试编写一个可以执行asynchronus memcache查询的方法。
这是我简单的get / set memcache客户端类。
Class MemcacheClient
{
private $socket;
private $reply;
private $replies = array(
'OK' => true,
'EXISTS' => false,
'DELETED' => true,
'STORED' => true,
'NOT_STORED' => false,
'NOT_FOUND' => false,
'ERROR' => null,
'CLIENT_ERROR' => null,
'SERVER_ERROR' => null
);
public function __construct($host, $port)
{
$this->socket = stream_socket_client("$host:$port", $errno, $errstr);
if(!$this->socket) {
throw new Exception("$errst ($errno)");
}
}
public function get($key)
{
$reply = $this->query("get $key");
return $reply;
}
public function set($key, $value, $exptime = 0, $flags = 0)
{
return $this->query(array("set $key $flags $exptime ".strlen($value), $value));
}
public function aget($key, $function)
{
}
public function process()
{
}
public function hasTasks()
{
}
private function query($query)
{
$query = is_array($query) ? implode("\r\n", $query) : $query;
fwrite($this->socket, $query."\r\n");
return $this->parseLine();
}
private function parseLine()
{
$line = fgets($this->socket);
$this->reply = substr($line, 0, strlen($line) - 2);
$words = explode(' ', $this->reply);
$result = isset($this->replies[$words[0]]) ? $this->replies[$words[0]] : $words;
if (is_null($result)) {
throw new Exception($this->reply);
}
if ($result[0] == 'VALUE') {
$value = fread($this->socket, $result[3] + 2);
return $value;
}
return $result;
}
}
我正在寻找像
这样的东西$memClient->aget('key', function($data) {echo $data;});
有关如何实施的任何想法?将不胜感激任何帮助。
谢谢!