WordPress.org

Ready to get started?Download WordPress

Forums

WP File Cache
Issue with Transient Value (7 posts)

  1. Adam Capriola
    Member
    Posted 2 years ago #

    I am getting this error on my pages when view them for a 2nd time:

    Warning: unserialize() [function.unserialize]: Node no longer exists in /var/www/html/tc/tcgscans.com/wp-content/plugins/wp-file-cache/lib/class.FileCache.php on line 193

    And this is the function that is causing the issue:

    // Get Amazon Price
    function get_amazon_price($asin) {
    	$transName = "amzn-transient-$asin"; // Name of value in database.
    	$cacheTime = 24; // Time in hours between updates.
    
    	// Do we already have saved data? If not, lets get it.
    	if ( false === ( $trans = get_transient($transName) ) ) :
    
    		// The Function to get Price
    		$public_key = 'AKIAJ7UYHSJ4L3Q2KINQ';
    		$private_key = 'nSPy3581vu0ZyV6M5mZ+Hw+AboU43SCY2AED9+Lm';
    		$pxml = aws_signed_request( 'com', array( 'Operation' => 'ItemLookup', 'ItemId' => $asin, 'ResponseGroup' => 'OfferSummary', 'Availability' => 'Available',	'Condition' => 'All', 'AssociateTag' => 'adamcapr-20', ), $public_key, $private_key );
    
    		if ( $pxml === False ) {
    		    $trans = 'n/a';
    		}
    		else {
    		    if ( isset( $pxml->Items->Item->OfferSummary->LowestNewPrice->FormattedPrice ) ) {
    		        $trans = $pxml->Items->Item->OfferSummary->LowestNewPrice->FormattedPrice;
    		    }
    		    else {
    		        $trans = 'n/a';
    		    }
    		}
    
    		// Save our new transient, plus save it in the longer "backup" transient.
    		set_transient($transName, $trans, 60 * 60 * $cacheTime);
    
    	endif;
    
    	return $trans;
    }

    And I call it within my templates like this:

    if ( get_post_meta($post->ID, 'asin', true) ) {
    	$asin = get_post_meta($post->ID, 'asin', true);
    
    	$out .= '<h4>Price on Amazon: ';
    	if ( function_exists('get_amazon_price') ) $out .= get_amazon_price($asin);
    	$out .= '</h4>';
    }

    Basically I save a transient value, and call it within my template. On the first page load it shows up ok, but on subsequent views the error shows.

    Any idea how to fix the issue? Thanks!

  2. Vladimir Kolesnikov
    Member
    Plugin Author

    Posted 2 years ago #

    Hi Adam,

    Could you please add

    var_export($trans);

    before

    set_transient($transName, $trans, 60 * 60 * $cacheTime);

    and post its value here?

    I suspect that $trans references a non-persistent resource and hence it cannot be correctly unserialized.

    Thank you,
    Vladimir

  3. Adam Capriola
    Member
    Posted 2 years ago #

    Wow, thank you for the super quick reply Vladimir!

    Here's an example value being returned:

    SimpleXMLElement::__set_state(array( 0 => '$3.95', ))

  4. Vladimir Kolesnikov
    Member
    Plugin Author

    Posted 2 years ago #

    OK, please try

    $trans = (string)$pxml->Items->Item->OfferSummary->LowestNewPrice->FormattedPrice;

    instead of

    $trans = $pxml->Items->Item->OfferSummary->LowestNewPrice->FormattedPrice;

    I think this should solve the issue.

    Best regards,
    Vladimir

  5. Adam Capriola
    Member
    Posted 2 years ago #

    Perfect! Everything seems to be working now.

    I noticed that the transient values do not seemed to be saved in the wp_options table when your plugin is active. Is this normal?

    I see them all within /wp-content/plugins/wp-file-cache/cache/transient, but they are not in wp_options. The way my function is written, I use get_transient() to check for an existing value, and if it's not there then generate a new transient value.

    I'm afraid that it'll keep generating a new value if it's not in wp_options... is your plugin written to get around that?

    Thanks again for the help, and is there any way I can donate a few bucks your way?

    Best,
    Adam

  6. Vladimir Kolesnikov
    Member
    Plugin Author

    Posted 2 years ago #

    Hi Adam,

    Transients are not saved to the database when an object cache is active - this is by design (WordPress).

    If you look at get_transient's code, you will see that it uses two different code paths depending on whether $_wp_using_ext_object_cache global is set to true. If it is, transients are never written to the database and are stored/restored from the cache. If it is not, wp_options table is used. This minimizes the traffic between the PHP and DBMS and does not clutter the database with garbage.

    If you want transients to be stored to the database, you need to modify plugin's wp_cache_init() function a bit: add

    global $_wp_using_ext_object_cache; $_wp_using_ext_object_cache = false;

    Thanks again for the help, and is there any way I can donate a few bucks your way?

    Unfortunatley, PayPal does not like Ukraine :-( if you want, you can try Donate form on my blog (http://blog.sjinks.pro/) but I honestly have no idea whether it will work for US.

    Thank you,
    Vladimir

  7. Adam Capriola
    Member
    Posted 2 years ago #

    Hi Vladimir,

    Awesome, gotcha. That's really awesome how the design works, that transient values aren't saved in the DB. Smart!

    Also I just tried to send a donation, but I cannot get it to work. I put in my phone number but received no password to continue. :(

    I really appreciate your help and plugins though!

    Best,

    Adam

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic