Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class KyotoTycoon {
- const METHOD_GET = 'GET';
- const METHOD_POST = 'POST';
- const DEFAULT_HOST = 'localhost';
- const DEFAULT_PORT = '1978';
- const DEFAULT_TIMEOUT = 0.5;
- const DEFAULT_METHOD = self::METHOD_GET;
- private $host = null;
- private $port = null;
- private $method = null;
- private $timeout = null;
- private $sockRes = false;
- public function __construct($in_host = self::DEFAULT_HOST, $in_port = self::DEFAULT_PORT, $in_timeout = self::DEFAULT_TIMEOUT, $in_method = self::DEFAULT_METHOD)
- {
- $this->host = $in_host;
- $this->port = $in_port;
- $this->timeout = $in_timeout;
- $this->method = $in_method;
- }
- public function set($key, $value, $expire = null)
- {
- // TODO
- $result = $this->write(__FUNCTION__, $key, $value, $expire);
- return true;
- }
- public function get($key)
- {
- $lines = explode("\n", trim($this->write(__FUNCTION__, $key)));
- $result = array('value' => null);
- foreach($lines as $data) {
- $tmp = @explode("\t", $data);
- if(1 < count($tmp)) $result[$tmp[0]] = $tmp[1];
- }
- return $result['value'];
- }
- public function remove($key)
- {
- $result = trim($this->write(__FUNCTION__, $key));
- $IsOK = true;
- if(stristr($result,"ERROR")) $IsOK = false;
- return $IsOK;
- }
- public function getTimeout() { return $this->timeout; }
- public function setTimeout($in_timeout)
- {
- $IsOK = false;
- //! Set Socket Timeout when socket is not open.
- if(!$this->sockRes && is_numeric($in_timeout)) {
- $this->timeout = (float) $in_timeout;
- $IsOK = true;
- }
- return $IsOK;
- }
- public function getMethod() { return $this->method; }
- public function setMethod($in_method)
- {
- $IsOK = false;
- switch($in_method) {
- case self::METHOD_GET:
- case self::METHOD_POST:
- $this->method = $in_method;
- $IsOK = true;
- break;
- default:
- break;
- }
- return $IsOK;
- }
- private function write($cmd, $key, $value = null, $expire = null)
- {
- $func = 'writeOn'. $this->method;
- $this->open();
- $result = $this->$func($cmd, $key, $value, $expire);
- $this->close();
- return $result;
- }
- private function writeOnGET($cmd, $key, $value, $expire)
- {
- $sendStr = '';
- $sendStr .= sprintf("GET /rpc/%s?%s HTTP/1.1\r\n", $cmd, $this->buildString($key, $value, $expire));
- $sendStr .= sprintf("Host: %s:%d\r\n", $this->host, $this->port);
- $sendStr .= sprintf("Connection: Close\r\n\r\n");
- $result = '';
- @fwrite($this->sockRes, $sendStr);
- while(!@feof($this->sockRes)) {
- $tmp = @fgets($this->sockRes, 1024);
- $result .= $tmp;
- }
- return $result;
- }
- private function writeOnPOST($cmd, $key, $value, $expire)
- {
- $postReq = $this->buildString($key, $value, $expire);
- $sendStr = '';
- $sendStr .= sprintf("POST /rpc/%s HTTP/1.1\r\n", $cmd);
- $sendStr .= sprintf("Host: %s:%d\r\n", $this->host, $this->port);
- $sendStr .= sprintf("Content-Type: application/x-www-form-urlencoded\r\n");
- $sendStr .= sprintf("Content-Length: %d\r\n", strlen($postReq));
- $sendStr .= sprintf("Connection: Close\r\n\r\n");
- $sendStr .= sprintf("%s \r\n\r\n", $postReq);
- $result = '';
- @fwrite($this->sockRes, $sendStr);
- while(!@feof($this->sockRes)) {
- $tmp = @fgets($this->sockRes, 1024);
- $result .= $tmp;
- }
- return $result;
- }
- private function buildString($key, $value, $expire)
- {
- $result = 'key=' . $key;
- if($value) $result .= "&value=$value";
- if($expire) $result .= "&xt=$expire";
- return $result;
- }
- private function open()
- {
- if($this->sockRes) return false;
- $this->sockRes = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
- if(!$this->sockRes) {
- throw new Exeption(sprintf("[Error] %s (%s)", $errstr, $errno));
- }
- }
- private function close()
- {
- if($this->sockRes) fclose($this->sockRes);
- $this->sockRes = false;
- }
- }
Add Comment
Please, Sign In to add comment