Absynth’s Research

Author Archive

Utilizing Memcache with PHP

by Absynth on Apr.04, 2009, under PHP, Programming

Intro
In this article I will demonstrate how to use the PHP memcache extension, as well as a possible use case for memcache. If you have not already installed memcached and the PHP memcache extension check out http://www.wolflabs.org/2008/12/07/memcached-php-memcache-extension-installation/.

Example Source
Here is the code that I will be using for this example. Please note that this script should be run from the cli on the same server memcached is currently running on.

<?php
/**
* Memcache demo script
*
* @author Anthony Stramer
* @version $Id: memcache.demo.php 2 2009-01-20 20:43:23Z absynth $
* @filesource memcached.demo.php
* @copyright (c) 2009 Anthony Stramer - All Rights Reserved
* @license http://www.gnu.org/licenses/gpl.html
*/

// create a new memcache object
$memcache = new Memcache;

// connect to our memcache server
$memcache->connect('localhost', 11211);

echo 'Checking if cache existed...';

// get the data from memcached
$var = $memcache->get('testData');

// check to see if our data is in cache already
if(!$var)
{
echo "no.\n";

$temp = time();

echo "Setting cache to value [{$temp}]...";
$memcache->set('testData', $temp, MEMCACHE_COMPRESSED);
echo "done";
}else{
echo "yes.\n";
echo "Data in cache: {$var}\n";

// clear the cache
echo "Clearing cache...";
$memcache->delete('testData');
echo "done.";
}

echo "\n";
?>

Using the Memcache PHP Extension
In the example code above I have outlined how to connect, set data, and destroy data in memcached. Lets go step by step through the code, the first thing we need to do is create a new memcache class, if you get an error saying that the class does not exist that means that the PHP memcache extension is not installed. As well we will connect to the memcahe server, in my case it is my localhost on the default memcahe port.

// create a new memcache object
$memcache = new Memcache;

// connect to our memcache server
$memcache->connect('localhost', 11211);

Next we will try to see if our test data already exists in memcache in the ‘testData’ key, think of memcache as an associative array.

$var = $memcache->get('testData');

Now the data that is loaded for the ‘testData’ key should be in our variable. We are going to check to see if there was data in our ‘testData’ key. If the data exists then we will unset it, although if the data did not exist we will set it with the current UNIX time stamp. Also please note that we are using the MEMCACHE_COMPRESSED flag, the reason for this is that we can store more information in memory this way, but it does cause more strain on the server’s CPU since every time we put or get data it needs to be compressed or uncompressed. If you are unable to set/get any data from memcache make sure your php install was compiled with zlib support, memcache compression uses zlib, if it is not available the set or get will fail silently and you won’t get any data.

// get the data from memcached
$var = $memcache->get('testData');

// check to see if our data is in cache already
if(!$var)
{
echo "no.\n";

$temp = time();

echo "Setting cache to value [{$temp}]...";
$memcache->set('testData', $temp, MEMCACHE_COMPRESSED);
echo "done";
}else{
echo "yes.\n";
echo "Data in cache: {$var}\n";

// clear the cache
echo "Clearing cache...";
$memcache->delete('testData');
echo "done.";
}

A Real World Example
A real world example for the use of memcache is user data for an application. Lets say that you have a really complicated ACL (access control list) that you use to set down fine grain permissions, these type of scripts can be very database intensive if you use normalization like I do, lets say that you are running 4 queries per user to figure out their permissions, that may not seem like a lot but you need to take into account all of the other read/writes that are happening in the database for every other user. You will strain the database with these queries that really do not need to be run on each page load. What I do for applications I work on is create a cache of the user’s permissions as well as their overall user data, and use a key that will be unique to each user, for example ‘USRD_’. In a perfect world this would work all the time and you would never have to reload user data once it is compiled, but this isn’t true since administrators could change user permissions on the fly. So my solution is to simply destroy the cached user data of all logged in users, or for a single user if permissions were changed for a single person, as well any time that a user changes their data, like email or name, the cache is destroyed and recreated on the next page load. Using caching techniques like this you should be able to lower the hit on the database server by a lot.

Something else that I would suggest that you should do is take a look at your slow query logs. If you see a particular query coming up a lot or generally taking a ton of system resources, ask yourself if it could be cached, remember memcache servers are cheap, database servers are not.

1 Comment more...

Installing PHP from source on CentOS x86_64 (w/ apache)

by Absynth on Dec.08, 2008, under Program Installation

Installing PHP from source is much easier than most people think. In this tutorial I will describe how to install a bare PHP build with mysql/mysqli support in addition to configuring apache to interpret PHP scripts.

Compiling PHP Source

Alright, well in order to compile the php source code you must first have gcc install (# yum install gcc). Also if you want to be able to use PHP in apache then you need to have httpd and httpd-devel packages installed. Here is how I did my install. (Please note that I used PHP 5.2.6 for my install, but this will work with just about any php version, just be sure to untar and cd into the proper directory for your version of php.)

[root@nitrogen ~]# yum install gcc-c++ httpd httpd-devel apr-devel libxml2-devel zlib zlib-devel mysql-devel openssl-devel
[root@nitrogen ~]# wget http://www.php.net/get/php-5.2.6.tar.gz/from/this/mirror
[root@nitrogen ~]# tar -zxvf php-5.2.6.tar.gz
[root@nitrogen ~]# cd php-5.2.6
[root@nitrogen cd php-5.2.6]# ./configureĀ --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.dĀ --with-apxs2 --with-libdir=lib64 --with-mysql --with-mysqli --with-zlib
[root@nitrogen cd php-5.2.6]# make clean
[root@nitrogen cd php-5.2.6]# make
[root@nitrogen cd php-5.2.6]# make install

You’re also going to want to place a php.ini into /etc/php.ini and make the /etc/php.d directory if you have not done so already.

[root@nitrogen cd php-5.2.6]# cp php.ini-recommended /etc/php.ini
[root@nitrogen cd php-5.2.6]# mkdir /etc/php.d

Installing PHP into apache

To install PHP into apache all you need to do is place the following configuration file in /etc/httpd/conf.d/php.conf.

# /etc/httpd/conf.d/php.conf
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages
#

LoadModule php5_module modules/libphp5.so

#
# Cause the PHP interpreter to handle files with a .php extension.
#
AddHandler php5-script .php
AddType text/html .php

#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php

#
# Uncommenting the following line to allow PHP to pretty-print .phps
# files as PHP source code:
#
#AddType application/x-httpd-php-source .phps

Finalizing our install is fairly simple, just restart apache by typing the following command and you should be good to run PHP applications for the web.

[root@nitrogen ~]# /sbin/service httpd restart

4 Comments :, , more...

Memcached + PHP Memcache Extension Installation

by Absynth on Dec.07, 2008, under Program Installation

Reciently at work as well as in my own personal developement I have found a lot of emphasis on caching technologies. Be this for keeping heavy database queries to a minimum or because you wish to keep heavily served files out of the disk I/O. In this post (my first post!!) I will demonstrate how to install and configure memcached on a linux (CentOS to be specific) box. As well how to install the memcache extension into PHP.

Installing Memcached

Make sure before you start tying to install memcache that you have gcc (# yum install gcc) and libevent-devel (# yum install libevent-devel) installed.

[root@nitrogen ~]# wget http://www.danga.com/memcached/dist/memcached-1.2.6.tar.gz
[root@nitrogen ~]# tar -zxvf memcached-1.2.6.tar.gz
[root@nitrogen ~]# cd memcached-1.2.6
[root@nitrogen memcached-1.2.6]# ./configure
[root@nitrogen memcached-1.2.6]# make
[root@nitrogen memcached-1.2.6]# make install

Now if you are lazy like me and don’t want to have to remember what the memcached flags are every time you start it I would recommend using an init.d script. I recommend using the one located at http://www.dev411.com/wiki/Memcached_startup_files_for_Red_Hat_(RHEL). To install the init.d script on your server you need to place the start-memcached perl script in /usr/local/bin/start-memcached. Make sure you have given this file chmod 755 and is chown root:root. Then you must place the memcached-init into /etc/init.d/memcached, make sure this script has the same permissions as the previous file. Finally create the configuration file /etc/memcached.conf, you can place any options that you want ran by the memcached binary in this file. I use the following in my file.

# /etc/memcached.conf

# user to run memcached as
-u apache

# we want to run memcached as a daemon
-d

# amount of RAM we want to dedicate to memcached in MB
-m 512

# interface to listen to for memcached connections on
-l 127.0.0.1

# port to listen for memcached connections on
-p 11211

Starting Memcached

Now you should be able to start and stop memcached quickly by using the following commands.

[root@nitrogen ~]# /sbin/service memcached start
[root@nitrogen ~]# /sbin/service memcached stop

If you did not install the init.d script then you can start memcached with the following command.

[root@nitrogen ~]# memcached -d -u apache -m 512 -l 127.0.0.1 -p 11211

Installing the Memcache PHP extension

Installing the memcache php module from source is fairly strait forward, it is just like installing any other php module. Please note that you must have zlib-devel and php-devel packages installed.

[root@nitrogen ~]# wget http://pecl.php.net/get/memcache
[root@nitrogen ~]# tar -zxvf memcache-3.0.2.tgz
[root@nitrogen ~]# cd memcache-3.0.2
[root@nitrogen memcache-3.0.2]# phpize
[root@nitrogen memcache-3.0.2]# ./configure
[root@nitrogen memcache-3.0.2]# make
[root@nitrogen memcache-3.0.2]# make install

Now we need to tell php to include our memcache extension when it starts up. We do this by putting the following configuration data in /etc/php.d/memcache.ini

; /etc/php.d/memcache.ini

; include the memcache php extension
extension=memcache.so

You will be able to verify that the memcache php extension has been installed by typing # php -m and looking for memcache in the php extension list.

3 Comments :, more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...