</> Developers Guide to Funraisin

Caching

Technology

Every Funraisin website supports 2 types of caching, Disk Caching and Memcached via AWS elasticache.

One thing to keep in mind is that Memcached is shared across all sites within the same region, so the cache names must be unique otherwise they will access caches from other sites potentially.

When to Use Caching

If in doubt then you should always use caching when querying a database, however sometimes caching can be not as efficient as the database server itself since file based caching can end up using a lot of CPU when a site is under load.

Using SUM() with GROUP BY

Any database query that uses a SUM or a GROUP BY should be cached.

Any Leaderboard

Leaderboards will pretty much always be database intensive due to the nature of what they have to do in order to return results. Therefore all leaderboards need to be cached, regardless.



When not to use Caching

Any query where you are literally just pulling a single record using an ID that is already indexed does not need caching since the database server is much more efficient that pulling a single record than it is to pull it, save it to disk, etc. So a SELECT * FROM members WHERE member_id=123 does not need caching

Example Code

Below is an example of how to use disk caching with a database query that returns multiple records for use with things like leaderboards.

$ci =& get_instance();
$ci->load->driver('cache', array('adapter' => 'file'));
if ( ! $leaderboarddata = $this->cache->get('unique_cache_name')){
   $query=$ci->db->query("SELECT * FROM table as a LEFT JOIN table2 as b on a.field=b.field WHERE etc");
   if($query->num_rows()>0) {
      $x=0
      foreach($query->result() as $row) {
      $cachdata[$x]["name"]=addslashes($row->d_fname.' '.$row->d_lname);
      $cachdata[$x]["amount"]=$row->d_amount;
      $x++;
      }
      $ci->cache->save('unique_cache_name',$cachedata,3600);// caches for 1 hour
      $leaderboarddata = $cachedata;
   }
}

// now you can access cached data
foreach($leaderboarddata as $leaderboard) {

}

Helpers

For database caching pulling single records there are some helpers that can make it easier to query and cache all in one go instead of coding up a full query.

View Database Helpers