On Github ethanclevenger91 / wordpress-transients
Created by Ethan Clevenger @ethanclevenger ethanclevenger91
Slow site = high bounce rate
Fast site = fun site!
foreach($foo as $b => $bar) {
  foreach($bar as $f => $fizz) {
    foreach($fizz as $z => $buzz){
      ...
    }
  }
}
  				$this->fetchUrl("https://graph.facebook.com/{$id}/posts?access_token={$tkn}");
          SELECT AUI.idea_id, AUI.title, AUI.events,
  GROUP_CONCAT(IC.category_name SEPARATOR ';') as categories
  FROM (
  	SELECT UI.idea_id, UI.title, GROUP_CONCAT(IE.events SEPARATOR ';') as events ,
  	       CONCAT(';',UI.idea_categories,';') as temp_categories
   	FROM user_idea UI
  	LEFT JOIN idea_events IE ON UI.idea_id = IE.idea_id
  	GROUP BY UI.idea_id
  ) AS AUI
  INNER JOIN idea_categories IC
  ON AUI.temp_categories LIKE CONCAT('%;',IC.category_id,';%')
  GROUP BY AUI.idea_id;
          Any time a little outdated information isn't a big deal
$mPosts = new WP_Query(['post_type'=>'post', 'posts_per_page'=>-1]); if($mPposts->have_posts()): while($mPosts->have_posts()): $mPosts->the_post(); echo '<div class="post">'; echo '<h2>'.get_the_title().'</h2>'; the_content(); echo '</div>'; endwhile; endif;
$mPosts = get_transient('my_posts');
if($mPosts === false) {
  $mPosts = new WP_Query(['post_type'=>'post', 'posts_per_page'=>-1]);
  set_transient('my_posts', $mPosts, DAY_IN_SECONDS);
}
if($mPosts->have_posts()): while($mPosts->have_posts()): $mPosts->the_post();
  echo '<div class="post">';
  echo '<h2>'.get_the_title().'</h2>';
  the_content();
  echo '</div>';
endwhile; endif;
          $mPosts = get_transient('my_posts');
if($mPosts === false) {
  $mPosts = new WP_Query(['post_type'=>'post', 'posts_per_page'=>-1]);
  set_transient('my_posts', $mPosts, DAY_IN_SECONDS);
}if($mPosts->have_posts()): while($mPosts->have_posts()): $mPosts->the_post();
  //make it look pretty
endwhile; endif;
          $mPosts = getMyPosts();
function getMyPosts() {
  $mPosts = get_transient('my_posts');
  if($mPosts === false) {
    $mPosts = refreshPostsTransient();
  }
  return $mPosts;
}
          function refreshPostsTransient() {
  $mPosts = new WP_Query(['post_type' => 'post', 'posts_per_page' =>-1]);
  set_transient('my_posts', $mPosts, DAY_IN_SECONDS);
  return $mPosts;
}
          add_action('save_post', 'refreshPostsTransient');
          add_action('save_post', [$this, 'refreshPostsTransient']);
          What if my transient takes a really long time to generate?
We are, of course, talking about optimization here and we want to be scalable, and we don't want that one user each hour or day or whatever waiting forever for this thing to be generated. Why are we doing this during a real user session? That's a good question, and it's why these don't scale super well out of the box. We want things to refresh when they expire, but we also want it to refresh before someone is waiting on it, which is how the transient currently functions. It doesn't refresh the transient until after the information is no good. Let's beat it to the punch.http://mysite.com?mytransient=refresh
add_action('init', [$this, 'maybe_refresh_transient']);
            public function maybe_refresh_transient() {
  if('refresh' === $_GET['mytransient']) {
    $this->refreshPostsTransient();
  }
}
            Awesome. If we set this cron to run every 50 minutes, it can do all the data processing and reset the transient in the background, without any users waiting on it.public function getResults($platform) {
  $results = get_transient("social_feeds_results_{$platform}");
  if(false === $results) {
    $results = array();
    switch($platform) {
      case 'twitter' :
      $results = $this->_getResultsTwitter();
      break;
      case 'facebook' :
      $results = $this->_getResultsFacebook();
      break;
    }
    set_transient("social_feeds_results_{$platform}", base64_encode(maybe_serialize($results)), HOUR_IN_SECONDS);
  }
  return maybe_unserialize(base64_decode($results));
}
$tweets = $socialFeeds->getResults('twitter');
      	  <img src="data: image/jpeg;base64,/9j...9k=">
function fetchLastThousandInstagramPicturesTransient() {
  $images = get_transient('thousand_instagram_pictures');
  if($images === false) {
    $images = fetchLastThousandInstagramPictures(); //an array of URLs
    $images = dataUriEncodeImages($images);
    set_transient('thousand_instagram_pictures', $images, HOUR_IN_SECONDS);
  }
  return $images;
}
          function dataUriEncodeImages($images) {
  $imagesAsData = [];
  foreach($images as $image) {
    $imagesAsData[] = getDataURI($image);
  }
  return $imagesAsData;
}
          function getDataURI($image) {
  return 'data: '.mime_content_type($image).';base64,'.base64_encode(file_get_contents($image));
}
          $images = fetchLastThousandInstagramPicturesTransient();
foreach($images as $image) {
  echo '<img src=" '.$image.'">';
}