Questions about enabling the object cache using Memcached

Hello!

So, I decided that for funsies I would set up the next site using Memcached instead of Redis as the object cache provider.

The documentation leaves a lot to be desired though, or maybe (most likely?) this is a skill issue

So I’ve added this to the development/wordpress_sites.yml:

object_cache:
    enabled: true
    provider: memcached 
    database: 0

And I’ve installed the suggested plugin with
composer require wpackagist-plugin/memcached

Now to the part where I get confused.
The documentation says:

The plugin typically auto-activates when it detects Memcached is available.

Is this an actual thing? Can a plugin auto-activate?

More importantly, when I try to activate the plugin manually I get this error:

Cannot redeclare function wp_cache_add()

Now according to the plugin’s site this is because it requires a very manual installation process:

  1. Install memcached on at least one server. Note the connection info. The default is 127.0.0.1:11211.
  2. Install the PECL memcache extension
  3. Copy object-cache.php to wp-content
  4. Add the WP_CACHE_KEY_SALT constant to the wp-config.php:phpdefine( 'WP_CACHE_KEY_SALT', '...long random string...' );

So the question is, what’s the recommended way of using Memcached these days? Is the recommendation a blank “use Redis instead, stoopid!” or is the documentation linking to the wrong plugin or something?

All help, feedback or hate is welcome with open arms.

memcached is a drop-in plugin.

Unlike regular plugins, it is not meant to be installed under wp-content/plugins. Drop-in plugins are meant to be copied to some specific locations. In this case, web/app/object-cache.php.

Check the list of drop-in plugins from the source code of _get_dropins().

Can a plugin auto-activate?

It could because WordPress core loads drop-in plugins very early in its lifecycle. Actually, there is no way for you to deactivate drop-in plugins.

it requires a very manual installation process

composer/installer supports drop-in plugins. But you might need to update some composer config on your projects. I couldn’t get composer/installer to work. Use koodimonni/composer-dropin-installer instead. See Revise Memcached Object Cache installation instructions by tangrufus · Pull Request #562 · roots/docs · GitHub

1 Like

Thanks for the clarification on that plugin being a drop-in! Just updated the docs

1 Like

it requires a very manual installation process…

Can you give Revise Memcached Object Cache installation instructions by tangrufus · Pull Request #562 · roots/docs · GitHub a try please, @Tetrahedrax ?

2 Likes

Hello thanks for the help!

I’ve done as follows in an attempt to follow your guide:

  1. Set this inside of trellis/group_vars/development/wordpress_sites.yml
object_cache:
    enabled: true # Enable object caching
    provider: memcached # Use Redis (or memcached)
  1. Run trellis provision development

  2. Add the object-cache.php that you linked here: public/content/object-cache.php (I’m testing this on a Radicle project so maybe the gitignore part should be updated to include Radicle as well?), and run the commands that you’ve included.

  3. I visit the site and I get this message:

Fatal error: Uncaught Error: Class "Memcache" not found in /srv/www/example.se/current/public/content/object-cache.php:849 Stack trace: #0 /srv/www/example.se/current/public/content/object-cache.php(82): WP_Object_Cache->__construct() #1 /srv/www/example.se/current/public/wp/wp-includes/load.php(893): wp_cache_init() #2 /srv/www/example.se/current/public/wp/wp-settings.php(147): wp_start_object_cache() #3 /srv/www/example.se/current/public/wp-config.php(11): require_once('/srv/www/example...') #4 /srv/www/example.se/current/public/wp/wp-load.php(55): require_once('/srv/www/example...') #5 /srv/www/example.se/current/public/wp/wp-admin/admin.php(35): require_once('/srv/www/example...') #6 /srv/www/example.se/current/public/wp/wp-admin/plugins.php(10): require_once('/srv/www/example...') #7 {main} thrown in /srv/www/example.se/current/public/content/object-cache.php on line 849

  1. Pull my hair trying to fix it. I got a lot of hair so that part is fine.

30 min later

So the issue is that we’re using Memcached while the plugin is using Memcache.

The solution? Use this plugin instead which uses Memcached:

The moment I added that plugin it started magically working. Feel free trying it on a newly provisioned server and see if it works for you too! If not, I’ll try it myself next week or whenever the reconstructing at work is fully complete. Working without an external screen is hell, man!

2 Likes