• Hi, I’m working on a small plugin that needs to distinguish between an empty option and an option that is not set. This is normally done reliably by looking at what get_option() returns: an empty value ” (when empty) or the boolean false (when not found).

    I have noticed that when Object caching (in my case via Redis socket, have not tested other scenarios) is on, this behavior is not always consistent.

    When the empty value is fetched from the DB and stored by the cache, the response is always ” (correct) but right after saving the empty option from admin, get_option( 'my_setting' ) starts responding with false instead of ” (not correct!).

    Only after a cache purge, the correct response is back. Or if I disable the Object caching on the admin entirely, of course…

    Is this a known issue?

Viewing 15 replies - 1 through 15 (of 16 total)
  • Plugin Support qtwrk

    (@qtwrk)

    <?php
    require('./wp-load.php');
    update_option('test_option', 'test_value');
    echo '<pre>' . var_dump(get_option('test_option')) . '</pre>';
    echo "update to empty<br><br>";
    update_option('test_option', '');
    echo '<pre>' . var_dump(get_option('test_option')) . '</pre>';

    above test code returns

    string(10) "test_value"
    update to empty

    string(0) ""

    seems alright on me

    please explain a bit what exactly did you do ?

    a sample code would be great help though.

    • This reply was modified 11 months, 1 week ago by qtwrk.
    • This reply was modified 11 months, 1 week ago by qtwrk.
    Thread Starter Rolf Allard van Hagen

    (@ravanh)

    Okay I found what is going “wrong”:

    In the plugin, I have a sanitize routine that returned false when the option is empty. In normal operations, without Object cache, this is stored in the DB as an empty option. Then fetched again, it shows an empty result (not false) but apparently when added to the object cache, this conversion from false to empty does not take place.

    So for testing purposes, try:

    <?php
    require('./wp-load.php');
    update_option( 'test_option', 'test_value' );
    update_option( 'test_option', false );
    var_dump( get_option( 'test_option' ) );

    I have not tested this exact code sample but I suspect that with the object cache active, the dumped variable shows bool(false), while without object cache or after a purge it will be string(0) "".

    Plugin Support qtwrk

    (@qtwrk)

    the one you provided , show bool false on me , with and without object cache

    Thread Starter Rolf Allard van Hagen

    (@ravanh)

    After update_option( 'test_option', false ) the option is stored in the database as an empty value, correct? Then get_option( 'test_option' ) should not be returning false but ''. It should only return false if the option is not found in the database…

    The only difference between the test code and my plugin is that the option’s autoload is set to auto-on (via a wp_default_autoload_value filter) … Please try with the autoload flag:

    update_option( 'test_option', 'test_value', true );
    update_option( 'test_option', false, true );
    var_dump( get_option( 'test_option' ) );

    I tested this and it does show different results with and without the object cache. At least on my dev site.

    Hope this helps πŸ™‚

    Plugin Support qtwrk

    (@qtwrk)

    with your latest code, I tested again , on a clean, new installed WP (no LiteSpeed plugin installed) , it still return bool false … 🧐

    Thread Starter Rolf Allard van Hagen

    (@ravanh)

    That is weird. Is the option actually in the DB?

    Plugin Support qtwrk

    (@qtwrk)

    yes, I looked in DB, it’s there and empty , have you tried on a clean wp ? what does it show to you ?

    Thread Starter Rolf Allard van Hagen

    (@ravanh)

    Interesting…

    Try this:

    update_option( 'test_option_1', 'test_value' );
    update_option( 'test_option_1', false );
    var_dump( get_option( 'test_option_1' ) );

    update_option( 'test_option_2', 'test_value', true );
    update_option( 'test_option_2', false, true );
    var_dump( get_option( 'test_option_2' ) );

    update_option( 'test_option_3', 'test_value', false );
    update_option( 'test_option_3', false, false );
    var_dump( get_option( 'test_option_3' ) );

    My result are:

    bool(false) bool(false) string(0) ""

    So I would guess the inconsistency is within WordPress itself, not the object caching?

    Plugin Support qtwrk

    (@qtwrk)

    bool(false) bool(false) string(0) ""

    okay …. this set of code gives me this result , with and without object cache …

    Thread Starter Rolf Allard van Hagen

    (@ravanh)

    Even more interesting:

    var_dump( get_option( 'test_option_1' ) );
    update_option( 'test_option_1', 'test_value' );
    update_option( 'test_option_1', false );
    var_dump( get_option( 'test_option_1' ) );

    var_dump( get_option( 'test_option_2' ) );
    update_option( 'test_option_2', 'test_value', true );
    update_option( 'test_option_2', false, true );
    var_dump( get_option( 'test_option_2' ) );

    var_dump( get_option( 'test_option_3' ) );
    update_option( 'test_option_3', 'test_value', false );
    update_option( 'test_option_3', false, false );
    var_dump( get_option( 'test_option_3' ) );

    Gives:

    string(0) "" bool(false) string(0) "" bool(false) string(0) "" string(0) ""

    Thread Starter Rolf Allard van Hagen

    (@ravanh)

    So it’s in the WP core options cache where the inconsistency occurs…

    Anyway, not an object cache issue. Thanks for looking into this with me πŸ™‚

    Plugin Support qtwrk

    (@qtwrk)

    update_option( 'test_option_1', 'test_value' );
    update_option( 'test_option_1', false );
    var_dump( get_option( 'test_option_1' ) );

    if I do this , it gives bool false , if I do

    #update_option( 'test_option_1', 'test_value' );   # no update on this one  
    update_option( 'test_option_1', false );
    var_dump( get_option( 'test_option_1' ) );

    then it gives string “”

    it just looks like it doesn’t like the way that you update the same option multiple times in same request ?

    • This reply was modified 11 months, 1 week ago by qtwrk.
    Thread Starter Rolf Allard van Hagen

    (@ravanh)

    it just looks like it doesn’t like the way that you update the same option in same request ?

    Indeed. See my answer just before where the code starts with getting the option from the DB and then updating with different dump…

    Thread Starter Rolf Allard van Hagen

    (@ravanh)

    I guess that with the Object cache on, this “odd” behavior is made persistent across requests. Not the object caches fault, just that it made it apparent (I’m not updating the option twice in one request but only after Save on the options page)

    Thread Starter Rolf Allard van Hagen

    (@ravanh)

Viewing 15 replies - 1 through 15 (of 16 total)

The topic ‘Object cache bug?’ is closed to new replies.