Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Should I use Transients in my queries like this?

<?php 
if ( ! $my_paged = absint( get_query_var( 'paged' ) ) )
$my_paged = 1;

if ( ! $my_query = get_transient( "567recent_$my_paged" ) ) {
    $my_query = new WP_Query ( "cat=2526&posts_per_page=1&paged=$my_paged" );
    set_transient( "567recent_$my_paged", $my_query, 60 * 60 );
}
while ($my_query->have_posts()) : $my_query->the_post(); ?> 

If so, could I also use W3 Total Cache with database caching and with APC enabled?

share|improve this question

closed as off-topic by G. M., Johannes Pille, birgire, EAMann Aug 22 at 16:56

This question appears to be off-topic. The users who voted to close gave these specific reasons:

If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

The Transient API saves data inside the database, which isn't as effective as APC, but a similar workflow as used for transients can be used for APC. It is important to check if APC is available and active, so its functions are usable. Because this isn't always the case, it is to consider to add a alternative to APC via Transient API and combine those two caching options in one workflow. Which would assure that there is a caching mechanism available, even if APC isn't available - this is good practice if your planing to distribute your code and want to make sure that caching works on different environments. If it is only for your own requirements and you have control over the setup, with APC on your server availabel and activated, then you can use only the APC variant, because you know it will work and it is much faster.

Below code shows the exemplary usage of APC:

Set Caching

WP Transient API:

set_transient( $transientname,$go,3600 );

with APC:

apc_add( $transientname,$go,3600 );

Also for deleting

WP Transient API:

delete_transient( $transientname );

with APC:

apc_delete( $transientname );

Caching Queries

$cachequery  = 'blog_id_' . $blog->ID;
$cacheexpire = 1800;

if ( $mypost = apc_fetch( $cachequery ) ) {
   echo "<!-- cached query -->";
} else {
   $mypost = query_posts( $args );
   apc_store( $cachequery, $mypost, $cacheexpire );
   echo "<!-- caching this -->";
}

APC Documentation

See the documentation of APC for more information and functions.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.