Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
memcached - overview, installation, example of use in PHP

Let's say you have a large database on a busy server, and queries that are eating into your resources. Would you like to be able to speed things up?

It usually turns out that there are certain queries which are very frequent and which return the same results each time (or for a period of time) which account for most of your traffic - although over time new queries will join the "popular" club and others will fade into the "was once popular" category. If you run a forum, or a blog, or an image resource site like we do, you'll probably recognise the metric.

Memcached provides a daemon which you can run on your server which grabs a specified amount of memory into which common query results (or other information) can be stored for re-use. It uses a hashing technique for very fast lookup, and it keeps tabs on the time / date that each item was last accessed so that it can drop the least popular items once it runs out of memory. It's really very clever because:
• Only small code changes are needed to your application, around the slow queries.
• It is self-adjusting in that it will identify the data it ought keep in memory and that will change as query popularity changes
• It works across multiple cpus, (i.e. you can have a network of servers) so that you make every bigger savings in a load balanced environment.

I've installed memcached on our server, and started the daemon. I've downloaded the PHP (Pecl) drivers and installed those too and put together a demonstration om memcached. You can run it here and view the source code here. I would (of course) be delighted to go into this in much more detail - and do some practical exercises - on our PHP techniques workshop.




Installing Memcached, and the PHP drivers

To quote a website I found this morning when doing the installation ...

"Installation instructions are fairly straightforward which is why I'm not going to discuss those here. Once you've installed the extension you need to restart your Apache web server. You can check whether the extension is running by using phpinfo(). If all is ok you'll see a section about memcache.".

Hmm - it might have been easy for that author, but I didn't feel that it would be trivial for our customer base, so here are some pointers at least - a summary of what I did.

Memcached Installation

1. Pre-requisite - libevent.

from http://www.monkey.org/~provos/libevent/

a) Download and untar the file
b) cd into the directory created
c) ./configure
d) make
as root:
e) make install

2. Memcached itself

from http://www.danga.com/memcached/download.bml

a) Download and untar the file
b) cd into the directory created
c) ./configure --prefix=/usr/local/memcached
(I chose to make up a separate directory in case I want to (re)move later)
d) make
as root:
e) make install

PHP drivers for memcached

from http://pecl.php.net/package/memcache

a) Download and untar the file
b) cd into the directory created
c) phpize && ./configure --enable-memcache && make
as root
d) cp modules/memcache.so /usr/local/lib/php
e) vi /usr/local/lib/php.ini
change
; extension_dir = "./"
extension_dir = "/usr/local/lib/php"
add
extension=memcache.so
f) stop and restart the web server
g) Check that the memcache extension loaded (via a phpinfo page)

Starting the memcached daemon

As a regular user with minimal privileges, I ran

/usr/local/memcached/bin/memcached -d -l 127.0.0.1 -m 32 -p 11211
-d - start as a daemon
-l - listen on 127.0.0.1 (loopback, for local use)
-m - allow 32 Mbytes of memory
-p - port number on which to run
(11211 is the default anyway)

I got the following error:

/usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory

which I corrected as root as follows:

cd /usr/lib
ln -s /usr/local/lib/libevent-1.4.so.2 libevent-1.4.so.2


Starting on reboot too!

I have added the following onto the end of /etc/rc.local

/usr/local/memcached/bin/memcached -d -l 127.0.0.1 -m 32 -p 11211 -u wellho

(The scripts runs as root - the -u option switches in to user wellho!)

This technical note introduces a huge subject ... drivers are available for other languages too, a single cache can be used for multiple applications, caching can be done cross-server, records can be updated (and you need to take care with records that may be updated in the database to avoid serving expired stuff). This is an excellent way - if you have a computer with a lot of memory but a cpu that's running flat out - to trade in memory to regain cpu cycles, but beware on 32 bit systems of going over 2Gbytes (if you do, run multiple memcached instances!)

Note too - my server is behind a firewall, so remote access to port 11211 should not be possible!
(written 2008-08-02)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
A164 - Web Application Deployment - Services and Regular Jobs
  [4487] Starting MySQL. ERROR! The server quit without updating PID file - how we fixed it. - (2015-05-06)
  [3792] Managing daemons from a terminal session - (2012-07-01)
  [3791] The Kernel, Shells and Daemons. Greek Gods in computing - (2012-07-01)
  [3143] On time - (2011-01-23)
  [3011] What are .pid files? - (2010-10-23)
  [2182] What Linux run level am I in? - (2009-05-15)
  [2145] Using the internet to remotely check for power failure at home (PHP) - (2009-04-29)
  [1903] daemons - what is running on my Linux server? - (2008-11-23)
  [1765] Dialects of English and Unix - (2008-08-21)
  [1731] Apache httpd, MySQL, PHP - installation procedure - (2008-08-01)
  [1700] FTP server on Fedora Linux - (2008-07-06)
  [1633] Changing a screen saver from a web page (PHP, Perl, OSX) - (2008-05-06)
  [1553] Automatic startup and shutdown of Tomcat - (2008-02-24)
  [1288] Linux run states, shell special commands, and directory structures - (2007-08-03)
  [1028] Linux / Unix - process priority and nice - (2007-01-10)
  [907] Browser -> httpd -> Tomcat -> MySQL. Restarting. - (2006-10-28)
  [544] Repeating tasks with crontab - (2005-12-27)

H307 - PHP - Web2 and caching
  [4627] Caching results in an object for efficiency - avoiding re-calculation - (2016-01-20)
  [4136] How do I post automatically from a PHP script to my Twitter account? - (2013-07-10)
  [4106] Web server efficiency - saving repetition through caches - (2013-05-30)
  [4075] Further recent PHP examples - (2013-04-28)
  [4055] Using web services to access you data - JSON and RESTful services - (2013-03-29)
  [3999] Handling failures / absences of your backend server nicely - (2013-02-08)
  [3955] Building up from a small PHP setup to an enterprise one - (2012-12-16)
  [3458] On this day ... one PHP script with three uses - (2011-09-26)
  [3186] How to add a customised twitter feed to your site - (2011-02-27)
  [3094] Setting your user_agent in PHP - telling back servers who you are - (2010-12-18)
  [3029] PHP data sources - other web servers, large data flows, and the client (browser) - (2010-11-04)
  [2545] Scraping content for your own page via PHP - (2009-12-21)
  [2321] Uploading and Downloading files - changing names (Perl and PHP) - (2009-08-04)
  [2196] New Example - cacheing results in PHP for faster loading - (2009-05-24)
  [1995] Automated server heartbeat and health check - (2009-01-16)
  [1926] Flash (client) to PHP (server) - example - (2008-12-06)
  [1814] Javascript/HTML example, dynamic server monitor - (2008-09-28)
  [1813] Ajax - going Asyncronous and what it means - (2008-09-28)
  [1812] Starting Ajax - easy example of browser calling up server data - (2008-09-27)
  [1647] Exchange Rates - PHP with your prices in your users currency - (2008-05-19)


Back to
Old pictures and comparisons
Previous and next
or
Horse's mouth home
Forward to
All around the world?
Some other Articles
Rules, suggestions, considerations for Lua variable names
Current visitors from around the world - PHP
Finding words and work boundaries (MySQL, Perl, PHP)
All around the world?
memcached - overview, installation, example of use in PHP
Old pictures and comparisons
Punting on the Cam
Back from the future
A short Perl example
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1733_mem ... n-PHP.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat