0

0

安装Redis前端缓存的PHP脚本

php中文网

php中文网

发布时间:2016-07-28 08:27:57

|

1114人浏览过

|

来源于php中文网

原创

1、Redis前端缓存的PHP脚本来自:http://www.shenbogame.com.com/wordpress-with-redis-as-a-frontend-cache/ 
     for setup and configuration see more here:      www.jeedo.net/lightning-fast-wordpress-with-nginx-redis/      use this script at your own risk. i currently use this albeit a slightly modified version     to display a redis badge whenever a cache is displayed.  */  // change vars here $cf = 1;// set to 1 if you are using cloudflare $debug = 0;// set to 1 if you wish to see execution time and cache actions $display_powered_by_redis = 1;  // set to 1 if you want to display a powered by redis message with execution time, see below  $start = microtime();   // start timing page exec  // if cloudflare is enabled if ($cf) {     if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {         $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];     } }  // from wp define('WP_USE_THEMES', true);  // init predis include("predis.php"); $redis = new Predis\Client('');  // init vars $domain = $_SERVER['HTTP_HOST']; $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $url = str_replace('?r=y', '', $url); $url = str_replace('?c=y', '', $url); $dkey = md5($domain); $ukey = md5($url);  // check if page isn't a comment submission (isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0') ? $submit = 1 : $submit = 0;  // check if logged in to wp $cookie = var_export($_COOKIE, true); $loggedin = preg_match("/wordpress_logged_in/", $cookie);  // check if a cache of the page exists if ($redis->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 "

"; }  // 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教程有兴趣的朋友有所帮助。

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Rust内存安全机制与所有权模型深度实践
Rust内存安全机制与所有权模型深度实践

本专题围绕 Rust 语言核心特性展开,深入讲解所有权机制、借用规则、生命周期管理以及智能指针等关键概念。通过系统级开发案例,分析内存安全保障原理与零成本抽象优势,并结合并发场景讲解 Send 与 Sync 特性实现机制。帮助开发者真正理解 Rust 的设计哲学,掌握在高性能与安全性并重场景中的工程实践能力。

2

2026.03.05

PHP高性能API设计与Laravel服务架构实践
PHP高性能API设计与Laravel服务架构实践

本专题围绕 PHP 在现代 Web 后端开发中的高性能实践展开,重点讲解基于 Laravel 框架构建可扩展 API 服务的核心方法。内容涵盖路由与中间件机制、服务容器与依赖注入、接口版本管理、缓存策略设计以及队列异步处理方案。同时结合高并发场景,深入分析性能瓶颈定位与优化思路,帮助开发者构建稳定、高效、易维护的 PHP 后端服务体系。

56

2026.03.04

AI安装教程大全
AI安装教程大全

2026最全AI工具安装教程专题:包含各版本AI绘图、AI视频、智能办公软件的本地化部署手册。全篇零基础友好,附带最新模型下载地址、一键安装脚本及常见报错修复方案。每日更新,收藏这一篇就够了,让AI安装不再报错!

30

2026.03.04

Swift iOS架构设计与MVVM模式实战
Swift iOS架构设计与MVVM模式实战

本专题聚焦 Swift 在 iOS 应用架构设计中的实践,系统讲解 MVVM 模式的核心思想、数据绑定机制、模块拆分策略以及组件化开发方法。内容涵盖网络层封装、状态管理、依赖注入与性能优化技巧。通过完整项目案例,帮助开发者构建结构清晰、可维护性强的 iOS 应用架构体系。

59

2026.03.03

C++高性能网络编程与Reactor模型实践
C++高性能网络编程与Reactor模型实践

本专题围绕 C++ 在高性能网络服务开发中的应用展开,深入讲解 Socket 编程、多路复用机制、Reactor 模型设计原理以及线程池协作策略。内容涵盖 epoll 实现机制、内存管理优化、连接管理策略与高并发场景下的性能调优方法。通过构建高并发网络服务器实战案例,帮助开发者掌握 C++ 在底层系统与网络通信领域的核心技术。

25

2026.03.03

Golang 测试体系与代码质量保障:工程级可靠性建设
Golang 测试体系与代码质量保障:工程级可靠性建设

Go语言测试体系与代码质量保障聚焦于构建工程级可靠性系统。本专题深入解析Go的测试工具链(如go test)、单元测试、集成测试及端到端测试实践,结合代码覆盖率分析、静态代码扫描(如go vet)和动态分析工具,建立全链路质量监控机制。通过自动化测试框架、持续集成(CI)流水线配置及代码审查规范,实现测试用例管理、缺陷追踪与质量门禁控制,确保代码健壮性与可维护性,为高可靠性工程系统提供质量保障。

79

2026.02.28

Golang 工程化架构设计:可维护与可演进系统构建
Golang 工程化架构设计:可维护与可演进系统构建

Go语言工程化架构设计专注于构建高可维护性、可演进的企业级系统。本专题深入探讨Go项目的目录结构设计、模块划分、依赖管理等核心架构原则,涵盖微服务架构、领域驱动设计(DDD)在Go中的实践应用。通过实战案例解析接口抽象、错误处理、配置管理、日志监控等关键工程化技术,帮助开发者掌握构建稳定、可扩展Go应用的最佳实践方法。

61

2026.02.28

Golang 性能分析与运行时机制:构建高性能程序
Golang 性能分析与运行时机制:构建高性能程序

Go语言以其高效的并发模型和优异的性能表现广泛应用于高并发、高性能场景。其运行时机制包括 Goroutine 调度、内存管理、垃圾回收等方面,深入理解这些机制有助于编写更高效稳定的程序。本专题将系统讲解 Golang 的性能分析工具使用、常见性能瓶颈定位及优化策略,并结合实际案例剖析 Go 程序的运行时行为,帮助开发者掌握构建高性能应用的关键技能。

50

2026.02.28

Golang 并发编程模型与工程实践:从语言特性到系统性能
Golang 并发编程模型与工程实践:从语言特性到系统性能

本专题系统讲解 Golang 并发编程模型,从语言级特性出发,深入理解 goroutine、channel 与调度机制。结合工程实践,分析并发设计模式、性能瓶颈与资源控制策略,帮助将并发能力有效转化为稳定、可扩展的系统性能优势。

47

2026.02.27

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与SOCKET
进程与SOCKET

共6课时 | 0.4万人学习

Redis+MySQL数据库面试教程
Redis+MySQL数据库面试教程

共72课时 | 7.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号