At the moment I'm using php and apc in my mvc based site, it's a custom built simple mvc so I'm trying to build it to fit my needs.
However I'm unsure where is the preferred location in which to handle caching?
I have two choices (I think) .. either do all caching in the various controllers which would mean objects were stored in the cache, or store returned data from queries in the cache inside the method:
Controller Example:
function showPage() {
$pageOb = new Page();
$key = md5('pageOb->getQuery()');
if(!$data = apc_fetch($key)){
$data = $pageOb->getQuery();
apc_add($key, $data, 600);
}
require 'template.php';
}
Method Example:
function getQuery(){
$sql = "SELECT * FROM table";
$key = md5('query'.$sql);
if(!$data = apc_fetch($key)){
$core = Connect::getInstance();
$stmt = $core->dbh->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll();
apc_add($key, $data, 600);
}
return $data;
}