Redis server for Local?

Is there some sort of object cache server, like Redis, that can be easily integrated with Local.app?

I find myself needing to test object cache data and return locally.

Hey @afragen! As far as I know there isn’t anything that can be integrated, at least not easily.

We do have a feature request hanging around too:

Thanks @Nick-B

Just like the feature request you referenced, I’d like to be able to test locally using an object cache.

1 Like

I’ve reached out to our Dev team @afragen to see if they have any other thoughts or recommendations on this!

I might have found a solution. Docket Cache – Object Cache Accelerator – WordPress plugin | WordPress.org

Testing.

@afragen Local doesn’t offer an “easy integration” at the moment, but here’s a guide to configure redis as an object cache with Local. (Assumes macOS.)

Steps 1 and 2 are one-time only. Steps 3 onward are needed per-site.

1. [CLI] Install redis

brew install redis

No Docker needed, we’ll just use the native binary.

2. [CLI] Run redis

brew services start redis

This will start redis-server now, and automatically restart it if you reboot.

brew services stop redis to stop it.

3. [Local] Add to your site’s wp-config.php

Be sure to change WP_REDIS_DATABASE or WP_REDIS_PREFIX (one or the other is fine) if using the same redis instance with multiple sites.

define('WP_REDIS_CLIENT', 'predis');   // Use PHP client, Local does not bundle the native PhpRedis extension.
define('WP_REDIS_HOST', '127.0.0.1');  // Assumes redis-server is running.
define('WP_REDIS_PORT', 6379);         // Default port is fine for us running redis-server.
define('WP_REDIS_DATABASE', 0);        // Either pick a unique DB per site, 0-15, or…
define('WP_REDIS_PREFIX', 'mysite:');  // …alternatively pick a unique prefix per site.

4. [Local Site Shell] Install and enable Redis Object Cache

wp plugin install redis-cache --activate
wp redis enable

You should see “Success: Object cache enabled.”

That’s it, you’re done!

To check it’s working

You can do wp redis status for more info for each site.

In your WP Admin, you should see this under Settings → Redis:

To turn Redis object caching off

Either:

  • wp redis disable via CLI in the site shell
  • Click “Disable Object Cache” in Settings → Redis in WP Admin.

Other notes

Predis is slower than PHP’s native PhpRedis extension, but should be fine for testing.

It’s likely possible to wrap this setup in an add-on to reduce manual steps and enable/disable Redis per-site via Local’s UI. But there will be gotchas like managing site prefixes and databases, configuring/tuning Redis, and ensuring cross-platform support.

4 Likes

Thanks @nickc! This is awesome, truly above and beyond. Now if someone there wants to create an Add-On… :smiling_face_with_three_hearts:

4 Likes

Also, add to wp-config.php, need unique

define( 'WP_CACHE_KEY_SALT', 'example' );
define( 'WP_REDIS_SELECTIVE_FLUSH', true);

with ‘example’ a random string from https://api.wordpress.org/secret-key/1.1/salt/ for each site.

2 Likes

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.