Goldlilys Media

San Diego Web Design and Development Company

How To Install Memcache

Another website accelerator that caches MySQL queries, and PHP executions is called Memcache. Memcache decreases the load on the server, enabling you to deliver the pages to your end-users more quickly because it doesn't need to calculate queries from the database anymore, but fetches it from the memory cache instead.

Think of it this way, if you're trying to remember a particular piece of info with facts coming from different sources, it's hard to find things if you haven't written it down somewhere. People easily forget much of their vast knowledge until they remember an idea about that topic. Memcache works almost like a traditional notebook. It allows the server to store all of the information in one spot, eliminating the need to search the entire database to execute an action because the action is already in the server's memory.

Steps to add Memcache to your server (mine runs on CentOS) are:

  1. # yum install memcache
  2. Depending on how much RAM your server contains, the amount of memory allocated should be at least ¼ of it. For example, if your server RAM is 4GB, then allocate 1GB of memory for memcache.
  3. Make changes in the config file at /etc/sysconfig/memcached and include these info:

    • PORT="11211"
    • CACHESIZE is the allocated memory where 1024 is 1GB.
    • The OPTIONS setting is to make sure memcache only listens to the localhost port in your server to avoid caching unnecessary data.
    • To make your memcache more secure, add this line to your IP tables:
      -A INPUT -p tcp --dport 11211 -j ACCEPT
  4. Once you've made changes to the setting, start up memcache by:

    # service memcached start
    # chkconfig memcached on
  5. Hold up, we are not done yet. This is the part that I usually forget when installing memcache. We now need to install the Memcache PECL Extension for PHP.

    # pecl install memcache
  6. Make changes to your php.ini at /etc/php.ini or /usr/local/lib/php.ini and add these info:

  7. Restart Apache to apply the changes to your server: # /etc/init.d/httpd restart
  8. To verify memcache is working: # ps aux | grep memcached

To add memcache to Drupal:

  1. Put site to offline mode.
  2. Drush and install memcache
  3. Go to your site's settings.php and add:

    $conf['cache_backends'][] = 'sites/all/modules/memcache/';
    $conf['cache_default_class'] = 'MemCacheDrupal';
    $conf['memcache_key_prefix'] = 'something_unique';
  4. Save, enable the memcache module and put your site back online.

To add memcache to WordPress:

  1. Download
  2. Copy object-cache.php to /wp-content directory.

These should be it for both Drupal and WordPress. Watch your websites load much faster than before.

Blog Category: Web Tools: Services:



Hera Hub Mission Valley
8885 Rio San Diego Drive, Suite 237
San Diego, CA 92108

Hera Hub Sorrento Valley
4010 Sorrento Valley Blvd, Suite 400
San Diego, CA 92121

Office Hours:

Monday - Friday 10am - 5pm

For a consultation, schedule an appointment.