The Server Pages

»

TheServerPages Articles

»

Webmasters

»

PHP

»

Performance

Memcached: cacheing objects to memory

Author: Wojjie     Posted: 2006-02-02     Viewed: 16,670

Introduction:

One common problem that is faced with a growing site today, is hardware. With more hits, comes more queries to your database, which can quickly add up to a high server loads and a slugish website.

There are many ways of dealing with this problem, including raid arrays, upgrading memory and optimizing your code/site. With this, most competitive businesses will try the optimizing route first before spending money on upgrading. One method of optimizing your site is to implement some form of cacheing, wether it be pre rendering data to html files, or using memory to temporarily store objects that are requested frequently.

Luckily a stable cacheing system already exists, and it goes by the name of memcached. In this article we will cover the basics of installing and implementing memcached on your server.


Installation:
1. Download the newest memcached from the danga website:
http://www.danga.com/memcached/download.bml
2. Extract the file:
tar -xzvf memcached-1.1.12.tar.gz
3. Run configure:
./configure
4.
make && make install

Running Memcached:

Memcached has many options that you can use to tweak the way it runs, but we will only cover the ones that you will most likely need/use.

Here are the options in the same order as found in the man pages ('man memcached'):
-l [ip_address] Used to specify what IP address memcached should listen to for connections (default: all)
-d Run in daemon mode
-u [username] If running from root, use this option to tell memcached to assume a different user
-m [megabytes] Number of megabytes of memory memcached should use for object storage
-p [port] Port memcached should listen to (Default: 11211)

It is suggested to run memcached behind a firewall or on a local network since there is no authentication.

Here is an example:
memcached -d -m 512 -l 192.168.2.10 -p 11211 -u apache


PHP memcached application:

You may not be able to access the memcached functions with a standard PHP install, if that is the case you will need to install the package via PEAR:

pear install memcache

Test Application:
$memcache = new Memcache;
@$memcache->connect('192.168.2.10', 11211);
print_r($memcache->getStats());
Example output (the output is from one of my servers that has been running memcached already for a long time, as you can tell):
Array
(
    [pid] => 3905
    [uptime] => 4556854
    [time] => 1138887455
    [version] => 1.1.12
    [rusage_user] => 4502.455522
    [rusage_system] => 15093.613421
    [curr_items] => 160954
    [total_items] => 48242229
    [bytes] => 308918308
    [curr_connections] => 2
    [total_connections] => 92131882
    [connection_structures] => 132
    [cmd_get] => 162657219
    [cmd_set] => 48242229
    [get_hits] => 160778314
    [get_misses] => 1878905
    [bytes_read] => 67607617885
    [bytes_written] => 465851923290
    [limit_maxbytes] => 536870912
)


Simple Counter Application
$memcache = new Memcache;
@$memcache->connect('192.168.2.10', 11211);

$count=$memcache->get('my_counter');
if ($count==false) $count=0;
$count++;
$memcache->set('my_counter',$count++);

echo $count;
Example Outputs:
1
2
3

Comments

Copyright © 2004-2015: TheServerPages.com