Wanted some opinions on a solution I’m trying to come up with:
I have a fan/enthusiast plugin I’m working on for fans of TV shows. One of the custom post types attached to the plugin is a release type which stores data on dvd releases of a show. Some of the meta data that I weant stored is info from Amazon using their Product Advertising API. I wanted to cache some of the pricing data available there, such as the current price of the dvd.
I was thinking of using transients but from what I can tell their nothing more than a layer on WP options; their too global. I’d have to create numerous transients with a naming scheme that would point to a certain post.
The other option would be to use custom fields but use the CRON API to update it, I’d either have one Cron job update all the custom fields hourly or create one cron job per post that did so.
My question is which solution would be more practical performance and convenience wise?
3 Answers
I think I would write a wrapper function around custom fields, something like this:
function get_post_transient( $post_ID, $meta_key, $update_function ) {
$current_value = get_post_meta( $post_ID, $meta_key, true );
if ( is_array( $current_value ) && $current_value['expiration'] < date('U') )
return $current_value['data'];
$new_value = call_user_function( $update_function, $post_ID );
update_post_meta( $post_ID, $meta_key, $new_value );
return $new_value['data'];
}
and save the meta as an array, of the form:
array(
'expiration' => //timestamp that this field exires
'data' => // all the custom data you want to save.
);
You would just need to have your update function return the same array structure. I don’t know if there’s any real benefit of this approach over using the options table through transients, but it would make more sense to me to have post meta stored in the postmeta table.