第一个实现是在一个名为the_content_cached
.
function the_content_cached( $more_link_text = null, $strip_teaser = false ) {
$post_id = get_the_ID();
// Not possible to tie content to post ID
if ( ! $post_id ) {
the_content( $more_link_text, $strip_teaser );
return;
}
$cached = wp_cache_get( $post_id, \'the_content\' );
// Cache miss
if ( ! $cached ) {
add_filter( \'the_content\', \'tiny_cache_save_the_content\', PHP_INT_MAX );
the_content( $more_link_text, $strip_teaser );
remove_filter( \'the_content\', \'tiny_cache_save_the_content\', PHP_INT_MAX );
return;
}
// Cache hit
$timestamp = gmdate( \'c\' );
$message_tpl = \'<!-- Cached content generated by Tiny cache on %s -->\';
print $cached;
printf( $message_tpl, $timestamp );
}
// Save the content to the object cache
function tiny_cache_save_the_content( $content ) {
$post_id = get_the_ID();
// Tie content to post ID
if ( $post_id ) {
wp_cache_set( $post_id, $content, \'the_content\' );
}
return $content;
}