用PHP自动创建缓存文件 [英] Automatically create cache file with php

查看:228
本文介绍了用PHP自动创建缓存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在基于此链接



到目前为止,它对我想做的一切都很好。

  $ cachefile ='cache /'。 basename($ _ SERVER ['QUERY_STRING'])。 ‘.html’; 
$ cachetime = 1440 * 60;

if(file_exists($ cachefile)&&(time()-$ cachetime< filemtime($ cachefile))){
include($ cachefile);
echo<!-缓存的 .date(’jS F Y H:i’,filemtime($ cachefile))。-> ;;
出口;
}

ob_start();

//我的html / php代码在这里


$ fp = fopen($ cachefile,‘w’); //打开用于写入
fwrite($ fp,ob_get_contents())的缓存文件; //将输出缓冲区的内容保存到文件
fclose($ fp); //关闭
ob_end_flush(); //发送到浏览器

但是我有几页包含更详细的mysql查询,我已经花了优化它需要花费相当多的时间,但是当我在mysql中查询它并在网站上花费更长的时间时,它仍然需要大约10秒钟才能运行。有时我收到以下消息时似乎超时。

 代理服务器收到上游服务器的无效响应。 
代理服务器无法处理请求GET http://www.example.com

原因:从远程服务器


这不是一个大问题,因为我使用的缓存系统仅是当日第一位点击它的人得到了延迟,其余时间用户获取缓存页面的时间,因此对他们来说实际上是非常快的。



我想让自己不必每天成为第一个进入页面并使该过程自动化的人,所以每天17:00(在服务器上)



我如何最好地实现这一目标?

解决方案

我建议您使用 Php Speedy ,否则可能会有所帮助:

 <?php 
函数getUrl(){
if(!isset($ _ SERVER ['REQUEST_URI'])) {
$ url = $ _SERVER ['REQUEST_URI'];
}否则{
$ url = $ _SERVER [’SCRIPT_NAME’];
$ url。=(!empty($ _ SERVER [’QUERY_STRING']))? ’?’。 $ _SERVER [‘QUERY_STRING’]:’;

}
返回$ url;
}

// getUrl获取带有查询字符串的查询页面
函数缓存($ buffer){//页面的内容为$ buffer
$ url = getUrl( );
$ filename = md5($ url)。 ‘.cache’;
$ data = time()。 ‘¦’。 $ buffer;
$ filew = fopen( cache /。$ filename,'w');
fwrite($ filew,$ data);
fclose($ filew);
返回$ buffer;
}

函数display(){
$ url = getUrl();
$ filename = md5($ url)。 ‘.cache’;
if(!file_exists( / cache /。$ filename)){
返回false;
}
$ filer = fopen( cache /。$ filename,'r');
$ data = fread($ filer,filesize( cache /。$ filename));
fclose($ filer);
$ content = explode(’¦,$ data,2);
if(count($ content)!= 2 OR!is_numeric($ content ['0'])){
返回false;
}
if(time()-(100)> $ content [’0’]){// 100是这里的缓存时间!
返回false;
}
echo $ content ['1'];
die();
}

//显示缓存(如果有)
display(); //如果显示,则die函数将在此处结束程序。

//如果没有缓存,则回调缓存
ob_start(‘cache’);
?>

只需在需要缓存的任何位置包含此脚本,并设置cron作业即可自动运行。


I have been using a basic caching system on my site based on this link

It has so far worked well for everthing I want to do.

$cachefile = 'cache/'. basename($_SERVER['QUERY_STRING']) . '.html';
$cachetime = 1440 * 60; 

if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) {
  include($cachefile);
  echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->";
  exit;
}

ob_start();

// My html/php code here


$fp = fopen($cachefile, 'w'); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close
ob_end_flush(); // Send to browser

However I have a couple of pages with more detailed mysql queries, I have spent a fair bit of time optimising it however it still takes about 10 secs to run when I query it in mysql and even longer on the website. And sometimes it seems to time out as I get the below message.

The proxy server received an invalid response from an upstream server.
The proxy server could not handle the requestGET http://www.example.com

Reason: Error reading from remote server

This isn't a huge issue as because I am using the caching system above only the first person to click on it for the day gets the delay and the rest of the time the users get the cached page so it is actually quite fast for them.

I want to save myself from having to be the first person each day to go to the page and automate this process so at 17:00 (on the server) each day the file gets written to the cache.

How would I best achieve this?

解决方案

I suggest you to use Php Speedy or this may help:

<?php
function getUrl () {
    if (!isset($_SERVER['REQUEST_URI'])) {
    $url = $_SERVER['REQUEST_URI'];
    } else {
    $url = $_SERVER['SCRIPT_NAME'];
    $url .= (!empty($_SERVER['QUERY_STRING']))? '?' . $_SERVER[ 'QUERY_STRING' ] : '';

    }
    return $url;
}

//getUrl gets the queried page with query string
function cache ($buffer) { //page's content is $buffer
    $url = getUrl();
    $filename = md5($url) . '.cache';
    $data = time() . '¦' . $buffer;
    $filew = fopen("cache/" . $filename, 'w');
    fwrite($filew, $data);
    fclose($filew);
    return $buffer;
}

function display () {
    $url = getUrl();
    $filename = md5($url) . '.cache';
    if (!file_exists("/cache/" . $filename)) {
    return false;
    }
    $filer = fopen("cache/" . $filename, 'r');
    $data = fread($filer, filesize("cache/" . $filename));
    fclose($filer);
    $content = explode('¦', $data, 2);
    if (count($content)!= 2 OR !is_numeric($content['0'])) {
        return false;
    }
    if (time()-(100) > $content['0']) { // 100 is the cache time here!!!
        return false;
    }
        echo $content['1'];
        die();
}

// Display cache (if any)
display();  // if it is displayed, die function will end the program here.

// if no cache, callback cache
ob_start ('cache');
?>

Just include this script anywhere you need caching and set a cron job for running it automated.

这篇关于用PHP自动创建缓存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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