Support » Plugin: WP Redis » Adding non persistent groups doesn’t always work

  • Martin Krcho

    (@martinkrcho)


    I think I found an issue with your code that handles non persistent groups.

    I need to prevent Redis from caching “alloptions” value, so I call the following code as early as possible (muplugins_loaded hook).

    wp_cache_add_non_persistent_groups( array(
    	'options',
    	'main_site-options'
    ) );

    Despite this, the “alloptions” value still comes from the internal $cache array in the WP_Object_Cache object in your plugin.

    This is happening because the get function in WP_Object_Cache object first checks internal $cache and then checks whether it should be persisted.

    I believe there are 2 possible fixes:
    * change logic in WP_Object_Cache::get to check if the group is persistent before trying to retrieve it from internal cache
    * change function WP_Object_Cache::add_non_persistent_groups to delete all already cached values that belong to one of the groups being added

Viewing 2 replies - 1 through 2 (of 2 total)
  • Martin Krcho

    (@martinkrcho)

    Here’s suggested patch for the function WP_Object_Cache::add_non_persistent_groups:

    public function add_non_persistent_groups( $groups ) {
    
    	$groups = (array) $groups;
    
    	$groups = array_fill_keys( $groups, true );
    	$this->non_persistent_groups = array_merge( $this->non_persistent_groups, $groups );
    
    	foreach (array_keys($groups) as $group) {
    
    		foreach ($this->cache as $cacheKey => $cacheValue) {
    
    			$pattern = '/^'.WP_CACHE_KEY_SALT.$group.':/';
    			if (preg_match($pattern, $cacheKey)) {
    				unset($this->cache[$cacheKey]);
    			}
    
    		}
    
    	}
    
    }
    Plugin Author Daniel Bachhuber

    (@danielbachhuber)

    Hi @martinkrcho,

    Thanks for the report.

    I need to prevent Redis from caching “alloptions” value, so I call the following code as early as possible (muplugins_loaded hook).

    Can you explain why you want to prevent Redis from caching ‘alloptions’?

    The muplugins_loaded hook is likely too late to use wp_cache_add_non_persistent_groups(), as options will already have been loaded by that point.

    Here’s suggested patch for the function

    You’re welcome to use this in a fork of the plugin. I’m not sure it makes sense to include in the main plugin as it will cause the plugin’s behavior to deviate from WordPress core.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Adding non persistent groups doesn’t always work’ is closed to new replies.