This tutorial will guide you through installing the Memcache caching service on CentOS. Memcache is a high-performance, memory-based caching plugin compatible with PHP. It can cache not only variables and objects but also work alongside MySQL to cache database queries. Since Memcache stores data in memory, it delivers blazing-fast read and write speeds, providing high-speed caching for large volumes of rapidly changing dynamic data.

Because compiling and installing Memcache from source is relatively complex, this article will use the simpler approach of installing Memcache directly via yum on a CentOS system.
1. Since the default CentOS repositories do not include the memcache package, you need to import a third-party repository. Run the following two commands:
Note: Most tutorials online blindly suggest using the RPMForge repository for yum. However, after testing by VPS Management Encyclopedia, that repository does not contain the memcached package, so it will not work. Simply follow the repository and method provided by VPS Management Encyclopedia for a successful installation.
2. Check the installed repositories
02 |
Loaded plugins: fastestmirror |
03 |
Loading mirror speeds from cached hostfile |
04 |
* base: centos.ustc.edu.cn |
05 |
* epel: mirrors.ustc.edu.cn |
06 |
* extras: centos.ustc.edu.cn |
07 |
* rpmforge: fr2.rpmfind.net |
08 |
* updates: centos.ustc.edu.cn |
09 |
repo id repo name status |
10 |
base CentOS-5 - Base 2,705 |
11 |
epel Extra Packages for Enterprise Linux 5 - i386 5,579 |
12 |
extras CentOS-5 - Extras 282 |
13 |
updates CentOS-5 - Updates 455 |
If you can see the epel repository listed, the installation was successful.
3. Install the Memcache server and PHP extension via yum
Both packages should now install successfully without any “not found” errors.
4. After installation, check if PHP has correctly loaded the memcache module:
If “memcache” is returned, it indicates the installation was successful.
5. Configure memcached to start automatically on boot
6. Start the memcached service and restart Apache
2 |
Starting memcached: [OK] |
7. Test if PHP Memcache Support is Working
Create a file named memcache.php in the Apache document root:
Content as follows:
2 |
$memcache = new Memcache(); |
3 |
$memcache->connect('127.0.0.1', 11211); |
4 |
$memcache->set('key', 'Memcache test successful!', 0, 60); |
5 |
$result = $memcache->get('key'); |
If everything is normal, visiting this page should return “Memcache test successful”. At this point, Memcached and the PHP extension memcache have been successfully installed.
The default port for Memcached is 11211, so just use this port in PHP. Below is a handy method to clear all Memcached cache content:
Execute:
Then input:
That’s it.
Source: http://www.bootf.com/442.html