@param int The UNIX timestamp the uncompiled template was last modified.
*/
public function StoreCompiledTemplate($name, $modification_time, $data);
+
+ /*!
+ Gets a set of templates from the cache in bulk. If the backend doesn't
+ support bulk fetch operations, just loop over GetTemplateDataForName.
+
+ @param array Map of template names to modification times.
+
+ @return array Map of template names to data for templates that are present
+ and valid in the cache.
+ */
+ public function GetMultipleTemplates(Array $templates);
}
*/
class PDOCacheBackend implements CacheBackend
{
+ /*! @var \PDO The dat abase to use. */
+ protected $db = NULL;
+
/*! @var \PDOStatement Used to query for cached templates. */
protected $fetch_statement = NULL;
/*! @var \PDOStatement Used to expire out-of-date templates. */
protected $delete_statement = NULL;
+ /*! @var string Base query for a multi-fetch operation. */
+ protected $fetch_multi_query = "";
+
/*!
Constructor that prepares the database statements.
$data_column_name = 'template',
$timestamp_column_name = 'timestamp')
{
+ $this->db = $db;
+
$this->fetch_statement = $db->Prepare("
SELECT `$data_column_name` AS template,
`$timestamp_column_name` AS timestamp
DELETE FROM `$table_name`
WHERE `$name_column_name` = :name
");
+
+ $this->fetch_multi_query = "
+ SELECT `$name_column_name` AS name,
+ `$data_column_name` AS template,
+ `$timestamp_column_name` AS timestamp
+ FROM $table_name
+ WHERE `$name_column_name` IN ";
}
public function GetTemplateDataForName($name, $modification_time)
'template' => $data,
));
}
+
+ public function GetMultipleTemplates(Array $fetch_templates)
+ {
+ $placeholders = array_fill(0, count($fetch_templates), '?');
+ $stmt = $this->db->Prepare($this->fetch_multi_query . '(' . implode(',', $placeholders) . ')');
+ $stmt->Execute(array_keys($fetch_templates));
+
+ $templates = array();
+ while (($template = $stmt->FetchObject())) {
+ if ($template->timestamp >= $fetch_templates[$template->name])
+ $templates[$template->name] = $template->template;
+ }
+ return $templates;
+ }
}
return clone $template;
}
+ /*!
+ Warms up the template cache with a set of templates. If the cache backend
+ supports multiple simultaneous fetches, this can greatly improve performance.
+ */
+ public function PreCache(Array $templates)
+ {
+ if (!$this->cache_backend)
+ return;
+
+ $fetch_templates = array();
+ foreach ($templates AS $name) {
+ // Do not re-cache templates that have already been cached.
+ if (isset($this->cache[$name]))
+ continue;
+
+ $tpl_path = $this->_TemplatePath($name);
+ $fetch_templates[$name] = filemtime($tpl_path);
+ }
+
+ $profile = Profiling::IsProfilingEnabled();
+
+ $cached_templates = $this->cache_backend->GetMultipleTemplates($fetch_templates);
+ foreach ($cached_templates AS $name => $data) {
+ $this->cache[$name] = Template::NewWithCompiledData($name, $data);
+ if ($profile)
+ $this->usage[$name] = 0;
+ }
+ }
+
/*! Convenience function for loading templates. */
static public function Fetch($name)
{