недавно начал разбирать функции по Memcache кешированию нo возникли вопросы.
как правильно кешировать запрос в Memcache что бы исключить вторичные подключения к Мускулу если сохраннёный ключ ещё действует:
Вот пример того что я собрал
PHP |
$memcached = new Memcache(); |
Цитата (glock18 @ 31.10.2009 - 04:19) |
ты пытаешься кэшировать текст запроса? |
Цитата (glock18 @ 31.10.2009 - 15:53) |
Если не нужно, чтобы соединения каждый раз создавались, то используй mysql_pconnect. |
Цитата (Sylex @ 31.10.2009 - 10:27) | ||
на практике использовал? |
Цитата |
на практике использовал? |
Цитата |
Все быстрее. |
$gethash = $_GET['info_hash'];
if ($gethash) {
$memcache = new Memcache();
$memcache->connect('127.0.0.1', 11211);
if (!$memcache->get("scrape_hash_".$gethash."")) {
$inserter = mysql_fetch_assoc(mysql_query("SELECT info_hash, times_completed, ders, hers FROM video WHERE info_hash = '".$gethash."'"));
if (!$inserter) {
print('No info hash. 1');
}
$memcache->set("scrape_hash_".$gethash."", $inserter, 0, 90);
} else {
$inserter = $memcache->get("scrape_hash_".$gethash."");
}
}else{
print('No info hash.');
}
while ($row = $inserter) {
print (" ".hash_pad($row['info_hash'])."");
print (" ".$row['ders']."");
print (" ".$row['times_completed']."");
print (" ".$row['hers']."");
}
Цитата (agentor @ 27.11.2009 - 20:01) |
почему у меня при выводе данных через while () такая ерунда? |
$inserter = mysql_fetch_assoc(mysql_query("..."));
Цитата (agentor @ 27.11.2009 - 20:12) |
интересно..каким образом можно в кеш запихнуть while () результат??? |
$data = array();
while($row = mysql_fetch_assoc($result))
{
$data[] = $row;
}
$memcache->set('key', $data);
If($data) {
print (" ".hash_pad($row['info_hash'])."");
print (" ".$row['ders']."");
print (" ".$row['times_completed']."");
print (" ".$row['hers']."");
}
$global_user_curuser = $memcached->get('global_user_curuser_' . $id);
if (false === $global_user_curuser) {
// значения нет
// выполняем запрос
// собираем данные в массив, используя mysql_fetch_assoc например
// сохраняем в кэш
$memcached->set('global_user_curuser_' . $id, $global_user_curuser); // флаги на усмотрение ставь
}
// значение есть
$data = array();
while($row = mysql_fetch_assoc($result)){
$data[] = $row;
print (" ".hash_pad($row['info_hash'])."");
print (" ".$row['ders']."");
print (" ".$row['times_completed']."");
print (" ".$data['hers']."");
}
$memcache->set("scrape_hash_".$gethash, $data, 0, 30);
$got ="<b>Работает MYSQL</b><br />";
} else {
$row = array();
$row[] = $memcache->get("scrape_hash_".$gethash);
$got ="<b>Работает Memcache</b><br />";
print (" ".hash_pad($row['info_hash'])."");
print (" ".$row['ders']."");
print (" ".$row['times_completed']."");
print (" ".$data['hers']."");
}
echo "$got";
$gethash = $_GET['info_hash'];
$memcache = new Memcache;
$memcache->connect("localhost", 11211) or die("Memcache Connection Failed");
$gethash = array();
function rezults($gethash) {
$memcache = new Memcache;
$memcache->connect("localhost", 11211) or die("Memcache Connection Failed");
$articles = $memcache->get("scrape_hash_" . $gethash);
if ($articles) {
return $articles;
}
$result = mysql_query("SELECT info_hash, times_completed, ders, hers FROM news WHERE info_hash = '".$gethash."'");
$articles = array();
while ($article = mysql_fetch_assoc($result)) {
$articles[] = $article;
}
$memcache->set("scrape_hash_".$gethash, $articles,0,30); // Store for 30 sek
return $articles;
}
rezults($gethash);