这个插件其实是我从bo-blog官方论坛上看来的,放在这里跟大家共享,算是借花献佛了。

这款插件可以应用于任何开通了rewrite和gizp功能的php网站,主要作用是将网站的css和java scripts进行缓存,这样下次登录的时候,就不必重复调用,直接读取缓存即可,经过我的测试,可以极大的提高博客的访问速度,非常实用。


引用

转载内容开始:
声明:原版版权属于http://www.cbmland.com/的CBM(Mr/Miss)。
这是是在原版的技术上加强版的缓存插件,主要缓存css,js,图片。
没有缓存页面,这个有空再加上吧。

现在的页面一般采用Div+Css的形式,页面不大,css和Js占了很大的比重,因此把这部分压缩传送就可以大大加快页面的打开速度,现在99%以上的浏览器支持压缩,所以为这个提供了可行性。
说明:
1,在服务器缓存了压缩过的文件,再次访问减少再压缩时间,降低CPU占用率。
2,通过设置客户端文件缓存时间,降低再次请求次数,可降低85%以上。
3,图片因为已经是压缩格式,只是设置客户端缓存时间,不做压缩处理。

使用方法:
1,服务器必须支持gzip,Rewrite功能。
2,在。htacess文件的“RewriteBase /”下面一行添加
RewriteRule (.*.css$|.*.js$|.*.jpg$|.*.gif$|.*.png$) gzip.php?$1 [L]
3,上传gzip.php到根目录
4,在根目录建cache文件夹,保证可读写。
OK,enjoy,再次感谢Cbm。



下面是gzip.php,大家把源代码给保存为gzip.php,放到网站根目录就可以了。



<?php
// <!-- 公共的返回header的子程序 -->
function sendheader($last_modified, $p_type, $content_length = 0) {
  // 设置客户端缓存有效时间
  header("Expires: " . gmdate("D, d M Y H:i:s", time() + 15360000) . "GMT");
  header("Cache-Control: max-age=315360000");
  header("Pragma: ");
  // 设置最后修改时间
  header("Last-Modified: " . $last_modified);
  // 设置文件类型信息
  header($p_type);
  header("Content-Length: " . $content_length);
}

define('ABSPATH', dirname(__FILE__) . '/');

$cache = true;
$cachedir = 'cache/'; //存放gz文件的目录,确保可写

if (empty($_SERVER['QUERY_STRING'])) exit();

$gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
if (empty($gzip)) $cache = false;

$key = array_shift(explode('?', $_SERVER['QUERY_STRING']));
$key = str_replace('../', '', $key);

$filename = ABSPATH . $key;

$symbol = '_';

$rel_path = str_replace(ABSPATH, '', dirname($filename));
$namespace = str_replace('/', $symbol, $rel_path);

$cache_filename = ABSPATH . $cachedir . $namespace . $symbol . basename($filename) . '.gz'; //生成gz文件路径

$ext = array_pop(explode('.', $filename)); //根据后缀判断文件类型信息

$type = "Content-type: text/html"; //默认的文件类型
switch ($ext) {
  case 'css':
    $type = "Content-type: text/css";
    break;
  case 'js':
    $type = "Content-type: text/javascript";
    break;
  case 'gif':
    $cache = false;
    $type = "Content-type: image/gif";
    break;
  case 'jpg':
    $cache = false;
    $type = "Content-type: image/jpeg";
    break;
  case 'png':
    $cache = false;
    $type = "Content-type: image/png";
    break;
  default:
    exit();
}

if ($cache) {
  if (file_exists($cache_filename)) { // 假如存在gz文件
    $mtime = filemtime($cache_filename);
    $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';

    if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
          array_shift(explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) == $gmt_mtime)
        ) {
      // 与浏览器cache中的文件修改日期一致,返回304
      header ("HTTP/1.1 304 Not Modified");
      // 发送客户端header
      header("Content-Encoding :gzip");
      sendheader($gmt_mtime, $type);
    } else {
      // 读取gz文件输出
      $content = file_get_contents($cache_filename);
      // 发送客户端header
      sendheader($gmt_mtime, $type, strlen($content));
      header("Content-Encoding: gzip");
      // 发送数据
      echo $content;
    }
  } else if (file_exists($filename)) { // 没有对应的gz文件
    $mtime = mktime();
    $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
    // 读取文件
    $content = file_get_contents($filename);
    // 去掉空白的部分
    // $content = ltrim($content);
    // 压缩文件内容
    $content = gzencode($content, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
    // 发送客户端header
    sendheader($gmt_mtime, $type, strlen($content));
    header("Content-Encoding: gzip");
    // 发送数据
    echo $content;
    // 写入文件
    file_put_contents($cache_filename, $content);
  } else {
    header("HTTP/1.0 404 Not Found");
  }
} else { // 处理不使用Gzip模式下的输出。原理基本同上
  if (file_exists($filename)) {
    $mtime = filemtime($filename);
    $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';

    if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
          array_shift(explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) == $gmt_mtime)
        ) {
      // 与浏览器cache中的文件修改日期一致,返回304
      header ("HTTP/1.1 304 Not Modified");
      // 发送客户端header
      sendheader($gmt_mtime, $type, strlen($content));
      header("Content-Encoding :gzip");
    } else {
      // 读取文件输出
      $content = file_get_contents($filename);
      // 发送客户端header
      sendheader($gmt_mtime, $type, strlen($content));
      // 发送数据
      echo $content;
    }
  } else {
    header("HTTP/1.0 404 Not Found");
  }
}

?>


汇率实时转换——点 击进入在线汇率换算工 具

作者:mlzy@牧龙在野!
地址:http://www.mlzy.net/post/978/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!



< Tags: , , , , >

1 comment(s)
zengbo
[2008/12/20 20:22]
不需要给 gzip.php 可执行权限。
我们网站我已经配置过 css 和 Js ,让浏览器进行缓存了。
这个工具用处不大,就是起到一个gzip压缩的作用。
不需要可执行权限就可以啊?!~ 那我得去改了。。。我妹妹那边都是777,汗。。
mlzy 回复于 2008/12/20 20:25
分页: 1/1 第一页 1 最后页

发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 不区分大小写