Home  >  Article  >  Backend Development  >  PHP script to install Redis front-end cache

PHP script to install Redis front-end cache

WBOY
WBOYOriginal
2016-07-28 08:27:571021browse

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 PredisClient('');  // 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. You can also directly click on the backup download :index-with-redis.php download address. Github project: https://gist.github.com/JimWestergren/3053250#file-index-with-redis-php
3. If you are using cloudflare, please set cf = 1; if you want to see it on the page For script execution time and cache loading time, please set $debug = 1; display_powered_by_redis = 1 means to display powered_by information.

4. Upload index-with-redis.php to the root directory of WordPress. If you are using nginx, rename the original index.php to any other name and rename index-with-redis.php to index. .php.

5. If you are using Apache, you need to replace index.php that appears in .htaccess with index-with-redis.php.

6. After all operations are completed, you can refresh the WordPress page to check the Redis cache effect.
7. During actual use, it is found that the above code will cause the WordPress homepage and categories to not be cached in time. Here is an optimized version, from http://www.88shenbogame.com/lightning-fast-wordpress-with-nginx-redis/ .
8. The functions are similar. The main ones are: the page is not cached when logging in. The cached page is not deleted unless deleted or reset. When logging in, add ?c=y after any URL to delete the entire website cache. Add after any URL. ?c=y can clear this URL cache, allow_fopen can run normally even if it is disabled, and delete the page cache when leaving a comment. 
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教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn