Documentation / Tutorials / Tutorials & How to’s

Using Transient For Saving Module Outputs

/ Tutorials & How to’s / Using Transient For Saving Module Outputs

In some cases, it is a good idea to use transient to temporarily cache the parsed output of a module to improve the performance, or if you are running expensive queries whose output do not change frequently.

Here is the strategy to use transient with Awesome Studio.

[aw2.template wisdom]
  [aw2.client less]
    body{
       background-color: #fff1e5;
    }   
  [/aw2.client]  
  <div class='container'>
    <div class='row'>
      <div class='col-xs-12'>
          <h1 class='text-center'>Think!</h1>
      </div>
    </div>
    <div class='row'>
      <div class='col-xs-12 col-md-5'>
        [aw2.query posts_builder part=start]
          {
          	"posts_per_page": 1,
          	"post_type": "wisdom_nugget",
          	"post_status": "publish",
          	"orderby": "rand"
          }
        [/aw2.query] 
        [aw2.query posts_builder part=tax_query]
          {
          	"taxonomy": "nugget_type",
          	"field": "slug",
          	"terms": "question"
          }
        [/aw2.query] 
        [aw2.query posts_builder part=run set=results /]
          <blockquote class="pullquote pullquote-question">
            [aw2.loop results.posts]
              [aw2.get item.the_content /]
            [/aw2.loop]  
          </blockquote>
      </div>
  </div>
[/aw2.template]

[aw2.template main]
[aw2.get function.get_transient p1="wisdom-layout" set='module.wisdom' /]
[aw2.if  empty="{module.wisdom}"]
  [aw2.run templates.wisdom set='module.wisdom' /]
  [aw2.get function.set_transient p1="wisdom-layout" p2="{module.wisdom}" p3="86400"/]

[/aw2.if]  
[aw2.get module.wisdom /]
[/aw2.template]

Here is how it works

  1. Instead of directly outputting the module, I have wrapped the code within a template 'wisdom'
  2. Then I create "main" template, as it is executed by default
  3. In the main template, I  fetched the data from transient using get_transient function and saved it to 'module.wisdom' variable.
  4. I then check if transient returned a false or not if false was returned then I executed the template wisdom and save them out into 'module.wisdom' variable, and saved the output to transient using the set_transient function.
  5. Finally, I output the value stored in 'module.wisdom'.
Categories
Most Popular

Leave a Reply

Your email address will not be published. Required fields are marked *