Skip to content

Instantly share code, notes, and snippets.

@scottlee
Last active March 15, 2019 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottlee/22a1530415b67d2ad6ea7116d4350f78 to your computer and use it in GitHub Desktop.
Save scottlee/22a1530415b67d2ad6ea7116d4350f78 to your computer and use it in GitHub Desktop.
Getting object caching working with MAMP, Memcached, and WordPress

MAMP 3.x

Steps

  1. Install Brew
  2. brew install memcached
  3. brew install libmemcached
  4. Grab a memcached.so file from here. Like this one for PHP 7.0.0.
  5. Move the memcached.so file to: /Applications/MAMP/bin/php/php7.x.x/lib/php/extensions/no-debug-non-zts-200xxxxx
  6. Add the following to the end of the php.ini: extension=memcached.so
  7. Grab a copy of object-cache.php from here.
  8. Move object-cache.php to the wp-content of your project.
  9. Start/Restart MAMP

Testing

$cash_money = wp_cache_get( '_cache_testing_101', '_testing_group' );
if ( false === $cash_money ) {
	$cash_money = wp_cache_set( '_cache_testing_101', '😎', '_testing_group', 60 );
	echo 'cache: fail 😞';
} else {
	echo 'cache: pass ' . $cash_money ;
}

MAMP 4.1+

Current versions of MAMP come with an interface for enabling/disabling Memcached. As such, some of the above steps from 3.x no longer apply.

tl;dr for some reason, you have to tell PHP about memcached even if activated via the GUI.

Steps

  1. Add the following to the end of the desired version via the php.ini: extension=memcached.so
  2. Grab a copy of object-cache.php from here.
  3. Move object-cache.php to the wp-content of your project.
  4. Start/Restart MAMP with Memecached enabled.

Testing

$cash_money = wp_cache_get( '_cache_testing_101', '_testing_group' );
if ( false === $cash_money ) {
	$cash_money = wp_cache_set( '_cache_testing_101', '😎', '_testing_group', 60 );
	echo 'cache: fail 😞';
} else {
	echo 'cache: pass ' . $cash_money ;
}

MAMP 4.5

Current versions of MAMP come with an interface for enabling/disabling Memcached. As such, some of the above steps from 4.1 no longer apply.

Steps

  1. Grab a copy of object-cache.php from here.
  2. Move object-cache.php to the wp-content of your project.
  3. Check "Use network to talk to Memcached" in MAMP.
  4. Start the Memecache service in MAMP.
  5. OPTIONAL Might need to enable Group Start for Memcache in order for the PHP extension to be loaded. Verify by checking the phpinfo() page.

Testing

$cash_money = wp_cache_get( '_cache_testing_101', '_testing_group' );
if ( false === $cash_money ) {
	$cash_money = wp_cache_set( '_cache_testing_101', '😎', '_testing_group', 60 );
	echo 'cache: fail 😞';
} else {
	echo 'cache: pass ' . $cash_money ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment