Supersimple static file based (html) php site cache

前端 未结 5 1126
长情又很酷
长情又很酷 2021-02-09 21:59

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

5条回答
  •  萌比男神i
    2021-02-09 22:38

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

    I do this with .htaccess and a mod_rewrite­Docs.

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

    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.

    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.

    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.

提交回复
热议问题