最后,本文由安逸手打,资料由安逸一点一点收集.
最后聚合而成。
转载请注明出处.谢谢
redis很久之前就有用过,但是感觉是没有Memcached的好用。于是就换了,听群里的人说redis是怎样怎样的好,看了下redis的确更新了好多版本于是重新来试试。
0.介绍
Redis是个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set –有序集合)和hash(哈希类型)。
Redis是个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value型NoSQL数据库。凭借自身丰富的API、超高的性能以及支持持久化存储等特点,Redis成为数据库缓存领域的优质选择。
首先你需要可以简单比较的数据,因为我专业性不是很强,属于娱乐记者哈,随便点了
1.你需要简单比较的数据
记录没启用redis时使用的加载时间
2.然后启用redis缓存再比较
这里我是使用宝塔面板的所以配置页面非常简便.
其实 我推荐amh面板 6元一个月付费版.宝塔喜欢把% 百分号自动转换成乱码。
如果你不是使用宝塔的.我也做了一些功课,大家可以看下以下网址,他教你怎么手动安装配置redis
使用Redis对象缓存加速WordPress
http://www.rainng.com/wordpress-redis/
使用Redis Object Cache加速WordPress
https://lala.im/2840.html
Redis为wordpress提供闪存级加速体验 绝佳的缓存加速方案
https://wanvi.net/9023.html
Wordpress使用Redis缓存加速|511遇见强烈推荐
http://www.511yj.com/wordpress-redis-apache.html
使用 Redis 做缓存为 wordpress 加速
https://blog.itnmg.net/2015/04/20/wordpress-redis-cache/
3.插件版安装(推荐)
后台搜索Redis Object Cache
然后启用.以下这个代表已经成功了、而www.an1.org这个是我自己添加的区分别的网站
(如果你一台服务器有多个网站都启用redis缓存的话)我建议你加上,naxTTl应该是网站缓存刷新时间
怎么加?请继续往下看
Redis Object Cache插件的个性化配置:
*通过修改Wordpress的设置文件wp-config.php,添加修改Redis Object Cache的配置。
(1)WP_REDIS_CLIENT (default: not set) 指定用于与Redis通信的客户端;
(2)WP_REDIS_SCHEME (default: tcp) 指定用于与Redis实例进行通信的协议。
(3)WP_REDIS_HOST (default: 127.0.0.1) 目标服务器的IP或主机名。
(4)WP_REDIS_PORT 目标服务器的TCP / IP端口。
(5)WP_REDIS_DATABASE (default: 0) 接受用于使用该SELECT命令自动选择逻辑数据库的数值。
(6)P_REDIS_PASSWORD (default: not set) 接受用于使用该AUTH命令的受密码保护的Redis服务器进行身份验证的值。
(7)WP_CACHE_KEY_SALT (default: not set)设置所有缓存键的前缀。(Wordpress多站点。)
*配置用法举例:
define('WP_REDIS_CLIENT', 'pecl'); define('WP_REDIS_SCHEME', 'tcp'); define('WP_REDIS_HOST', '$Your.Redis.IP'); define('WP_REDIS_DATABASE', '0'); define('WP_CACHE_KEY_SALT', 'www.an1.org'); define('WP_REDIS_MAXTTL', '86400');
该资料来源:https://www.skyoy.com/wordpress-redis-object-cache.html
这里的$Your.Redis.IP我建议你改成127.0.0.1,网站域名标识符改下,其他默认即可
4.代码版(也超级简单的)
这个是安逸最早之前使用的版本哦
使用 Predis 作为 Redis 的 PHP 客户端
我们需要一个程序作为redis的客户端,可以理解为服务器的中间文件,将php与redis相互链接。这里推荐predis,一个非常不错的开源程序。
项目地址:https://packagist.org/packages/predis/predis
把predis.php放到wordpress根目录下
前端缓存的 PHP 脚本
步骤1: 在 WordPress 的根目录创建新文件 index-with-redis.php ,内容如下:
- <?php
- // Change these two variables:
- $seconds_of_caching = 60*60*24*7; // 7 days.
- $ip_of_this_website = ‘204.62.14.112’;
- /*
- – This file is written by Jim Westergren, copyright all rights reserved.
- – See more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
- – The code is free for everyone to use how they want but please mention my name and link to my article when writing about this.
- – Change $ip_of_this_website to the IP of your website above.
- – Add ?refresh=yes to the end of a URL to refresh it’s cache
- – You can also enter the redis client via the command prompt with the command “redis-cli” and then remove all cache with the command “flushdb”.
- */
- // Very necessary if you use Cloudfare:
- if (isset($_SERVER[‘HTTP_CF_CONNECTING_IP’])) {
- $_SERVER[‘REMOTE_ADDR’] = $_SERVER[‘HTTP_CF_CONNECTING_IP’];
- }
- // This is from WordPress:
- define(‘WP_USE_THEMES’, true);
- // Start the timer:
- function getmicrotime($t) {
- list($usec, $sec) = explode(” “,$t);
- return ((float)$usec + (float)$sec);
- }
- $start = microtime();
- // Initiate redis and the PHP client for redis:
- include(“predis.php”);
- $redis = new Predis\Client(”);
- // few variables:
- $current_page_url = “http://”.$_SERVER[‘HTTP_HOST’].$_SERVER[‘REQUEST_URI’];
- $current_page_url = str_replace(‘?refresh=yes’, ”, $current_page_url);
- $redis_key = md5($current_page_url);
- // This first case is either manual refresh cache by adding ?refresh=yes after the URL or somebody posting a comment
- if (isset($_GET[‘refresh’]) || substr($_SERVER[‘REQUEST_URI’], -12) == ‘?refresh=yes’ || ($_SERVER[‘HTTP_REFERER’] == $current_page_url && $_SERVER[‘REQUEST_URI’] != ‘/’ && $_SERVER[‘REMOTE_ADDR’] != $ip_of_this_website)) {
- require(‘./wp-blog-header.php’);
- $redis->del($redis_key);
- // Second case: cache exist in redis, let’s display it
- } else if ($redis->exists($redis_key)) {
- $html_of_current_page = $redis->get($redis_key);
- echo $html_of_current_page;
- echo “<!– This is cache –>”;
- // third: a normal visitor without cache. And do not cache a preview page from the wp-admin:
- } else if ($_SERVER[‘REMOTE_ADDR’] != $ip_of_this_website && strstr($current_page_url, ‘preview=true’) == false) {
- require(‘./wp-blog-header.php’);
- $html_of_current_page = file_get_contents($current_page_url);
- $redis->setex($redis_key, $seconds_of_caching, $html_of_current_page);
- echo “<!– Cache has been set –>”;
- // last case: the normal WordPress. Should only be called with file_get_contents:
- } else {
- require(‘./wp-blog-header.php’);
- }
- // Let’s display some page generation time (note: CloudFlare may strip out comments):
- $end = microtime();
- $t2 = (getmicrotime($end) – getmicrotime($start));
- if ($_SERVER[‘REMOTE_ADDR’] != $ip_of_this_website) {
- echo “<!– Cache system by Jim Westergren. Page generated in “.round($t2,5).” seconds. –>”;
- }
- ?>
步骤2:将上述代码中的 IP 地址替换成你网站的 IP 地址
步骤3:在 .htaccess 中将所有出现 index.php 的地方改为 index-with-redis.php ,如果你使用的是 Nginx 则修改 nginx.conf 中的 index.php 为 index-with-redis.php(并重载 Nginx : killall -s HUP nginx)。
部分资料来自:https://wanvi.net/9023.html
5.PHP代码版2
直接去https://raw.githubusercontent.com/alleyinteractive/wp-redis/master/object-cache.php
复制即可、
WordPress Object Cache
cd /data/wwwroot/example.com/wp-content
wget https://raw.githubusercontent.com/alleyinteractive/wp-redis/master/object-cache.php
chown www-data: object-cache.php
WordPress Cache Salt/Prefix
如果你有多个站点的话,可以加盐(给你的cache加个属于你自己特定的值,让缓存不发生冲突)
在 wp-config.php文件最底下加入
define('WP_CACHE', true);
define('WP_CACHE_KEY_SALT', 'example.com/subdir');
来源:https://www.49gd.com/2422.html
最后,本文由安逸手打,资料由安逸一点一点收集.
最后聚合而成。
转载请注明出处.谢谢
评论抢沙发