Home > Chỉ dẫn lập trình > Test Memcache

Test Memcache

<?php
$startTime = microtime(true);
$post_id = isset($_GET['p']) ? $_GET['p'] : 'xyz';
$memcached = new Memcache;
$memcached->connect('localhost', 11211);
if (($rows = $memcached->get('post_id_' . $post_id)) === false) {
    echo 'Init cache<br />';
    //yes this is safe, we type casted it already ;)
    mysql_connect('localhost:3306', 'root', 'Clip2008');
    $rs = mysql_query('SHOW TABLES IN mysql');
    if ($rs && mysql_num_rows($rs) > 0) {
        $rows = array();
        while ($row = mysql_fetch_assoc($rs)) {
            $rows[] = $row;
        }
        // cache compressed for 1 hour
        $memcached->set('post_id_' . $post_id, $rows, MEMCACHE_COMPRESSED, time() + 3600);
    }
} else {
    echo 'Load from cache<br />';
}
var_dump($rows);

echo microtime(true) – $startTime;

  1. Chưa có bình luận nào.
Content (You can use Yahoo! Messenger Emoticons :D = smile)
Captcha