cacheresource.mysql.php 5.5 KB

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