【问题标题】:PHP Codes After Caching [How to]捕获后的 PHP 代码 [如何]
【发布时间】:2017-03-03 20:10:07
【问题描述】:

我的 php 代码有问题。我正在使用具有此功能的简单缓存系统。

// Cache Function
function cachestart($min){
    global $here;
    global $cachefile;
    $cachedosyasi = 'cache/' . 'file' . md5($here);
    if(file_exists($cachefile) && (time() - $min*60 < filemtime($cachefile))){
    include($cachefile);
    die;
    }else{
        ob_start();
        global $cache;
        $cache = 1;
    }
}

function cachefinish(){
    global $cache;
    global $cachefile;
    if(@$cache){
        $ch = fopen($cachefile, 'w');
        fwrite($ch, ob_get_contents());
        fclose($ch);
        ob_end_flush(); 
    }
}

我正在尝试:

-- Some Queries (Controls, is user logged etc. )
<< Start Caching >> (With cachestart() )
-- Some Queries (Show entries in database. )
<< Stop Caching >> (With cachefinish() )
-- Some Queries (Comment box)

但我不能这样做,因为“死”。我对此一无所知。

感谢您的帮助!

【问题讨论】:

  • 所有查询都再次工作,我看到缓存函数之间的所有内容都翻了一番。编辑:有人说“删除功能中的'死'”,但评论已删除。

标签: php caching ob-start


【解决方案1】:

所以这并不完美,但使用您现有的代码;只需返回 truefalse 并让您的应用程序据此做出决定:

function cachestart($min){
    global $cachefile;
    if(file_exists($cachefile) && (time() - $min*60 < filemtime($cachefile))){
        include($cachefile);
        //we are not caching
        return false;
    } else {
        ob_start();
        //we are caching
        return true;
    }
}

在申请开始时做出决定:

if(!cachestart(60)) {
    //page loaded from cache
    //maybe do some queries for comment box
    die(); //or call cachefinish() with a die() in there
}

//main application code

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    相关资源
    最近更新 更多