]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/demo/plugins/cacheresource.mysql.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / smarty / demo / plugins / cacheresource.mysql.php
1 <?php
2
3 /**
4  * MySQL CacheResource
5  * CacheResource Implementation based on the Custom API to use
6  * MySQL as the storage resource for Smarty's output caching.
7  * Table definition:
8  * <pre>CREATE TABLE IF NOT EXISTS `output_cache` (
9  *   `id` CHAR(40) NOT NULL COMMENT 'sha1 hash',
10  *   `name` VARCHAR(250) NOT NULL,
11  *   `cache_id` VARCHAR(250) NULL DEFAULT NULL,
12  *   `compile_id` VARCHAR(250) NULL DEFAULT NULL,
13  *   `modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
14  *   `content` LONGTEXT NOT NULL,
15  *   PRIMARY KEY (`id`),
16  *   INDEX(`name`),
17  *   INDEX(`cache_id`),
18  *   INDEX(`compile_id`),
19  *   INDEX(`modified`)
20  * ) ENGINE = InnoDB;</pre>
21  *
22  * @package CacheResource-examples
23  * @author  Rodney Rehm
24  */
25 class Smarty_CacheResource_Mysql extends Smarty_CacheResource_Custom
26 {
27     // PDO instance
28     protected $db;
29
30     protected $fetch;
31
32     protected $fetchTimestamp;
33
34     protected $save;
35
36     public function __construct()
37     {
38         try {
39             $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty");
40         }
41         catch (PDOException $e) {
42             throw new SmartyException('Mysql Resource failed: ' . $e->getMessage());
43         }
44         $this->fetch = $this->db->prepare('SELECT modified, content FROM output_cache WHERE id = :id');
45         $this->fetchTimestamp = $this->db->prepare('SELECT modified FROM output_cache WHERE id = :id');
46         $this->save = $this->db->prepare('REPLACE INTO output_cache (id, name, cache_id, compile_id, content)
47             VALUES  (:id, :name, :cache_id, :compile_id, :content)');
48     }
49
50     /**
51      * fetch cached content and its modification time from data source
52      *
53      * @param  string  $id         unique cache content identifier
54      * @param  string  $name       template name
55      * @param  string  $cache_id   cache id
56      * @param  string  $compile_id compile id
57      * @param  string  $content    cached content
58      * @param  integer $mtime      cache modification timestamp (epoch)
59      *
60      * @return void
61      */
62     protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime)
63     {
64         $this->fetch->execute(array('id' => $id));
65         $row = $this->fetch->fetch();
66         $this->fetch->closeCursor();
67         if ($row) {
68             $content = $row[ 'content' ];
69             $mtime = strtotime($row[ 'modified' ]);
70         } else {
71             $content = null;
72             $mtime = null;
73         }
74     }
75
76     /**
77      * Fetch cached content's modification timestamp from data source
78      *
79      * @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the complete cached content.
80      *
81      * @param  string $id         unique cache content identifier
82      * @param  string $name       template name
83      * @param  string $cache_id   cache id
84      * @param  string $compile_id compile id
85      *
86      * @return integer|boolean timestamp (epoch) the template was modified, or false if not found
87      */
88     protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
89     {
90         $this->fetchTimestamp->execute(array('id' => $id));
91         $mtime = strtotime($this->fetchTimestamp->fetchColumn());
92         $this->fetchTimestamp->closeCursor();
93
94         return $mtime;
95     }
96
97     /**
98      * Save content to cache
99      *
100      * @param  string       $id         unique cache content identifier
101      * @param  string       $name       template name
102      * @param  string       $cache_id   cache id
103      * @param  string       $compile_id compile id
104      * @param  integer|null $exp_time   seconds till expiration time in seconds or null
105      * @param  string       $content    content to cache
106      *
107      * @return boolean      success
108      */
109     protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content)
110     {
111         $this->save->execute(array('id' => $id, 'name' => $name, 'cache_id' => $cache_id, 'compile_id' => $compile_id,
112                                    'content' => $content,));
113
114         return !!$this->save->rowCount();
115     }
116
117     /**
118      * Delete content from cache
119      *
120      * @param  string       $name       template name
121      * @param  string       $cache_id   cache id
122      * @param  string       $compile_id compile id
123      * @param  integer|null $exp_time   seconds till expiration or null
124      *
125      * @return integer      number of deleted caches
126      */
127     protected function delete($name, $cache_id, $compile_id, $exp_time)
128     {
129         // delete the whole cache
130         if ($name === null && $cache_id === null && $compile_id === null && $exp_time === null) {
131             // returning the number of deleted caches would require a second query to count them
132             $query = $this->db->query('TRUNCATE TABLE output_cache');
133
134             return - 1;
135         }
136         // build the filter
137         $where = array();
138         // equal test name
139         if ($name !== null) {
140             $where[] = 'name = ' . $this->db->quote($name);
141         }
142         // equal test compile_id
143         if ($compile_id !== null) {
144             $where[] = 'compile_id = ' . $this->db->quote($compile_id);
145         }
146         // range test expiration time
147         if ($exp_time !== null) {
148             $where[] = 'modified < DATE_SUB(NOW(), INTERVAL ' . intval($exp_time) . ' SECOND)';
149         }
150         // equal test cache_id and match sub-groups
151         if ($cache_id !== null) {
152             $where[] = '(cache_id = ' . $this->db->quote($cache_id) . ' OR cache_id LIKE ' .
153                        $this->db->quote($cache_id . '|%') . ')';
154         }
155         // run delete query
156         $query = $this->db->query('DELETE FROM output_cache WHERE ' . join(' AND ', $where));
157
158         return $query->rowCount();
159     }
160 }