<?
// connect to memcached
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ('no memcached');
// connect to mysql
$link = mysql_connect('localhost', 'msw', '');
assert($link);
$db = mysql_select_db('test', $link);
assert($db);
list($usec, $sec) = explode(" ", microtime());
$start = (float) $usec + (float) $sec;
// for filling up the DB with test data
/*
for ($i = 0; $i < 1200000; $i++)
{
$rs = mysql_query('INSERT INTO Test (Name, age) VALUES ("'.
mysql_real_escape_string(md5(rand())).'", '.$i.')');
}
*/
$q = 'SELECT id FROM Test WHERE Name LIKE "%f6543c%" AND AGE > 3';
$rows = $memcache->get(strtolower($q));
// handle cache miss
if ($rows === false)
{
$rs = mysql_query($q);
$rows = array();
while ($row = mysql_fetch_assoc($rs))
{ $rows[] = $row['id'];
}
$memcache->set(strtolower($q), $rows, 0, 10);
}
list($usec, $sec) = explode(" ", microtime());
$end = (float) $usec + (float) $sec;
echo "rows found: ".count($rows)."\n";
foreach ($rows as $r)
{ echo 'row id:'.$r."\n";
}
printf("elapsed time: %.3f\n", $end - $start);
?>
--
MattWalsh - 24 Oct 2008