3 // Copyright (c) 2013 Blue Static
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
17 namespace hoplite
\views
;
19 require_once HOPLITE_ROOT
. '/views/cache_backend.php';
22 An instance of CacheBackend that stores compiled templates in a database
23 table accessed through a PDO object.
25 class PDOCacheBackend
implements CacheBackend
27 /*! @var \PDO The dat abase to use. */
30 /*! @var \PDOStatement Used to query for cached templates. */
31 protected $fetch_statement = NULL
;
33 /*! @var \PDOStatement Used to insert new templates. */
34 protected $insert_statement = NULL
;
36 /*! @var \PDOStatement Used to expire out-of-date templates. */
37 protected $delete_statement = NULL
;
39 /*! @var string Base query for a multi-fetch operation. */
40 protected $fetch_multi_query = "";
43 Constructor that prepares the database statements.
45 @param \PDO The connected database object.
46 @param string Name of the database table to store templates in.
47 @param string Column in which the template name is stored.
48 @param string Column in which the template data is stored.
49 @param string Column in which the template last modified time is stored.
51 public function __construct(\PDO
$db,
53 $name_column_name = 'name',
54 $data_column_name = 'template',
55 $timestamp_column_name = 'timestamp')
59 $this->fetch_statement
= $db->Prepare("
60 SELECT `$data_column_name` AS template,
61 `$timestamp_column_name` AS timestamp
63 WHERE `$name_column_name` IN (:name)
66 $this->insert_statement
= $db->Prepare("
67 INSERT INTO `$table_name`
68 (`$name_column_name`, `$data_column_name`, `$timestamp_column_name`)
70 (:name, :template, :timestamp)
73 $this->delete_statement
= $db->Prepare("
74 DELETE FROM `$table_name`
75 WHERE `$name_column_name` = :name
78 $this->fetch_multi_query
= "
79 SELECT `$name_column_name` AS name,
80 `$data_column_name` AS template,
81 `$timestamp_column_name` AS timestamp
83 WHERE `$name_column_name` IN ";
86 public function GetTemplateDataForName($name, $modification_time)
88 $this->fetch_statement
->Execute(array('name' => $name));
89 $tpl = $this->fetch_statement
->FetchObject();
93 if ($tpl->timestamp
< $modification_time) {
94 $this->delete_statement
->Execute(array('name' => $name));
98 return $tpl->template
;
101 public function StoreCompiledTemplate($name, $modification_time, $data)
103 $this->insert_statement
->Execute(array(
105 'timestamp' => $modification_time,
110 public function GetMultipleTemplates(Array $fetch_templates)
112 $placeholders = array_fill(0, count($fetch_templates), '?');
113 $stmt = $this->db
->Prepare($this->fetch_multi_query
. '(' . implode(',', $placeholders) . ')');
114 $stmt->Execute(array_keys($fetch_templates));
116 $templates = array();
117 while (($template = $stmt->FetchObject())) {
118 if ($template->timestamp
>= $fetch_templates[$template->name
])
119 $templates[$template->name
] = $template->template
;