• Kim

    (@kimikaze)


    I have noticed an issue with Polylang and persistent object caching. I am using W3 Total Cache. The problem is with the default widgets, for example list of categories. When switching language you still see the previous languages until the cache expires. What would be the best solution to this? Turn off caching for those groups? One idea I had was to use wp_cache_switch_to_blog() to set a unique id for every language, so each language could have it’s own set of persistent object caching.

    add_action('init', 'set_language_cache');
    function set_language_cache(){
    	$current_language_slug = pll_current_language( 'slug' );
    	$default_language_slug = pll_default_language( 'slug' );
    
    	if ( $current_language_slug == $default_language_slug ) {
    		return;
    	}
    
    	$languages = pll_the_languages(array('raw' => true));
    	foreach ( $languages as $language ){
    		if ( $language['slug'] == $current_language_slug ) {
    			$current_language_id = $language['id'];
    			break;
    		}
    	}
    
    	if ( ! empty( $current_language_id ) ) {
    		$blog_id = get_current_blog_id();
    		$new_blog_id = (int)($blog_id . '000' . $current_language_id);
    		wp_cache_switch_to_blog( $new_blog_id );
    	}
    }

    Something like this seems to work but not sure if it is a good solution, so would be interesting to hear how others solve this. Maybe other caching plugins got better solutions with Polylang support?

    https://wordpress.org/plugins/polylang/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Chouby

    (@chouby)

    I am surprised that this is reported only now… Yes there are issues with persistent object caching of W3 Total cache. It seems to be a common issue with other multilingual plugins

    Are you testing on a multisite? Looking at code of wp_cache_switch_to_blog(), I have seen that WP first checks that the install is multisite, so I expect your solution working only on multisite.

    There is still the solution to disable the caching for relevant groups.

    NB: To understand the issue, WordPress generally allows plugins to modify the database query but the cache key does not take these modifications into account. So the same database query in two different languages has exactly the same cache key 🙁

    I did not test other plugins for persistent object cache. But they are likely to have the same issue. Except if they offer a filter on the cache key which is not the case for WP and W3TC.

    Thread Starter Kim

    (@kimikaze)

    The code was tested on a local single install, and not on a multisite network. It seemed to work for the few minutes I tested it. It showed the correct language content in the widgets, and it used cached data in every language the second time the pages was loaded. I’m just afraid there are some negative consequences of doing this. I expect wp_cache_switch_to_blog() is intended to be used with multisite networks, but on the other hand it is also the function taking over from the deprecated wp_cache_reset().

    Thread Starter Kim

    (@kimikaze)

    Hm it should not work and I don’t understand why it does on my local installation. The blog_prefix is not changed by the code so it is only a strange coincidence that it worked it seems.

    Thread Starter Kim

    (@kimikaze)

    Ah now I see. global $wp_object_cache is the class W3_ObjectCacheBridge and not the WordPress class WP_Object_Cache.

    Plugin Author Chouby

    (@chouby)

    Ah now I see. global $wp_object_cache is the class W3_ObjectCacheBridge and not the WordPress class WP_Object_Cache.

    I was also looking at WordPress code when writing my post above 😀

    Thread Starter Kim

    (@kimikaze)

    I guess there might be caching plugin specific workarounds or solutions, but not a reliable way in WordPress core that can solve this.

    Plugin Author Chouby

    (@chouby)

    Yes. I examined other object cache plugins (apc cache, memcache) and they all (quite logically) implement the switch_to_blog function the same way as WordPress.

    That means that your solution seems to work only with W3 total cache.

    Plugin Author Chouby

    (@chouby)

    I tried to include a better support of object cache in the beta version of Polylang 1.5. I made tests with W3 total cache and it’s ok for me now with the list of categories, tag cloud, recent posts and recent comments (I believe that “only” these 4 default widgets had issue with object caching). Feel free to test and report your experience.
    More info on: http://polylang.wordpress.com/2014/04/21/polylang-1-5-beta/

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Polylang and object caching’ is closed to new replies.