一、安装redis:
0 1 2 3 4 5 6 7 8 9 10 |
wget http://download.redis.io/releases/redis-3.2.0.tar.gz tar -zxf redis-3.2.0.tar.gz cd redis-3.2.0/ make MALLOC=jemalloc make PREFIX=/byrd/service/redis-3.2.0 install ln -s /byrd/service/redis-3.2.0 /usr/local/redis mkdir /usr/local/redis/etc cp redis.conf /usr/local/redis/etc/ sysctl -w vm.overcommit_memory=1 echo "512" > /proc/sys/net/core/somaxconn /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf & |
二、调用Predis:
I recommend Predis. Upload predis.php to the WordPress root directory. See more about Predis on Github (if you use a newer version of Predis you need also to modify the script below).SourceByrd's Weblog-https://note.t4x.org/environment/wordpress-redis-MySQL/
点击下载Predis.php代码
链接: http://pan.baidu.com/s/1i4Ie2Tf 密码: a8mn
SourceByrd's Weblog-https://note.t4x.org/environment/wordpress-redis-mysql/
三、Installing the PHP script for the Frontend Cache:
展示index-with-redis.php代码
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
<?php /* Author: Jim Westergren & Jeedo Aquino File: index-with-redis.php Updated: 2012-10-25 This is a redis caching system for wordpress. see more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/ Originally written by Jim Westergren but improved by Jeedo Aquino. some caching mechanics are different from jim's script which is summarized below: - cached pages do not expire not unless explicitly deleted or reset - appending a ?c=y to a url deletes the entire cache of the domain, only works when you are logged in - appending a ?r=y to a url deletes the cache of that url - submitting a comment deletes the cache of that page - refreshing (f5) a page deletes the cache of that page - includes a debug mode, stats are displayed at the bottom most part after </html> 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 "<style>#redis_powered{float:right;margin:20px 0;background:url(http://images.staticjw.com/jim/3959/redis.png) 10px no-repeat #fff;border:1px solid #D7D8DF;padding:10px;width:190px;} #redis_powered div{width:190px;text-align:right;font:10px/11px arial,sans-serif;color:#000;}</style>"; echo "<a href=\"https://note.t4x.org/\" style=\"text-decoration:none;\"><div id=\"redis_powered\"><div>Page generated in<br/> ".t_exec($start, $end)." sec</div></div></a>"; } // 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); } ?> |
SourceByrd's Weblog-https://note.t4x.org/environment/wordpress-redis-mysql/
四、执行步骤
①:安装+运行redis;
②:下载predis.php到wordpress根目录;
③:index-with-redis.php改名为index.php;
④:链接: http://pan.baidu.com/s/1i4Ie2Tf 密码: a8mn SourceByrd's Weblog-https://note.t4x.org/environment/wordpress-redis-mysql/
五、结果对比
未安装:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[root@Test ~]# siege -t 1m -c 100 http://blog.t4x.org/project/centos-install-extmail/ ** SIEGE 3.1.3 ** Preparing 100 concurrent users for battle. The server is now under siege... HTTP/1.1 200 2.18 secs: 201035 bytes ==> GET /project/centos-install-extmail/ HTTP/1.1 200 2.34 secs: 201038 bytes ==> GET /project/centos-install-extmail/ HTTP/1.1 200 44.08 secs: 202281 bytes ==> GET /project/centos-install-extmail/ HTTP/1.1 200 43.93 secs: 202281 bytes ==> GET /project/centos-install-extmail/ Lifting the server siege... done. Transactions: 131 hits Availability: 100.00 % Elapsed time: 59.62 secs Data transferred: 25.15 MB Response time: 27.93 secs Transaction rate: 2.20 trans/sec Throughput: 0.42 MB/sec Concurrency: 61.36 Successful transactions: 131 Failed transactions: 0 Longest transaction: 45.13 Shortest transaction: 1.44 [root@Web3 ~]# cat /var/log/mysqld.log | wc -l 22594 |
安装后:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[root@Test ~]# siege -t 1m -c 100 http://blog.t4x.org/project/centos-install-extmail/ ** SIEGE 3.1.3 ** Preparing 100 concurrent users for battle. The server is now under siege... HTTP/1.1 200 0.11 secs: 201224 bytes ==> GET /project/centos-install-extmail/ HTTP/1.1 200 0.12 secs: 201224 bytes ==> GET /project/centos-install-extmail/ HTTP/1.1 200 0.80 secs: 201224 bytes ==> GET /project/centos-install-extmail/ HTTP/1.1 200 0.78 secs: 201224 bytes ==> GET /project/centos-install-extmail/ Lifting the server siege... done. Transactions: 4022 hits Availability: 100.00 % Elapsed time: 59.57 secs Data transferred: 771.83 MB Response time: 0.97 secs Transaction rate: 67.52 trans/sec Throughput: 12.96 MB/sec Concurrency: 65.65 Successful transactions: 4022 Failed transactions: 0 Longest transaction: 4.01 Shortest transaction: 0.73 [root@Web2 ~]# cat /var/log/mysqld.log |wc -l 543 |
六、测试说明
未开启redis的时候,每次请求网站的时候都会请求mysql,而开启redis后,第一次请求会请求到后端的mysql,以后的内容都缓存到了redis中。 SourceByrd's Weblog-https://note.t4x.org/environment/wordpress-redis-mysql/
参考文档:
1:https://www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
2:http://www.jeedo.net/lightning-fast-wordpress-with-nginx-redis/SourceByrd's Weblog-https://note.t4x.org/environment/wordpress-redis-mysql/
SourceByrd's Weblog-https://note.t4x.org/environment/wordpress-redis-mysql/