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 \PDOStatement Used to query for cached templates. */
28 protected $fetch_statement = NULL
;
30 /*! @var \PDOStatement Used to insert new templates. */
31 protected $insert_statement = NULL
;
33 /*! @var \PDOStatement Used to expire out-of-date templates. */
34 protected $delete_statement = NULL
;
37 Constructor that prepares the database statements.
39 @param \PDO The connected database object.
40 @param string Name of the database table to store templates in.
41 @param string Column in which the template name is stored.
42 @param string Column in which the template data is stored.
43 @param string Column in which the template last modified time is stored.
45 public function __construct(\PDO
$db,
47 $name_column_name = 'name',
48 $data_column_name = 'template',
49 $timestamp_column_name = 'timestamp')
51 $this->fetch_statement
= $db->Prepare("
52 SELECT `$data_column_name` AS template,
53 `$timestamp_column_name` AS timestamp
55 WHERE `$name_column_name` IN (:name)
58 $this->insert_statement
= $db->Prepare("
59 INSERT INTO `$table_name`
60 (`$name_column_name`, `$data_column_name`, `$timestamp_column_name`)
62 (:name, :template, :timestamp)
65 $this->delete_statement
= $db->Prepare("
66 DELETE FROM `$table_name`
67 WHERE `$name_column_name` = :name
71 public function GetTemplateDataForName($name, $modification_time)
73 if (!$this->fetch_statement
->Execute(array('name' => $name)))
76 $tpl = $this->fetch_statement
->FetchObject();
80 if ($tpl->timestamp
< $modification_time) {
81 $this->delete_statement
->Execute(array('name' => $name));
85 return $tpl->template
;
88 public function StoreCompiledTemplate($name, $modification_time, $data)
90 $this->insert_statement
->Execute(array(
92 'timestamp' => $modification_time,