Posts tagged MemCache

Linux Logo

CentOS – PHP – Install Memcached (with a d) Stack

0

Recently I started retro-fitting the MeLikeDrinks.com drink website to cache frequently used data to improve performance, as such I wrote a light, custom cache API which sits on top of PHP’s Memcached API. Because I follow TDD principles, I wrote out the tests first, which helped me write out the API calls needed to support the application, but when it came time to actually get the memcached service on my CentOS box, I ran into all sorts of confusion, which motivated me to write this article. It is my hope that this article will help alleviate any confusion others may face when they decide to dive into the cache pool.

First, I need to clarify one of the more confusing issues regarding PHP and Memcache, which is, there are two different PHP Apis. Depending on which PHP Memcache API you select, will determine the steps necessary for PHP to gain access to the underlying memcached server. The differences between PHP Memcache and PHP Memcached are outlined here. Regardless of the differences between the two PHP APIs we have to choose from, they both access the same underlying Memcached Service. The only REAL difference, as far as configuration and installation steps are concerned, is that PHP Memcached requires an external library known as libmemcached. In short, the stacks look like this:

PHP (PECL) Memcache

PHP (PECL) Memcached

  •  libevent (dependency)
  • memcached (service)
  • libmemcached
  • zlib (PHP dependency)
  • PHP (PECL) Memcached

As you can see, the stacks are nearly identical, except for the fact that PHP Memcached requires an extra layer; libmemcached. If you opt to use PHP Memcache, and because this article assumes you are using CentOS,  you can simply have YUM install the entire stack for your via `yum install php-memcache`. If your environment requires you to compile PHP, then you can issue a `yum install memcached` command, and YUM will install libevent and memcached, then you can compile PHP (and PHP Memcache module).

If you are still reading, then you want to install and use PHP Memcached, which unfortunately will require a little more work on your end. I will not go over how to install PHP Memcached using PECL, as I do not believe in these types of automated processes. In the past I have had bad experiences with PECL and rather not introduce another layer of complexity, so the following steps will allow you to compile and install the PHP Memcached stack without PECL.

Steps required:

  • libevent (dependency)
    • yum install libevent libevent-devel
  • memcached (service)
    • yum install memcached
  • libmemcached
    • First check which version of PHP Memcached you wish to use, which will determine which version of libmemcached you need. For example; according to PHP PECL Memcached changelog, the latest version of libmemcached you can use is 1.0.4, otherwise if you try to use a newer version of PHP PECL Memcached you may run into unforeseen issues, in other words, you should ALWAYS assume that PHP PECL Memcached is a few versions behind libmemcached.
    • Based on which version of libmemcached you need from the previous step, you can download from libmemcached download page.
    • Extract file and CD into dir
    • $ -> ./configure –with-libevent-prefix=/usr
    • $ -> make
    • $ -> make install
  • PHP PECL Memcached
    • Download the correct version based on which version of libmemcached you compiled and installed via changelog (which links to download) page.
    • Extract file and CD into dir
    • $ -> phpize
    • $ -> ./configure –with-libmemcached-dir=/path/to/where/memcached.h/is/located
    • make
    • make install
  • PHP ini config
    • vi /path/to/php.ini
    • Add: extension=memcached.so
  • Test module installation
    • $ -> php -m
      • You should memcached listed among other modules
    • $ -> php -i | fgrep -irs cache
      • You should see various memcached config settings
  • Finishing touches
    • Restart apache
    • Start memcached
    • Write a test script to test the setting and getting of a value from your cache server via PHP Memcached API.

And there you have it, a memcached stack without using PECL. All things considered it should not have been too painful an installation, however I must make one disclaimer; I customized my memcached stack a bit more than I eluded to in this article, so if you run into an issue, just post a comment and I will try to help you resolve the issue.

Now that you have a memcached service, and an API to use, you should start focusing on the code points of your application with the most overhead, this will give you the most bang for your buck when you start caching data. Good luck, and happy caching.

Linux – Install Memcached on CentOS

0

According to http://memcached.org/, Memcached is a “distributed memory object caching system”. What this means is that memcached will return stored values from memory rather than other storage mediums such as disk or database. How do you access these values? Well, depending upon the key construct you used, you just need to provide the memcached with the proper key and you will gain access to stored values (also known as a key-value pair).

Memcached provides a layer which sits between your application and your backend storage solution. What memcached does is allow you, the developer, to store commonly retrieved data into the cache layer so subsequent requests will use the cache version rather than querying your database or pinging your filesystem. As you can probably tell, this is a much faster solution and reduces the resources needed by your databases.

There is a lot of power that memcached provides, but as a developer you need to ensure your app can correctly harness this power. What I mean by this is; imagine you update a value in your database, then through your application you submit a form to confirm the value has indeed been changed, but for some reason you keep seeing the old value. Guess what? Your old value was cached and you optimized your application to use cache before hitting the database. As a developer you need to create a method of clearing cache when needed so you don’t bang your head on the desk trying to figure out why values you changed are not being respected by your application.

Now that you have a basic understanding of what memcached does, lets install it on your centos system.

Memcached is not available to your system by default (via YUM), you will have to gain access to rpmforge (a collection of rpms). If you don’t have rpmforge setup, follow this guide: http://melikedev.com/2010/03/10/centos-basic-usage-guide/ in the section RPMForge.

Now that you have rpmforge setup you should be able to get access to memcached. Try:

yum list memcached

If all looks good, then go ahead and install:

yum install memcached

It should also install about 10 more dependencies all from rpmforge, but shouldn’t have any negative impact on your system. When that’s finished you now have memcached installed on your system.

Now it’s time to configure. First we want to add a user to the system so we can run the memcached(aemon) as a non-root user:

adduser memcached

Now we can edit memcached settings:


# /etc/sysconfig/memcached

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""

Now you should be able to start your memcached server:

sudo -u memcached /etc/init.d/memcached start

If you get an permission related error regarding the lock file, you will have to ensure that the memcached user can write the log file to /var/lock/subsys (This lock file ensures you don’t run multiple memcacheds at the sametime). What I did is:


chown root:daemon /var/lock/subsys #allow daemon group, group control over subsys

chmod 775 /var/lock/subsys #make subsys dir writable by group owner

You can now test your memcached server by following these steps outlined here: http://melikedev.com/category/memcache/.

Now that you have an up and running cache server you should develop a caching class within your app that will access your memcached server so you can save commonly run query results within the caching layer. Don’t get carried away though, try to be strategic about what you are populating your cache with, and don’t forget to develop some mechanism for clearing cache when needed.

Good luck.

Memcache – Accessing data via TelNet

0

Accessing Memcache via CLI (telnet)

If  you have access to a memcache server you can access it directly as if it were a mysql server or any other daemon that allows console management. To access:

telnet <hostname> 21201

Accessing data

Now that you have gained access to the memcache console you can set, get, and delete memcache keys.

Retrieve:

get <key>

Set:

set <key>

Delete:

delete <key>
Go to Top