Filter uncanny-automator

uap_options_cache_miss_ttl

Filters the time-to-live for caching missed items, controlling how long before rechecking.

add_filter( 'uap_options_cache_miss_ttl', $callback, 10, 1 );

Description

Fires when retrieving the cache miss time-to-live (TTL) value. Developers can filter this hook to dynamically adjust how long failed cache lookups are stored before retrying. This is useful for optimizing performance or handling scenarios with frequently changing data. The default TTL is 30 seconds.


Usage

add_filter( 'uap_options_cache_miss_ttl', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

/**
 * Adjust the cache miss TTL for the Automator plugin based on user role.
 *
 * This function modifies the default cache miss time-to-live (TTL) for options
 * in the Automator plugin. It sets a shorter TTL for administrators and a
 * longer TTL for other user roles to potentially improve performance for
 * non-admin users while ensuring administrators see fresher data.
 *
 * @param int $default_ttl The default cache miss TTL in seconds provided by the plugin.
 * @return int The adjusted cache miss TTL in seconds.
 */
add_filter( 'uap_options_cache_miss_ttl', function( $default_ttl ) {
    // Check if the current user is an administrator
    if ( current_user_can( 'manage_options' ) ) {
        // For administrators, reduce the cache miss TTL to 60 seconds
        // to ensure they see the most up-to-date option values.
        return 60;
    } else {
        // For non-administrators, extend the cache miss TTL to 300 seconds (5 minutes)
        // to potentially reduce database queries and improve performance.
        return 300;
    }
}, 10, 1 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/core/classes/automator-options/class-automator-options-cache.php:114

public function get_cache_miss_ttl() {
		return apply_filters( 'uap_options_cache_miss_ttl', self::CACHE_MISS_TTL );
	}


Scroll to Top