Write a CacheBackend implementation for PDO.
[hoplite.git] / views / pdo_cache_backend.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2013 Blue Static
4 //
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.
8 //
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
12 // more details.
13 //
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/>.
16
17 namespace hoplite\views;
18
19 require_once HOPLITE_ROOT . '/views/cache_backend.php';
20
21 /*!
22 An instance of CacheBackend that stores compiled templates in a database
23 table accessed through a PDO object.
24 */
25 class PDOCacheBackend implements CacheBackend
26 {
27 /*! @var \PDOStatement Used to query for cached templates. */
28 protected $fetch_statement = NULL;
29
30 /*! @var \PDOStatement Used to insert new templates. */
31 protected $insert_statement = NULL;
32
33 /*! @var \PDOStatement Used to expire out-of-date templates. */
34 protected $delete_statement = NULL;
35
36 /*!
37 Constructor that prepares the database statements.
38
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.
44 */
45 public function __construct(\PDO $db,
46 $table_name,
47 $name_column_name = 'name',
48 $data_column_name = 'template',
49 $timestamp_column_name = 'timestamp')
50 {
51 $this->fetch_statement = $db->Prepare("
52 SELECT `$data_column_name` AS template,
53 `$timestamp_column_name` AS timestamp
54 FROM $table_name
55 WHERE `$name_column_name` IN (:name)
56 ");
57
58 $this->insert_statement = $db->Prepare("
59 INSERT INTO `$table_name`
60 (`$name_column_name`, `$data_column_name`, `$timestamp_column_name`)
61 VALUES
62 (:name, :template, :timestamp)
63 ");
64
65 $this->delete_statement = $db->Prepare("
66 DELETE FROM `$table_name`
67 WHERE `$name_column_name` = :name
68 ");
69 }
70
71 public function GetTemplateDataForName($name, $modification_time)
72 {
73 if (!$this->fetch_statement->Execute(array('name' => $name)))
74 return NULL;
75
76 $tpl = $this->fetch_statement->FetchObject();
77 if (!$tpl)
78 return NULL;
79
80 if ($tpl->timestamp < $modification_time) {
81 $this->delete_statement->Execute(array('name' => $name));
82 return NULL;
83 }
84
85 return $tpl->template;
86 }
87
88 public function StoreCompiledTemplate($name, $modification_time, $data)
89 {
90 $this->insert_statement->Execute(array(
91 'name' => $name,
92 'timestamp' => $modification_time,
93 'template' => $data,
94 ));
95 }
96 }