加速blog:服务器端的中转和缓存

前面已经提到了浏览器端的缓存,通过适当的Header可以建议和命令浏览器缓存页面内容比如javascript, css, 图片等。这里的服务器端的缓存又是什么意思呢?

现在很多网站都提供了API,提供比如JSON这样的数据方式以便在blog上引用。比如说picasaweb,我们可以用它来做一个picasa web album的站内镜像,但它的一个缺点就是慢,每次从Google服务器下载会json都会让我的blog停顿2到3秒钟,这让我很不爽。

但利用我的三线服务器,每次我需要数据的时候,我不是直接引用Google提供的js文件,而是通过我自己的服务器来中转这个数据,由于我自己的服务器无论同我自己还是同Google的服务器之间都有很好的速度,从而使得比我直接引用Google的文件还要快。

这便是服务器的中转。更具体的例子是Google Picasa Web提供给我显示相册列表的JSON地址为http://picasaweb.google.com/data/feed/base/user/$username?category=album&alt=json,我只需要像下面这样引用,便能拿到我的相册数据,

<script type="text/javascript" src="http://picasaweb.google.com/data/feed/base/user/mathzqy?category=album&alt=json&callback=showalbum">
</script>

然后我可以再写一个showalbum的js函数来控制相册的显示方式。但在实际中我并没有用这种方式,而是通过一个后台的PHP处理文件来中转这个文件,最后我是这样获取相册数据的:

<script type="text/javascript">
	document.write('<scrip' + 't type="text/javascript" src="feed.php?type=js&uri=' + encodeURIComponent('http://picasaweb.google.com/data/feed/base/user/mathzqy?category=album&alt=json&callback=showalbum') + '"></s' + 'cript>');
</script>

也就是说我向http://zhiqiang.org/feed.php索要所需要的js文件,feed.php在服务器上会从Google的服务器上下载所需要的文件,然后转发给我。由于服务器同Google的服务器之间的连接更快更稳定,所以这样比我直接向Google索要此文件还要快。

通过服务器中转的更大的好处是,我可以在服务器上缓存数据。这样不同的用户所要相同的文件时,服务器端可以直接从缓存里读取文件发送给用户,这样速度就更快了。缓存有两种形式,存到数据库或者缓存到文件里。著名的Twitter Tools便将数据缓存在数据库里,每1小时更新一次。当如果自己写的话,缓存到文件更方便。下面的feed.php为一个简单的服务器端中转和缓存处理代码(注意修改$cache_path和$snoopy_file路径):

<?php
  // feed.php, to cache and transfer data
  // example: feed.php?uri=http://zhiqiang.org/blog/feed&type=xml

  // the request uri  
  $rss_uri = stripslashes($_REQUEST['uri']);
  $rss_uri = str_replace(array('"', ' '), array('%22','%20'), $rss_uri);
  // the request file type
  $file_type  = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'xml';
  // the refresh time, control how much time the cache kept, 
  // default cache 1 hour.
  $refresh    = isset($_REQUEST['refresh']) ? $_REQUEST['refresh'] : 3600000;
  // the cache file directory, make sure the directory is writeable
  $cache_path = 'cache/';
  // this script need class snoopy.
  $snoopy_file = '/wp-includes/class-snoopy.php';

  // send HTTP header
  switch ($file_type){
    case 'css':
      header("Content-type: text/css");
      break;
    case 'js' :
      header("Content-type: application/x-javascript");
      break;
    case 'gif':
      header("Content-type: image/gif");
      break;
    case 'jpg':
      header("Content-type: image/jpeg");
      break;
    case 'png':
      header("Content-type: image/png");
      break;
    default:
      header("Content-type: text/plain");
  }

  $rss_hash = md5($rss_uri);
  $cache_rss_path = $cache_path.$rss_hash.'.'.$file_type;
  if ( !is_file($cache_rss_path) || time()-filemtime($cache_rss_path) > $refresh) {
    if (!class_exists('Snoopy')) require_once ($snoopy_file);

    $snoopy = new Snoopy;
    $snoopy->fetch($rss_uri);
    // this will copy the created tex-image to your cache-folder
    $cache_file = fopen($cache_rss_path, 'w');
    fputs($cache_file, $snoopy->results);
    fclose($cache_file);

    echo $snoopy->results;

  } else {
    $content = file_get_contents ($cache_rss_path);
    echo $content;
  }
?>

你可能感兴趣的
相关文章

板凳

(Required)
(Required, not published)

guest | 注册 | BBS | 管理 | English | 繁體

阅微堂

应该让别人的生活因为有了你的生存而更加美好。
Loading...
Loading...
Loading...