Typo3. Caching-Framework. Memcache. Mamp Pro. OS X Lion.

For my latest project, we used Extbase and Fluid with Typo3 4.5. My development environment is a MacBook Pro running OSX Lion with MAMP Pro 2.0. Newer versions of Typo3 come with a caching framework that allows you to define different caching methods for different tasks and even lets you integrate your own caching mechanisms into your extensions. The caching framework makes use of several caching backend – DB, File, RAM-Memory, APC etc.

For my installation I wanted to use the apache memcache extension.

Configure Typo3 to use memcached

Do enable the framework you need to set some values in the localconf-file. Take care – some values will change in Typo3 4.6. – especially there are more defaults so you don’t have to set all of this. The code below is for Typo3 4.5.X to be put into typo3conf/localconf.php:

// cachingframework-Memcached - START
$TYPO3_CONF_VARS['SYS']['useCachingFramework'] = '1';
$TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations']['cache_hash'] = array (
	'frontend' => 't3lib_cache_frontend_VariableFrontend',
	'backend' => 't3lib_cache_backend_FileBackend',
	'options' => array(  
 	)
);
if($_SERVER['T3_USEMEMCACHE']){ 
	$mserver = $_SERVER['T3_MCACHESERVER'] ? $_SERVER['T3_MCACHESERVER'] : 'localhost:11211';
	$TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations']['cache_pages'] = array (
		'frontend' => 't3lib_cache_frontend_VariableFrontend',
		'backend' => 't3lib_cache_backend_MemcachedBackend',
		'options' => array(
			'servers' => array($mserver), 
		)                       
	);    
	$TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations']['cache_pagesection'] = array (
		'frontend' => 't3lib_cache_frontend_VariableFrontend',
		'backend' => 't3lib_cache_backend_MemcachedBackend',
		'options' => array(                        
			'servers' => array($mserver),
		)   
	);  
}
// cachingframework-Memcached - END 

Take a look at line 9 – I use such settings to make configurations on server / environment level – you can then set this variables in the a .htaccess file or – as I use to do it – in the vHost config:

# Use memcache extension for caching
SetEnv T3_USEMEMCACHE 1 

If you have inserted the code into your localconf.php file and hit the refresh-button, you’ll get this;

Now, MAMP Pro comes with the memcache extensions enabled – unfortunately it doesn’t work. (it’s 32Bit, not 64Bit…)

After digging around a bit I finally could make it run. This article of Leon Barrett was the key to success. I am using MAMP Pro Version 2.0 so a few paths are different. That’s why I decided to create this article so you don’t have to make the same mistakes as I did…

Install Xcode

First, you need to download the free Xcode from the App-Store. What you download is only the installer – install it after download! After installing Xcode, you are ready to compile memcache for MAMP. Most parts of the following steps are taken from Leon’s Website.

Step 1 – Make the MAMP php files executable

sudo chmod +xrw /Applications/MAMP/bin/php/php*

Step 2 – Switch to the download directory

cd ~/Downloads/

Step 3 – Download the memcache source

curl http://pecl.php.net/get/memcache-2.2.5.tgz > memcache-2.2.5.tgz

Step 4 – Unpack the source and go into the folder

tar -zxvf memcache-2.2.5.tgz
cd memcache-2.2.5

Step 5 – PHPize the Memcache extension files

/Applications/MAMP/bin/php/php5.3.6/bin/phpize

Step 6 – Compile the Memcached extension

CFLAGS=‘-O3 -fno-common -arch i386 -arch x86_64 -g -Os‘ LDFLAGS=‘-O3 -arch i386 -arch x86_64′ CXXFLAGS=‘-O3 -fno-common -arch i386 -arch x86_64 -g -Os‘ ./configure –disable-dependancy-tracking –disable-shared -enable-static

make

Step 7 – Copy the extension into MAMP

sudo cp modules/memcache.so /Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/no-debug-non-zts-20090626/.

Step 8 – Add the extension to the php.ini file

To do this open the MAMP application, go to File > Edit Template > PHP > PHP 5.3.6 php.ini
Search for „; Extensions“ and add the following line:

extension=memcache.so

Step 9 – Configure Memcache

Somewhere in this file, you need to configure Memcache. I used the configuration proposed by Dmitry Dulepov which works just fine for me:

memcached.allow_failover 1
memcached.chunk_size 32768
memcached.default_port 11211
memcached.hash_function fnv
memcached.hash_strategy consistent
memcached.max_failover_attempts 20

Save the file, stop MAMP and start it back up. Take a look inside your php log to see if there were any errors starting up.

Step 10 – Start Memcached

memcached -d -m 8 -M -p 11211

You can stop memcached with „killall memcached“

Enjoy

Hit the refresh-button on your website again – it shines in memcached glory!

Now, memcache needs to be started manually each time you start MAMP. The should be ways to automate this – but I couldn’t make it work…