| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * MySQL CacheResource | 
					
						
							|  |  |  |  * CacheResource Implementation based on the Custom API to use | 
					
						
							|  |  |  |  * MySQL as the storage resource for Smarty's output caching. | 
					
						
							|  |  |  |  * Table definition: | 
					
						
							|  |  |  |  * <pre>CREATE TABLE IF NOT EXISTS `output_cache` ( | 
					
						
							|  |  |  |  *   `id` CHAR(40) NOT NULL COMMENT 'sha1 hash', | 
					
						
							|  |  |  |  *   `name` VARCHAR(250) NOT NULL, | 
					
						
							|  |  |  |  *   `cache_id` VARCHAR(250) NULL DEFAULT NULL, | 
					
						
							|  |  |  |  *   `compile_id` VARCHAR(250) NULL DEFAULT NULL, | 
					
						
							|  |  |  |  *   `modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | 
					
						
							|  |  |  |  *   `content` LONGTEXT NOT NULL, | 
					
						
							|  |  |  |  *   PRIMARY KEY (`id`), | 
					
						
							|  |  |  |  *   INDEX(`name`), | 
					
						
							|  |  |  |  *   INDEX(`cache_id`), | 
					
						
							|  |  |  |  *   INDEX(`compile_id`), | 
					
						
							|  |  |  |  *   INDEX(`modified`) | 
					
						
							|  |  |  |  * ) ENGINE = InnoDB;</pre> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package CacheResource-examples | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  * @author  Rodney Rehm | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | class Smarty_CacheResource_Mysql extends Smarty_CacheResource_Custom | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     // PDO instance
 | 
					
						
							|  |  |  |     protected $db; | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     protected $fetch; | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     protected $fetchTimestamp; | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     protected $save; | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2013-07-15 18:18:28 +00:00
										 |  |  |             $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty"); | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         catch (PDOException $e) { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             throw new SmartyException('Mysql Resource failed: ' . $e->getMessage()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $this->fetch = $this->db->prepare('SELECT modified, content FROM output_cache WHERE id = :id'); | 
					
						
							|  |  |  |         $this->fetchTimestamp = $this->db->prepare('SELECT modified FROM output_cache WHERE id = :id'); | 
					
						
							|  |  |  |         $this->save = $this->db->prepare('REPLACE INTO output_cache (id, name, cache_id, compile_id, content) | 
					
						
							|  |  |  |             VALUES  (:id, :name, :cache_id, :compile_id, :content)'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * fetch cached content and its modification time from data source | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @param  string  $id         unique cache content identifier | 
					
						
							|  |  |  |      * @param  string  $name       template name | 
					
						
							|  |  |  |      * @param  string  $cache_id   cache id | 
					
						
							|  |  |  |      * @param  string  $compile_id compile id | 
					
						
							|  |  |  |      * @param  string  $content    cached content | 
					
						
							|  |  |  |      * @param  integer $mtime      cache modification timestamp (epoch) | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->fetch->execute(array('id' => $id)); | 
					
						
							|  |  |  |         $row = $this->fetch->fetch(); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |         $this->fetch->closeCursor(); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         if ($row) { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             $content = $row[ 'content' ]; | 
					
						
							|  |  |  |             $mtime = strtotime($row[ 'modified' ]); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             $content = null; | 
					
						
							|  |  |  |             $mtime = null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Fetch cached content's modification timestamp from data source | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the complete cached content. | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param  string $id         unique cache content identifier | 
					
						
							|  |  |  |      * @param  string $name       template name | 
					
						
							|  |  |  |      * @param  string $cache_id   cache id | 
					
						
							|  |  |  |      * @param  string $compile_id compile id | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * @return integer|boolean timestamp (epoch) the template was modified, or false if not found | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function fetchTimestamp($id, $name, $cache_id, $compile_id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->fetchTimestamp->execute(array('id' => $id)); | 
					
						
							| 
									
										
										
										
											2011-10-10 19:31:34 +00:00
										 |  |  |         $mtime = strtotime($this->fetchTimestamp->fetchColumn()); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         $this->fetchTimestamp->closeCursor(); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         return $mtime; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Save content to cache | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @param  string       $id         unique cache content identifier | 
					
						
							|  |  |  |      * @param  string       $name       template name | 
					
						
							|  |  |  |      * @param  string       $cache_id   cache id | 
					
						
							|  |  |  |      * @param  string       $compile_id compile id | 
					
						
							|  |  |  |      * @param  integer|null $exp_time   seconds till expiration time in seconds or null | 
					
						
							|  |  |  |      * @param  string       $content    content to cache | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @return boolean      success | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |         $this->save->execute(array('id' => $id, 'name' => $name, 'cache_id' => $cache_id, 'compile_id' => $compile_id, | 
					
						
							|  |  |  |                                    'content' => $content,)); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         return !!$this->save->rowCount(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Delete content from cache | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @param  string       $name       template name | 
					
						
							|  |  |  |      * @param  string       $cache_id   cache id | 
					
						
							|  |  |  |      * @param  string       $compile_id compile id | 
					
						
							|  |  |  |      * @param  integer|null $exp_time   seconds till expiration or null | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @return integer      number of deleted caches | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function delete($name, $cache_id, $compile_id, $exp_time) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // delete the whole cache
 | 
					
						
							|  |  |  |         if ($name === null && $cache_id === null && $compile_id === null && $exp_time === null) { | 
					
						
							|  |  |  |             // returning the number of deleted caches would require a second query to count them
 | 
					
						
							|  |  |  |             $query = $this->db->query('TRUNCATE TABLE output_cache'); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |             return - 1; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         // build the filter
 | 
					
						
							|  |  |  |         $where = array(); | 
					
						
							|  |  |  |         // equal test name
 | 
					
						
							|  |  |  |         if ($name !== null) { | 
					
						
							|  |  |  |             $where[] = 'name = ' . $this->db->quote($name); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // equal test compile_id
 | 
					
						
							|  |  |  |         if ($compile_id !== null) { | 
					
						
							|  |  |  |             $where[] = 'compile_id = ' . $this->db->quote($compile_id); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // range test expiration time
 | 
					
						
							|  |  |  |         if ($exp_time !== null) { | 
					
						
							|  |  |  |             $where[] = 'modified < DATE_SUB(NOW(), INTERVAL ' . intval($exp_time) . ' SECOND)'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // equal test cache_id and match sub-groups
 | 
					
						
							|  |  |  |         if ($cache_id !== null) { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             $where[] = '(cache_id = ' . $this->db->quote($cache_id) . ' OR cache_id LIKE ' . | 
					
						
							|  |  |  |                        $this->db->quote($cache_id . '|%') . ')'; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         // run delete query
 | 
					
						
							|  |  |  |         $query = $this->db->query('DELETE FROM output_cache WHERE ' . join(' AND ', $where)); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         return $query->rowCount(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |