PHP缓存太长

问题描述:

I rolled out a pretty simple cache system on my site last week using a PHP class referenced in my sitewide template file. The cache system dramatically improved server response times (the template pulls data from a couple other servers in our datacenter, so it isn't all cached in memory by Apache). The problem is, it was over-caching files. I believe I have it set to cache for five minutes, or until the HTML is modified, but it was still serving cached versions of pages days after the cached versions were created.

Can anyone spot any issues with my code, or do you know about any versions of PHP/Apache/Red Hat that might, say, report bad info on file modification times? (We're on PHP 5.3.3, Apache 2.2.15, and RHEL 6. I had been running mod_pagespeed, but the problem persisted even after I disabled it.)

<?php

class IWU_Cache {
    protected $cache_prefix = '/tmp/webcache_';
    protected $remote_delay = 300; // in seconds; 300 is five minutes
    protected $path;

    public function __construct($path = '') {
        $this->path = $path;
    }

    public function isRecentlyCached() {
        $cache_location = $this->path2Cache($this->path);

        if(is_file($cache_location)) {
            if(is_file($_SERVER['DOCUMENT_ROOT'] . $this->path) && (filemtime($_SERVER['DOCUMENT_ROOT'] . $this->path) < filemtime($cache_location))) {
                return TRUE;
            }
            elseif(filemtime($cache_location) > time() - $this->remote_delay) {
                return TRUE;
            }
        }

        return FALSE;
    }

    public function getCachedVersion() {
        return file_get_contents($this->path2Cache($this->path));
    }

    public function addToCache($html) {
        return file_put_contents($this->path2Cache($this->path), $html);
    }

    protected function path2Cache($path) {
        return $this->cache_prefix . str_replace('/', '____', $path);
    }
}

?>

上周我使用我的全站模板文件中引用的PHP类在我的网站上推出了一个非常简单的缓存系统。 缓存系统显着改善了服务器响应时间(模板从我们数据中心的其他几台服务器中提取数据,因此Apache并未将其缓存在内存中)。 问题是,它是过度缓存文件。 我相信我已将其设置为缓存五分钟,或直到HTML被修改,但它仍然在创建缓存版本后几天提供缓存版本的页面。 p>

任何人都可以发现 我的代码有任何问题,或者您是否知道任何版本的PHP / Apache / Red Hat可能会报告文件修改时间的错误信息? (我们使用的是PHP 5.3.3,Apache 2.2.15和RHEL 6.我一直在运行mod_pagespeed,但是在我禁用它之后问题仍然存在。) p>

   &lt;?php 
 
class IWU_Cache {
 protected $ cache_prefix ='/ tmp / webcache _'; 
 protected $ remote_delay = 300;  // 很快;  300是五分钟
 protected $ path; 
 
公共函数__construct($ path =''){
 $ this-&gt; path = $ path; 
} 
 
公共函数isRecentlyCached(){  
 $ cache_location = $ this-&gt; path2Cache($ this-&gt; path); 
 
 if(is_file($ cache_location)){
 if(is_file($ _ SERVER ['DOCUMENT_ROOT']。$ this-  &gt;路径)&amp;&amp;(filemtime($ _ SERVER ['DOCUMENT_ROOT']。$ this-&gt; path)&lt; filemtime($ cache_location))){
返回TRUE; 
} 
 elseif(filemtime(  $ cache_location)&gt; time() -  $ this-&gt; remote_delay){
返回TRUE; 
} 
} 
 
返回FALSE; 
} 
 
公共函数getCachedVersion(){
  return file_get_contents($ this-&gt; path2Cache($ this-&gt; path)); 
} 
 
公共函数addToCache($ html){
 return file_put_contents($ this-&gt; path2Cache($ this-&gt  ; path),$ html); 
} 
 
受保护的函数path2Cache($ path){
 return $ this-&gt; ca  che_prefix。  str_replace('/','____',$ path); 
} 
} 
 
?&gt; 
  code>  pre> 
  div>

I assume you are calling isRecentlyCached() to determine whether the file is cached? In which case, once the cache file has been written then the first condition will always be true, until the actual file is changed again...

if(is_file($_SERVER['DOCUMENT_ROOT'] . $this->path) && (filemtime($_SERVER['DOCUMENT_ROOT'] . $this->path) < filemtime($cache_location))) {
    return TRUE;
}

In other words (pseudo code):

if (timestamp of actual file < timestamp of cached file) /* TRUE */