Training, Open Source Programming Languages

This is page ... n-PHP.html

Our email: • Phone: 01144 1225 708225

For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.


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

2. Memcached itself


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


a) Download and untar the file
b) cd into the directory created
c) phpize && ./configure --enable-memcache && make
as root
d) cp modules/ /usr/local/lib/php
e) vi /usr/local/lib/php.ini
; extension_dir = "./"
extension_dir = "/usr/local/lib/php"
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 -m 32 -p 11211
-d - start as a daemon
-l - listen on (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: 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/

Starting on reboot too!

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

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

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

Back to
Old pictures and comparisons
Previous and next
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 - 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).

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: • WEB: • SKYPE: wellho

PAGE: ... n-PHP.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb