MemCache – In-memory-caching in PHP

When building API-like applications in PHP, one issue will always remain; a high load will eventually slow down everything. To prevent over-loading, anyone will come to the conclusion a cache is needed. The most access calls should be served without recalculating anything to free up CPU-time.

Caching can be done on Disk, in a database (indirectly also Disk-caching), or directly in memory.

To setup MemCache and use this memory-caching-system, read on :)

Installation of the PHP extension is fairly straightforward.

sudo pecl install memcache

You will be asked

Enable memcache session handler support? [yes] :
type either ‘no’ to keep sessions disk-based, or press enter to keep sessions in-memory. Then after installation, add the line
to /etc/php.ini and restart apache.

Installation of the memcache software is equally easy;

brew update
brew install memcached
cp /usr/local/Cellar/memcached/1.4.14/homebrew.mxcl.memcached.plist ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist

Note: this installs memcache for the current user, not system-wide. I have tried to run it as root, but even supplying the -u root parameter does not solve the issue of memcached crashing.

Reminder; This post is the sixth post in a series to build the “ultimate” setup. See ‘The “Ultimate” Guide …’ for a complete list.

Author: Remon Pel

WebDeveloper though not WebDesigner

Leave a Reply

Your email address will not be published. Required fields are marked *