• Resolved danmuc

    (@danmuc)


    I am using W3 Total Cache Plugin in WordPress and setted up Object-Cache to use Memcached.

    I created a test file which should store the result of a complex operation via wp_cache_set() and will use it in future script calls via wp_cache_get() to save execution time. Everything is working fine when i call the script via browser on first call it’s generated and then loaded from cache (i know it because i added a cache_status field to my json output and of course i see that first call is much slower).

    The problem is that when i call the file via CLI then the cache status is still false in all future calls which means that wp_cache_get() is not able to find the key in memcached server.

    I already checked, that Memcached module is enabled in the CLI php.ini and can be used within php scripts.

    I think that W3TC might adds some prefix to my memcached key which is based on the environment (maybe hostname or something) which of course is different in cli environment. But i did not find any documentation about that and have no clue how to debug. I found out that item key includes some variables like host, blog_id and module for example but i am not sure where they are defined: https://github.com/crowdfavorite-mirrors/wp-w3-total-cache/blob/master/Cache_Base.php#L190

    Setting WP_CACHE_KEY_SALT also did not had any effect.

    Does anyone have experience with that kind of problem?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter danmuc

    (@danmuc)

    I investigated that problem a little bit more and it’s reproducable. I also turned on memcached verbose logging and found out that dbcache is working on cli but there is not any GET or SET command incoming which is related to objectcache…

    How to reproduce..

    Create test.php in your wordpress main directory

    <?php
    require_once ('wp-load.php');
    
    $cached_object = wp_cache_get('test','test');
    
    if ($cached_object === FALSE){
    	echo 'cached object not found setting it';
    	wp_cache_set('test', 'test '.date("D.m.Y - H:i:s"), 'test', 86400);
    }else{
    	echo 'cached object found: '.$cached_object;
    }

    Call test.php via Browser… at first call it will show you not found and will set key.. on second call it will found old cache entry and show you old time.

    Now open terminal and cd to wordpress directory enter “php test.php” you will always see message “cached object not found”

    • This reply was modified 4 years, 11 months ago by danmuc.
    Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello,

    The host is used for the key.
    So for cli, you should set up that variable. or call your script via curl.

    Thread Starter danmuc

    (@danmuc)

    Hi Marko,

    thanks for your reply. Calling via curl is no option for us because we want to save nginx ressources.

    Is there any way how we can define this host variable in functions.php or via a hook or something else?

    But i am wondering why there is also no call to memcached server. When the hostname for key is used and hostname is empty in php-cli then i expect that maybe the key for the stored object is something like 123123__objectcache_[…] instead of 123123_hostname.com_objectcache so the hostname is empty which should lead to that the php-cli would also cache on second request via cli? But it does not you can test it with my script which i sent you above. I guess it’s a bug inside w3tc

    Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello,
    Sorry for the late reply.
    The object cache is disabled in console mode
    You need to comment out lines

    if ( PHP_SAPI === 'cli' ) {
       $this->cache_reject_reason = 'Console mode';
    
                return false;
            }

    in ObjectCache_WpObjectCache_Regular.php file to make your script working.
    We will add an option to control that via config in next version

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Why is Memcached Object Cache not working in W3TC Plugin for WordPress via CLI?’ is closed to new replies.