Customizations

  1. Dashboard widget
  2. Automatic cache flushing
  3. Preventing cache flushing
  4. Identifying cache flushers
  5. Automatic drop-in updates
  6. Drop-in validation
  7. Filesystem health check
  8. Hide plugin setting pointer
  9. Default plugin settings
  10. Analytics charts
  11. Dashboard widget charts
  12. Transient cleanup
  13. Analytics comment
  14. REST capability

Dashboard widget

By default the dashboard widget is visible to all users with the objectcache_manage capability. In some scenarios you might want to hide the dashboard widget, or restrict access to certain roles.

/**
 * Remove the Object Cache Pro dashboard and network-dashboard widget.
 */
add_filter( 'objectcache_dashboard_widget', '__return_false' );
add_filter( 'objectcache_network_dashboard_widget', '__return_false' );

Automatic cache flushing

By default the object cache will be flushed whenever the drop-in is enabled or disabled, as well as when the plugin is deactivated. This helps prevent avoid stale cache data and potential data loss and fatal errors. You can alter this behavior using the objectcache_autoflush filter.

/**
 * Disable automatic object cache flushes.
 */
add_filter( 'objectcache_autoflush', '__return_false' );

Preventing cache flushing

Some plugins neurotically flush the entire cache, instead of writing good code. You can circumvent that using the pre_objectcache_flush filter.

/**
 * Prevent you’re mom from flushing the object cache.
 */
add_filter( 'pre_objectcache_flush', function (bool $should_flush, array $backtrace) {
    if ($backtrace[0]['function'] === 'ur_moms_plugin\flush') {
        return false;
    }

    return $should_flush;
}, 10, 2 );

The same can be done for group flushes using pre_objectcache_flush_group:

/**
 * Prevent you’re mom from flushing the object cache.
 */
add_filter( 'pre_objectcache_flush_group', function (bool $should_flush, string $group, array $backtrace) {
    // ...

    return $should_flush;
}, 10, 2 );

Identifying cache flushers

Object Cache Pro records the last 10 cache flushes and tries to identify who it was. Sometimes this can be ugly function names and you may want show a more readable name.

add_filter( 'objectcache_flushlog_caller', function (string $caller, string $backtrace) {
    if (str_contains($caller, 'acf()->') {
        return 'ACF';
    }

    return $caller;
}, 10, 2 );

Automatic drop-in updates

By default the object cache drop-in will be kept up-to-date (unless DISALLOW_FILE_MODS is set to true). You can alter this behavior using the file_mod_allowed filter.

/**
 * Disable automatic object cache drop-in updates.
 */
add_filter( 'objectcache_allow_dropin_mod', '__return_false' );

Drop-in validation

In some rare scenarios you might wish to disable the drop-in validation or drop-in version check. You can do so with the objectcache_validate_dropin and objectcache_validate_dropin_version filters.

/**
 * Force the object cache drop-in to be valid.
 */
add_filter( 'objectcache_validate_dropin', '__return_false' );

/**
 * Force object cache drop-in version to be up-to-date.
 */
add_filter( 'objectcache_validate_dropin_version', '__return_false' );

Filesystem health check

One of the plugin’s health checks tests for filesystem access to ensure the object-cache.php drop-in can be seamlessly updated.

This health check temporarily creates a .object-cache-test.tmp file in WP_CONTENT_DIR. To disable this health check you can use the objectcache_check_filesystem filter.

/**
 * Disable filesystem health check.
 */
add_filter( 'objectcache_check_filesystem', '__return_false' );

Hide plugin setting pointer

You can hide the pointer that informs users of the settings menu either for everyone or just some users using the objectcache_omit_settings_pointer filter.

/**
 * Disable settings pointer in Dashboard.
 */
add_filter( 'objectcache_omit_settings_pointer', '__return_true' );

Default plugin settings

The default plugin settings can be altered using the objectcache_default_options filter.

/**
 * Filters the default interval for object cache analytics.
 *
 * @param  int  $interval  The interval, in seconds.
 */
add_filter( 'objectcache_default_options', fn ($defaults) => [
    'channel' => 'stable',
    'flushlog' => false,
] );

You can even set values using WP CLI:

wp option pluck objectcache_options channel
wp option patch update objectcache_options channel 'beta'

Analytics charts

The cache analytics charts can be customized on a per-user basis using the “Screen Options”. To customize the default use these filters:

/**
 * Filters the default interval for object cache analytics.
 *
 * @param  int  $interval  The interval, in seconds.
 */
add_filter( 'objectcache_analytics_interval', fn () => 15 );

 /**
 * Filters the intervals for object cache analytics.
 *
 * The array keys represent the resolution in seconds
 * and the values are the number of intervals.
 *
 * @param  array  $intervals  The intervals.
 */
add_filter( 'objectcache_analytics_intervals', fn () => [
    10 => 30,
    60 => 30,
    300 => 24,
] );

/**
 * Filters the default order and available metrics on the object cache dashboard.
 * Use `default_hidden_meta_boxes` filter to hide metrics by default without removing them.
 *
 * @param  array  $metrics  The available metrics.
 */
add_filter( 'objectcache_dashboard_metrics', function ( $metrics ) {
    return array_diff($metrics, [
        'redis-tracking-clients',
        'redis-rejected-connections',
    ]);
} );

Dashboard widget charts

The Dashboard widget’s metrics can be disabled entirely:

add_filter( 'objectcache_widget_metrics', '__return_empty_array' );

Or customized to show any charts available to Object Cache Pro:

add_filter( 'objectcache_widget_metrics', function ( $metrics ) {
    return [
        'redis-tracking-clients',
        'redis-rejected-connections',
    ];
} );

Transient cleanup

To disable transients being deleted from the database when the object cache drop-in is enabled, use the objectcache_cleanup_transients filter:

add_filter( 'objectcache_cleanup_transients', '__return_false' );

Analytics comment

By default a small comment will be printed at the bottom of each page containing metrics and insensitive metadata:

<!-- plugin=object-cache-pro client=relay metric#hits=977 metric#misses=17 ... -->

To disable the comment you can use the analytics.footnote configuration option, or the objectcache_omit_analytics_footnote filter:

add_filter( 'objectcache_omit_analytics_footnote', '__return_true' );

REST capability

To change the capability needed to access the /wp-json/objectcache/ REST API endpoints.

add_filter( 'objectcache_rest_capability', function () {
  return 'read' // anybody
  return 'do_not_allow' // nobody
} );

When restricting access, be mindful that the analytics widgets and object cache tools need the REST API to function.