<?php

extension_loaded('tokyocabinet') || dl('tokyocabinet.so') || exit(1);
try {
    $hdb = new TCHDB(TCHDB::KTBINARY, TCHDB::VTSERIALIZED);
    $hdb->open('casket-s.hdb', TCHDB::OWRITER | TCHDB::OCREAT);
    $hdb->put('foo', array('hop'));
    $hdb->put('bar', array('step', 'Step'));
    $hdb->put('baz', array('jump', 'Jump', 'JUMP'));
    print_r($hdb->get('foo'));
    echo "-\n";
    foreach ($hdb as $key => $value) {
        printf("%s:%s", $key, print_r($value, true));
    }
    echo "-\n";
    printf("%s (%d records, %d bytes)\n", $hdb->path(), $hdb->rnum(), $hdb->fsiz());
    $hdb->sync();
    unset($hdb);
    // destruct
    echo "--\n";
    $raw = new TCHDB();
    $raw->open('casket-s.hdb', TCHDB::OREADER);
    foreach ($raw as $key => $value) {
        printf("%s:%s\n", $key, $value);
    }
} catch (TCException $e) {
    echo $e->getMessage(), "\n";
}
Example #2
0
<?php

extension_loaded('tokyocabinet') || dl('tokyocabinet.so') || exit(1);
try {
    $hdb = new TCHDB(TCBDB::KTINT64);
    $hdb->open('casket-i64.hdb', TCHDB::OWRITER | TCHDB::OCREAT);
    $hdb->put(2, 'hop');
    $hdb->put(0, 'step');
    $hdb->put(1, 'jump');
    echo $hdb->get(2), "\n";
    echo "-\n";
    foreach ($hdb as $key => $value) {
        printf("%s(%s):%s\n", gettype($key), $key, $value);
    }
    echo "-\n";
    printf("%s (%d records, %d bytes)\n", $hdb->path(), $hdb->rnum(), $hdb->fsiz());
} catch (TCException $e) {
    echo $e->getMessage(), "\n";
}
Example #3
0
    /**
     * Send a HTTP response.
     *
     * @param int $code
     * @param string $mimetype
     * @param string $body
     * @param array $headers
     * @return void
     */
    private function returnResponse($code, $body = '', $mimetype = 'text/plain', array $headers = array())
    {
        $msg = new HttpMessage();
        $msg->setType(HttpMessage::TYPE_RESPONSE);
        $msg->setResponseCode($code);
        $msg->setResponseStatus(self::$statuses[$code]);
        $headers['Content-Type'] = (string) $mimetype;
        $headers['Content-Length'] = (string) strlen($body);
        $msg->setHeaders($headers);
        $msg->setBody($body);
        fwrite($this->conn, $msg->toString());
    }
}
/**
 * Run the test server.
 */
if (PHP_SAPI == 'cli' && realpath($_SERVER['SCRIPT_FILENAME']) == __FILE__) {
    $hdb = new TCHDB();
    $hdb->open('/tmp/server-test.hdb', TCHDB::OWRITER | TCHDB::OCREAT);
    $server = new TCHDBServer($hdb);
    $server->run();
}