memcached是什么?
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。
memcache又是什么?
memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问速度提升效果十分显著。
最简单直白的理解:memcached是memcache的强化版本!
所以,我们本篇主要说的是:使用memcached来加速wordpress网站!
我们一般使用比较简单的直接yum安装了
#Centos直接使用yum安装即可,其他系统自行搜索安装命令,比如ubuntu
yum -y install memcached
#启动memcached
service memcached start
#开机启动
chkconfig memcached on
然后集成php-memcached拓展,这些教程百度千篇一律,都有,本文不作介绍。正确安装完成后,服务器就支持环境就已经支持
memcached缓存了。
下面我们在WordPress中怎么来使用这个神器:
1、安装插件
一定要访问github项目页面下载插件包(一定要这个插件!!!):
https://github.com/tollmanz/wordpress-pecl-memcached-object-cache
下载并解压得到的 object-cache.php,上传到 wp-content 目录即可开启memcached缓存。
WordPress官网上的object-cache.php虽然也号称Memcached 插件,然而它只支持Memcache,不支持Memcached,所以不能使用。如果错误地将object-cache.php和Memcached混用的话,则会出现WordPress打不开,前台后台页面一片空白的现象。
这也就是经常有人反馈WordPress启用memcached功能后,页面空白,memcached 500错误的错误原因了。所以,我们最重要的是
memcached插件一定要使用对。
- Now that the server dependencies are resolved, the rest of the configuration is in the WordPress application. Take the object-cache.php file and place it in your wp-content folder. For instance, if the root of your WordPress installation is at /srv/www/wordpress, the location of object-cache.php would be /srv/www/wordpress/wp-content/object-cache.php. Please note that object-cache.php is a WordPress drop-in. It is not a regular plugin or an MU plugin. Drop-ins are located directly in the wp-content folder.
- Add the following to your wp-config.php file:
global $memcached_servers; $memcached_servers = array( array( '127.0.0.1', // Memcached server IP address 11211 // Memcached server port ) );
If your Memcached server is located on a different server or port, adjust those values as needed. If you have multiple Memcached instances, add additional servers to the array:
global $memcached_servers; $memcached_servers = array( array( '1.2.3.4', 11211 ), array( '1.2.3.5', 11211 ) );
- To test the WordPress object cache setup, add the following code as an MU plugin:
<?php $key = 'dummy'; $value = '100'; $dummy_value = wp_cache_get( $key ); if ( $value !== $dummy_value ) { echo "The dummy value is not in cache. Adding the value now."; wp_cache_set( $key, $value ); } else { echo "Value is " . $dummy_value . ". The WordPress Memcached Backend is working!"; }
After adding the code, reload your WordPress site twice. On the second load, you should see a success message printed at the top of the page. Remove the MU plugin after you’ve verified the setup.
Authors
- Zack Tollman
- 10up