title: Simple Html DOM Caching
body: <p>I'm using Simple HTML DOM to scrape (with permission) some websites. I basically scrape around 50 different websites with statistical data which is updated around four times a day.</p>
  
  <p>As you can imagine it takes times to do the scraping and therefore I need to speed up the process by doing some caching.</p>
  
  <p>My vision is:</p>
  
  <p>DATA-PRESENTATION.php // where all the results are shown</p>
  
  <p>SCRAPING.php // the code that makes the job</p>
  
  <p>I want to set up a cron job on SCRAPING.PHP in a way it executes 4 times a day and save all the data in caché which then will be requested by DATA-PRESENTATION.PHP making the experience for the user way faster.</p>
  
  <p>My question is how can I implement this caché thing? I'm very rookie at PHP, I've been reading tutorials but they are not very helpfull and there are just a few so I just couldn't really learn how to do it.</p>
  
  <p>I know other solution might be implementing a database but I don't want to do that. Also, I've been reading about high end solutions like memcached, but the site is very simple and for personal use, so I don't need that kind of stuff.</p>
  
  <p>Thanks!!</p>
  
  <p>SCRAPING.PHP</p>
  
  <pre><code>&lt;?php
  include("simple_html_dom.php");
  
  // Labour stats
  $html7 = file_get_html('http://www.website1.html');
  $web_title = $html7-&gt;find(".title h1");
  $web_figure = $html7-&gt;find(".figures h2");
  
  ?&gt;
  </code></pre>
  
  <p>DATA-PRESENTATION.PHP</p>
  
  <pre><code> &lt;div class="news-pitch"&gt;
   &lt;h1&gt;Webiste: &lt;?php echo utf8_encode($web_title[0]-&gt;plaintext); ?&gt;&lt;/h1&gt;
   &lt;p&gt;Unemployment rate: &lt;?php echo utf8_encode($web_figure[0]-&gt;plaintext); ?&gt;&lt;/p&gt;
   &lt;/div&gt;
  </code></pre>
  
  <p><strong>FINAL CODE</strong>! Many thanks @jerjer and @PaulD.Waite, I couldn't really get this done without your help!</p>
  
  <p>Files:</p>
  
  <p>1- DataPresentation.php <em>// here I show the data requested to Cache.html</em></p>
  
  <p>2- Scraping.php <em>// here I scrape the sites and then save the results to Cache.html</em></p>
  
  <p>3- Cache.html <em>// here the scraping results are saved</em></p>
  
  <p>I set up a Cron Job on Scraping.php telling it to overwrite Cache.html each time.</p>
  
  <p><strong>1- DataPresentation.php</strong></p>
  
  <pre><code>&lt;?php
  include("simple_html_dom.php");
  
  $html = file_get_html("cache/test.html");
  $title = $html-&gt;find("h1");
  echo $title[0]-&gt;plaintext;
  ?&gt;
  </code></pre>
  
  <p><strong>2- Scraping.php</strong></p>
  
  <pre><code>&lt;?php
  include("simple_html_dom.php");
  
  // by adding "-&gt;find("h1")" I speed up things as it only retrieves the information I'll be using and not the whole page.
  $filename = "cache/test.html";
  $content = file_get_html ('http://www.website.com/')-&gt;find("h1");
  file_put_contents($filename, $content);
  ?&gt;
  </code></pre>
  
  <p><strong>3- Cache.html</strong></p>
  
  <pre><code>&lt;h1&gt;Current unemployment 7,2%&lt;/h1&gt;
  </code></pre>
  
  <p>It loads immediately and by setting things this way I assure there's always a Caché file to be loaded.</p>