0

缓存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);

?>

萤火虫导致截图:

缓存后缓存缓存后缓存前

cache.dump 文件大小为 7kb

4

2 回答 2

2

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

您的代码正试图坐在别人的地方。数据库是用来存储数据的。
PHP 用于检索和格式化它。

只是不要使用自制缓存复制您的数据库。数据库有它自己的,而且速度非常快。

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

于 2013-06-05T08:28:45.040 回答
0

您可以使用var_export()而不是serialize. 我认为这会提高性能。

于 2013-06-05T08:10:40.117 回答