基于超简单静态文件的(html)php网站缓存 [英] Supersimple static file based (html) php site cache

查看:307
本文介绍了基于超简单静态文件的(html)php网站缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,该网站基本上只显示事物,而没有任何形式和获取信息。
该网站基于PHP,并托管在共享主机上。它很少改变。
我想为该网站启用缓存。
它的共享主机,所以我需要一个解决方案,

I have a website that basically only displays things without any forms and post-gets. This website is PHP based and hosted on shared hosting. It rarely changes. I would like to enable caching for this website. Its shared hosting so i need a solution that:


  • 不使用Memcached

  • 不要将我的网站移至VPS

  • 不要使用APC或其他东西

基本上,我想完成的工作是将每个子站点都缓存到HTML,并告诉PHP获得5分钟的当前子站点的HTML缓存版本并将其显示给用户。并在5分钟后刷新了缓存。

So basically what i would like to acomplish is cache every subsite to HTML and tell PHP to get for 5 minutes the HTML cached version of current subsite and display it to user. And after 5 minutes to refresh the cache.

我一直在互联网上寻找时间,并且有一些教程和框架支持这种古怪的缓存。

I've been looking for some while on the internets and there are some tutorials and frameworks that support this kind of kinky cache.

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

But what i need is just one good library that is extremely easy to use.

我想这样工作:

<?

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

..mywebsite rendering code

?>

听起来很简单,但我希望其他优秀的开发人员以前也做过这种库。那么,您知道这样易于使用,实现解决方案不是很费时吗?

So simple as it sounds but i hope some good fellow developer did library of this kind before. So do you know such ready to use, not very time consuming to implement solution?

推荐答案

这是我通常的做法,但是我不知道您的URL设计或目录/文件布局。

This is how I normally do this, however I don't know your URL design nor your directory / file layout.

我使用 .htaccess mod_rewrite ­文档

网络服务器检查是否存在缓存的HTML文件,如果存在,则将其交付。您还可以检查它的年龄。

The webserver checks if a cached HTML file exists, and if yes, it's delivered. You can also check it's age.

如果它太旧或不存在,则将启动PHP脚本。在脚本的开头,您启动输出缓冲区 ­ Docs 。在脚本末尾,您将获得输出缓冲区,然后将内容放入缓存文件中,然后将其输出。

If it's too old or if it does not exists your PHP script(s?) is started. At the beginning of your script you start the output buffer­Docs. At the end of your script, you obtain the output buffer and you place the content into the cache file and then you output it.

此解决方案的好处是如果静态文件存在,并且无需调用PHP进程,则apache将提供它们。如果您在PHP本身中做所有这些事情,那么您将没有任何好处。

The benefit of this solution is, that apache will deliver static files in case they exist and there is no need to invoke a PHP process. If you do it all within PHP itself, you won't have that benefit.

我什至可以更进一步,并运行cron-job来删除旧的缓存,文件而不是在 .htaccess 中进行时间检查。完成此操作后,您可以使重写变得不太复杂,而更喜欢使用 .php.cached 文件而不是 .php 文件

I would even go a step further and run a cron-job that removes older cache-files instead of doing a time-check inside the .htaccess. That done, you can make the rewrite less complex to prefer a .php.cached file instead of the .php file.

这篇关于基于超简单静态文件的(html)php网站缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆