What is Memcached?
Memcached is a distributed memory caching system just like the name suggests. This general purpose caching system was originally developed by Danga Interactive for LiveJournal (a blog hosting site). It is now used by many in order to speed up their dynamic database driven websites by caching objects and data into memory. This reduces the amount of times the database needs to be queried and saves on resources.
When to use Memcached?
Memcached should ideally be used on resource intensive websites. A lot of people have asked the question about having Memcached installed on shared hosting. If your website is starting to consume resources that go beyond the scope of shared hosting then Memcached isn’t going to save your site. It’s likely it’s going to need to be upgraded from shared to VPS hosting. Standard file caching methods should be used on shared, and if it’s still not enough then you likely need to upgrade. Also, shared hosting accounts will typically not allow Memcached as it lacks authentication, meaning other users on the same shared hosting could potentially retrieve your Memcached data if they have the correct keys.
This is because Memcached lacks security features and authentication. Therefore it should only be utilized on servers with a properly configured firewall. VPS and Dedicated hosts allow you to configure your firewall which is another reason they are better suited, as opposed to shared.
If you do have a VPS and want to use Memcached, then you’re likely going to require a VPS with high enough memory limitation as Memcached can use a lot of memory on a large site, otherwise you might be best served by with an alternative caching method.
Installing Memcached on a VPS Server
File and program versions may have become outdated and/or there are just better ones you should use. Best to find the latest and not use the ones in this guide. The methods required and may differ depending on your server setup and the flavor/version of Linux being used. If in doubt it might be best to ask your server administrator for assistance or to install Memcached for you.
tar xfz libevent-1.4.8-stable.tar.gz
sudo make install
sudo ln -s /usr/local/lib/libevent-1.4.so.2 /usr/lib
tar xfz memcached-1.2.6.tar.gz
sudo make install
memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211
Install Memcached PHP Extension
In your accounts home directory edit your ‘.profile’ file and add:
Install PEAR by doing:
wget http://pear.php.net/go-pear -O go-pear.php
ls -l ~/.pearrc
edit your ‘.profile‘ and add (don’t forget to change yourname):
pecl install memcache
Add ‘extension=memcache.so’ to your php.ini file, you can find it using the command below:
php -i | grep ‘php.ini’
An Alternative Method to Installing Memcached
mkdir -p /root/source
rpm -ivh rpmforge-release-0.3.6-1.el5.rf.i386.rpm
yum -y install libevent*
yum -y install memcached*
gem source -a http://gems.github.com
tar -xzvf libmemcached-0.25.14.tar.gz
./configure && make && make install
gem install memcached-0.13.gem
gem install memcache-client—version=1.6.3