缓存mysql结果后PHP速度低

时间:2013-06-05 08:05:27

标签: php

缓存mysql结果后我的网页下载到加载页面,我正在测试firebug并获得缓存结果之后和之前的任何结果,当我不使用缓存时我的页面可以快速加载,

PHP:

<?php
@session_start();
$filename = '../cache/cache.dump';
$AND = $WHERE = NULL;
if ( isset( $_SESSION['portal_ID'] )) {
    $AND   = " AND contents.portal = {$_SESSION['portal_ID']}";
    $WHERE = " WHERE portal = {$_SESSION['portal_ID']}";
}

// ------------------ CACHE MYSQL RESULTS ----------------
if (filemtime($filename) < time()-24*3600) { 
    $getTopics=$db->loadAssoc($db->setQuery("SELECT * ,  categories.title AS category_title, 
                                            status_topics.title as status_topic
                                            FROM contents
                                            JOIN categories ON categories.id = contents.category
                                            JOIN status_topics ON status_topics.id = contents.status 
                                            $AND"));
    $getCategories           = $db->loadAssoc($db->setQuery("SELECT * FROM categories $WHERE"));
    $getAllCategories        = $db->loadAssoc($db->setQuery("SELECT * FROM categories $WHERE"));
    $getTags                 = $db->loadAssoc($db->setQuery("SELECT * FROM tags $WHERE"));
    $getAllTags              = $db->loadAssoc($db->setQuery("SELECT * FROM tags "));
    $getStatusTopics         = $db->loadAssoc($db->setQuery("SELECT * FROM status_topics"));
    $getPages                = $db->loadAssoc($db->setQuery("SELECT * FROM pages $WHERE"));
    $getSiteInformation      = $db->loadRow  ($db->setQuery("SELECT * FROM settings $WHERE"));
    $getSubDomainInformation = $db->loadAssoc($db->setQuery("SELECT * FROM sub_domains"));
    $getUserInformation      = $db->loadAssoc($db->setQuery("SELECT * FROM users $WHERE"));
    $getActiveSubDomains     = $db->loadAssoc($db->setQuery("SELECT * FROM sub_domains WHERE  subdomain_active = 1"));
    $getDeactiveSubDomains   = $db->loadAssoc($db->setQuery("SELECT * FROM sub_domains WHERE  subdomain_active = 0"));
    $getUser_information=$db->loadRow($db->setQuery("SELECT * FROM users JOIN permissions on permissions.id = users.permission WHERE username = '{$_SESSION['username']}'"));
    file_put_contents($filename, serialize(
        array(
                $getTopics,
                $getCategories, 
                $getAllCategories,
                $getTags,
                $getAllTags,
                $getStatusTopics,
                $getPages,
                $getSiteInformation,
                $getSubDomainInformation,
                $getUserInformation,
                $getActiveSubDomains,
                $getDeactiveSubDomains,
                $getUser_information
            ))); 
} 
// ------------------READ FROM CACHE MYSQL RESULTS ----------------
else 
{
  $data = unserialize(file_get_contents($filename));
  list(
        $getTopics,
        $getCategories, 
        $getAllCategories,
        $getTags,
        $getAllTags,
        $getStatusTopics,
        $getPages,
        $getSiteInformation,
        $getSubDomainInformation,
        $getUserInformation,
        $getActiveSubDomains,
        $getDeactiveSubDomains,
        $getUser_information
      ) = $data;
}

//print_r($WHERE);

?>

firebug导致截图:

缓存after cache后的

在缓存before cache之前

cache.dump文件大小为7kb

2 个答案:

答案 0 :(得分:2)

看起来你选择的方法是错误的。

您的代码正试图坐在别人的位置上。 数据库用于存储数据 PHP用于检索和格式化它。

请勿使用自制缓存复制数据库。数据库拥有它自己,并且非常快。

所以,只需摆脱这种“缓存”并采用通用方式 - 在每个页面上选择此页面上所需的唯一数据。这就是全部

答案 1 :(得分:0)

您可以使用var_export()而不是serialize。我认为这会改善表现。