2012-01-06 58 views
6

我有一个网站,基本上只显示没有任何形式和事后获取的东西。 这个网站是基于PHP的,并托管在共享主机上。它很少改变。 我想为这个网站启用缓存。 它的共享托管,所以我需要一个解决方案:基于超静态文件(html)的PHP网站缓存

  • 没有使用Memcached的
  • 不需要到我的网站转移到VPS
  • 不要使用APC或其他东西

所以基本上什么我想说的是将每个子网站缓存到HTML并告诉PHP获取5分钟当前子网站的HTML缓存版本并将其显示给用户。并在5分钟后刷新缓存。

我一直在寻找一些在互联网上,有一些教程和框架,支持这种扭曲的缓存。

但我需要的只是一个非常好用的库。

我想象它以这种方式工作:

<? 

if (current_site_cache_is_valid()) 
{ 
    display_cached_version(); 
    die; 
} 

..mywebsite rendering code 

?> 

,因为它听起来很简单,但我希望有好人开发商之前做过这样的图书馆。那么您是否知道这样准备好使用,而不是非常耗时地实施解决方案?

+0

如果你可以使用PEAR,PEAR_Cache可能是你所需要的:http://pear.php.net/package/Cache – 2012-01-06 12:59:50

+0

你有apache + .htaccess吗? – hakre 2012-01-06 13:01:40

+0

可能的重复:[如何为PHP网站实现HTML缓存?](http://stackoverflow.com/questions/55223/how-do-i-implement-a-html-cache-for-a- php站点) – hakre 2012-01-06 13:51:08

回答

5

这是我通常这样做的,但是我不知道你的URL设计,也没有你的目录/文件格式。

我用.htaccessmod_rewrite­Docs这样做。

网络服务器检查缓存的HTML文件是否存在,如果存在,它将被传送。你也可以检查它的年龄。

如果太旧或者它不存在,则启动PHP脚本(s?)。在你的脚本的开头你start the output buffer­Docs。在脚本结尾处,您将获得输出缓冲区,并将内容放入缓存文件中,然后输出它。

这个解决方案的好处是,apache将提供静态文件,以防它们存在并且不需要调用PHP进程。如果你在PHP本身内部完成这些工作,你将不会获得这种好处。

我甚至会更进一步,运行一个cron-job,删除较旧的缓存文件,而不是在.htaccess中进行时间检查。完成之后,您可以使重写更简单,以选择.php.cached文件而不是.php文件。

+1

以及如何实现它? – 2012-01-07 20:46:48

+0

@tomaszs:当你需要它时,它取决于你的url/file/cache-layout。如果您正在寻找示例和想法,我觉得这篇文章非常有见地:[使用PHP,C++和Apache mod_rewrite的高级缓存机制](http://sven.webiny.com/advanced-cache-mechanism-using-php- cpp-and-apache /) - 接下来请看这个相关的问题:[RewriteRule检查文件在重写文件路径中存在](http:// stackoverflow。com/questions/470880/rewriterule-checking-file-in-rewriten-file-path-exists) – hakre 2012-02-15 17:19:46

1

您应该试试skycache编辑:这个项目似乎也很酷:cacheme

另一种解决方案是使用auto_prepend_file/auto_append_file。像什么在本教程中所描述的内容:Output caching for beginners

+0

skycache似乎很有趣,但它似乎不能在安全模式下工作。有任何想法吗? – 2012-01-07 21:43:37

+0

发现这个:https://github.com/tlrobinson/cacheme.php – greut 2012-01-11 12:46:26

3

我有一个HTML缓存简单的算法中,预测下列条件

  • 用户是游客(登录的用户有一个blog_user设置Cookie)
  • 请求URI是不包含请求参数
  • 文件的HTML缓存版本存在

那么一个GET重写规则,将请求映射到缓存文件。其他任何东西都被假定为特定于上下文的,因此不可缓存。请注意,我为我的博客使用wikipedia风格的URI映射,因此/article-23在未缓存时映射到/index.php=article-23

我在我的DOCUMENT_ROOT目录中使用一个HTML访问文件,这里是相关的提取。这是第三个重写规则,可以做你想做的事情。任何生成可高速缓存的O/P的脚本都会在ob_start()ob_get_clean()对中包装它并写出HTML缓存文件(尽管这全部由我的模板引擎处理)。更新也会根据需要刷新HTML缓存目录。

RewriteEngine on 
RewriteBase/

# ... 
# Handle blog index 
RewriteRule ^blog/$         blog/index    [skip=1] 

# If the URI maps to a file that exists then stop. This will kill endless loops 

RewriteCond %{REQUEST_FILENAME}      -f 
RewriteRule ^blog/.*         -      [last] 

# If the request is HTML cacheable (a GET to a specific list, with no query params) 
# the user is not logged on and the HTML cache file exists then use it instead of executing PHP 

RewriteCond %{HTTP_COOKIE}        !blog_user 
RewriteCond %{REQUEST_METHOD}%{QUERY_STRING}   =GET      [nocase] 
RewriteCond %{DOCUMENT_ROOT}/blog/html_cache/$1.html -f 
RewriteRule ^blog/(article-\d+|index|sitemap.xml|search-\w+|rss-[0-9a-z]*)$ \ 
                 blog/html_cache/$1.html [last] 

# Anything else relating to the blog pass to index.php 
RewriteRule blog/(.*)         blog/index.php?page=$1 [qsappend,last] 

希望这会有所帮助。我的博客更详细地描述了这一点。 :-)

2

自从你问这个问题有一段时间了,但由于这仍然在收集搜索结果,我想我会给你一个更好的答案。

你可以在不使用.htaccess或其他技巧的PHP中进行静态缓存。我发现这个在http://simonwillison.net/2003/may/5/cachingwithphp/

<?php 
$cachefile = 'cache/index-cached.html'; 
$cachetime = 5 * 60; 
// Serve from the cache if it is younger than $cachetime 
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) { 
    include($cachefile); 
    echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n"; 
    exit; 
} 
ob_start(); // Start the output buffer 

/* The code to dynamically generate the page goes here */ 

// Cache the output to a file 
$fp = fopen($cachefile, 'w'); 
fwrite($fp, ob_get_contents()); 
fclose($fp); 
ob_end_flush(); // Send the output to the browser 
?> 
+1

这是_not_静态文件缓存,一个PHP进程仍然参与每个请求,它违背了静态文件缓存的全部要点。 – kizzx2 2013-02-24 16:30:39

2

只需添加更多一点尼科的响应,使之成为通用的复制和粘贴使用更加有用通过保存在保存每个文件单独求CacheFile名打字的时间。

原文:

$cachefile = 'cache/index-cached.html'; 

修改:

$cachefile = $_SERVER['DOCUMENT_ROOT'].'/cache/'.pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_FILENAME).'-cached.html'; 

这样做是采取一切文件,它位于的文件名,减去扩展名(在我的情况.PHP),和将“-cached.html”标签和新扩展附加到缓存文件。有可能更有效的方法来做到这一点,但它适用于我,并希望为他人节省一些时间和精力。