<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:refactormycode.com,2007:users483</id>
  <link type="application/atom+xml" href="http://refactormycode.com/users/483" rel="self"/>
  <title>DevP</title>
  <updated>Tue Jan 29 16:01:34 +0000 2008</updated>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor1880</id>
    <published>2008-01-29T16:01:34+00:00</published>
    <title>[PHP] On DB Cache</title>
    <content type="html">&lt;p&gt;What do you think of my refactoring (and my caching comments)?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>DevP</name>
      <email>dev@forgreatjustice.net</email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/213-db-cache/refactors/1880" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor1849</id>
    <published>2008-01-28T20:50:22+00:00</published>
    <title>[PHP] On DB Cache</title>
    <content type="html">&lt;p&gt;This is my first time trying to refactor stuff!&lt;/p&gt;

&lt;p&gt;- I took for granted the overall approach, though I have my doubts (see below).
&lt;br /&gt;- I didn't test this, but I think I got the idea across.
&lt;br /&gt;- I'm assuming the Cache Table has a uniqueness constraint for &amp;quot;query&amp;quot;.
&lt;br /&gt;- I did collapse some of the error messages together when it seemed worth it for conciseness.
&lt;br /&gt;- I was in a &amp;quot;functions with long English names&amp;quot; mood, rather than an &amp;quot;object-oriented&amp;quot; mood. Not sure what's up with that.&lt;/p&gt;

&lt;p&gt;As I said above: I think the actual caching approach has some problems. The serializing/unserializing of a big array like that might hurt, and moreover putting a cache into the DB (when the DB is probably the bottleneck) doesn't seem right. So I'd suggest:&lt;/p&gt;

&lt;p&gt;- caching less info, in a less structured way (so: just a list of ids that you can do an easy query on to get the results)
&lt;br /&gt;- put these cached results elsewhere, like on the filesystem
&lt;br /&gt;- once we're talking about non-db caching: caching outputs makes more sense&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php

function get_query_results($sql)
{
  if (!($results = get_query_results_from_cache($sql)))
  {
    $results = get_query_results_from_db($sql);
    put_query_results_in_cache($sql, $results);
  }
  return $results;
}

function get_query_results_from_db($sql)
{
  $query = mysql_query($sql) or die('Query error');
  $arr = array();
  while($row = mysql_fetch_assoc($query))
  {
    $result = array();
    foreach($row as $key=&amp;gt;$val)
    {
      $results[$key] = $val;
    }
    $arr[] = $result;
  }
  return $arr;
}

function get_query_results_from_cache($sql)
{
  $update_time_sql = 'SELECT `updated` FROM `ftp_updates` ORDER BY `updated` DESC LIMIT 1';
  $cache_sql = 'SELECT * FROM `query_cache` WHERE `query` = \''.mysql_escape_string($sql).'\' AND cached&amp;gt;('.$update_time_sql.') LIMIT 1';
  $cache_qry = mysql_query($cache_sql) or die('Cache Error::: '.mysql_error());
  $row = mysql_fetch_row($qry);
  if (!empty($row) &amp;amp;&amp;amp; !empty($row['results']))
  {
    return unserialize($row['results']);
  }
  else
  {
    return false;
  }
}

function put_query_results_in_cache($sql, $results)
{
  //NOTE: this assumes that the query has field has a unique key constraint
  $cache_sql = 'INSERT INTO `query_cache` (`cached`, `query`, `results`) VALUES (now(), \''.mysql_escape_string($sql).'\', \''.mysql_escape_string(serialize($arr)).'\') ON DUPLICATE KEY UPDATE cached=now(), query=VALUES(query), results=VALUES(results)';
  mysql_query($cache_sql) or die('Cache Update Error'); // same message for any failed insert/update into the cache
}

?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>DevP</name>
      <email>dev@forgreatjustice.net</email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/213-db-cache/refactors/1849" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor1840</id>
    <published>2008-01-28T17:27:18+00:00</published>
    <title>[PHP] On DB Cache</title>
    <content type="html">&lt;p&gt;I'm curious: what sort of app is this for? I'm surprised at a caching DB results in the DB (though if we're talking about really rough queries anyway, I suppose it could make sense).&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>DevP</name>
      <email>dev@forgreatjustice.net</email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/213-db-cache/refactors/1840" rel="alternate"/>
  </entry>
</feed>
