您好,欢迎访问一九零五行业门户网

安装Redis前端缓存的PHP脚本

1、redis前端缓存的php脚本来自:http://www.shenbogame.com.com/wordpress-with-redis-as-a-frontend-cache/ 
hexists($dkey, $ukey) && !$loggedin && !$submit && !strpos($url, '/feed/')) {      echo $redis->hget($dkey, $ukey);     $cached = 1;     $msg = 'this is a cache';  // if a comment was submitted or clear page cache request was made delete cache of page } else if ($submit || substr($_server['request_uri'], -4) == '?r=y') {      require('./wp-blog-header.php');     $redis->hdel($dkey, $ukey);     $msg = 'cache of page deleted';  // delete entire cache, works only if logged in } else if ($loggedin && substr($_server['request_uri'], -4) == '?c=y') {      require('./wp-blog-header.php');     if ($redis->exists($dkey)) {         $redis->del($dkey);         $msg = 'domain cache flushed';     } else {         $msg = 'no cache to flush';     }  // if logged in don't cache anything } else if ($loggedin) {      require('./wp-blog-header.php');     $msg = 'not cached';  // cache the page } else {      // turn on output buffering     ob_start();      require('./wp-blog-header.php');      // get contents of output buffer     $html = ob_get_contents();      // clean output buffer     ob_end_clean();     echo $html;      // store to cache only if the page exist and is not a search result.     if (!is_404() && !is_search()) {         // store html contents to redis cache         $redis->hset($dkey, $ukey, $html);         $msg = 'cache is set';     } }  $end = microtime(); // get end execution time  // show messages if debug is enabled if ($debug) {     echo $msg.': ';     echo t_exec($start, $end); }  if ($cached && $display_powered_by_redis) { // you should move this css to your css file and change the: float:right;margin:20px 0;echo ;echo page generated in
.t_exec($start, $end). sec
; }  // time diff function t_exec($start, $end) {     $t = (getmicrotime($end) - getmicrotime($start));     return round($t,5); }  // get time function getmicrotime($t) {     list($usec, $sec) = explode( ,$t);     return ((float)$usec + (float)$sec); }  ?>
2、 你也可以直接点击备用下载:index-with-redis.php下载地址。 github项目:https://gist.github.com/jimwestergren/3053250#file-index-with-redis-php 
3、 如果你正在使用cloudflare,请设置cf = 1; ,如果你想在页面上看到脚本执行时间和缓存加载时间,请设置$debug = 1; display_powered_by_redis = 1表示显示powered_by信息。
4、将index-with-redis.php上传到wordpress的根目录,如果你使用的是nginx,重命令原来的index.php为任意其它名字,把index-with-redis.php重命名为index.php。
5、如果你使用的是apache,则需要把.htaccess中出现的index.php替换成index-with-redis.php。
6、所有的操作完成后,你就可以刷新一下wordpress页面,查看redis缓存效果了。 
7、实际使用过程中发现以上代码会出现wordpress首页和分类没有及时缓存,这里再给出优化版本,出自http://www.88shenbogame.com/lightning-fast-wordpress-with-nginx-redis/。 
8、功能差不多,主要有:登录时页面不缓存、除非删除或者重置否则不删除缓存页面、登录时在任意url后加上?c=y可以删除整个网站缓存、在任意url后面加上?c=y可以清除此url缓存、allow_fopen被禁止也可以正常运行、发表评论时删除该页面缓存。 
9、index-with-redis.php优化版本的源码是: 
hexists($dkey, $ukey) && !$loggedin && !$submit) {     echo $redis->hget($dkey, $ukey);     if (!$debug) exit(0);     $msg = 'this is a cache'; // if a comment was submitted or clear page cache request was made delete cache of page } else if ($submit || substr($_server['request_uri'], -4) == '?r=y') {     require('./wp-blog-header.php');     $redis->hdel($dkey, $ukey);     $msg = 'cache of page deleted'; // delete entire cache, works only if logged in } else if ($loggedin && substr($_server['request_uri'], -4) == '?c=y') {     require('./wp-blog-header.php');     if ($redis->exists($dkey)) {         $redis->del($dkey);         $msg = 'domain cache flushed';     } else {         $msg = 'no cache to flush';     } // if logged in don't cache anything } else if ($loggedin) {     require('./wp-blog-header.php');     $msg = 'not cached'; // cache the page } else {     // turn on output buffering     ob_start();     require('./wp-blog-header.php');     // get contents of output buffer     $html = ob_get_contents();     // clean output buffer     ob_end_clean();     echo $html;     // store html contents to redis cache     $redis->hset($dkey, $ukey, $html);     $msg = 'cache is set'; } $end = microtime(); // get end execution time // show messages if debug is enabled if ($debug) {     echo $msg.': ';     echo t_exec($start, $end); } // time diff function t_exec($start, $end) {     $t = (getmicrotime($end) - getmicrotime($start));     return round($t,5); } // get time function getmicrotime($t) {     list($usec, $sec) = explode( ,$t);     return ((float)$usec + (float)$sec); } ?>
以上就介绍了 安装redis前端缓存的php脚本,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息