| 1: | <?php
|
| 2: |
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: |
|
| 25: | class Smarty_CacheResource_Mysql extends Smarty_CacheResource_Custom
|
| 26: | {
|
| 27: | |
| 28: | |
| 29: |
|
| 30: | protected $db;
|
| 31: |
|
| 32: | |
| 33: | |
| 34: |
|
| 35: | protected $fetch;
|
| 36: |
|
| 37: | |
| 38: | |
| 39: |
|
| 40: | protected $fetchTimestamp;
|
| 41: |
|
| 42: | |
| 43: | |
| 44: |
|
| 45: | protected $save;
|
| 46: |
|
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: |
|
| 52: | public function __construct()
|
| 53: | {
|
| 54: | try {
|
| 55: | $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty");
|
| 56: | } catch (PDOException $e) {
|
| 57: | throw new SmartyException('Mysql Resource failed: ' . $e->getMessage());
|
| 58: | }
|
| 59: | $this->fetch = $this->db->prepare('SELECT modified, content FROM output_cache WHERE id = :id');
|
| 60: | $this->fetchTimestamp = $this->db->prepare('SELECT modified FROM output_cache WHERE id = :id');
|
| 61: | $this->save = $this->db->prepare(
|
| 62: | 'REPLACE INTO output_cache (id, name, cache_id, compile_id, content)
|
| 63: | VALUES (:id, :name, :cache_id, :compile_id, :content)'
|
| 64: | );
|
| 65: | }
|
| 66: |
|
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: |
|
| 79: | protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime)
|
| 80: | {
|
| 81: | $this->fetch->execute(array('id' => $id));
|
| 82: | $row = $this->fetch->fetch();
|
| 83: | $this->fetch->closeCursor();
|
| 84: | if ($row) {
|
| 85: | $content = $row[ 'content' ];
|
| 86: | $mtime = strtotime($row[ 'modified' ]);
|
| 87: | } else {
|
| 88: | $content = null;
|
| 89: | $mtime = null;
|
| 90: | }
|
| 91: | }
|
| 92: |
|
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: |
|
| 106: | protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
|
| 107: | {
|
| 108: | $this->fetchTimestamp->execute(array('id' => $id));
|
| 109: | $mtime = strtotime($this->fetchTimestamp->fetchColumn());
|
| 110: | $this->fetchTimestamp->closeCursor();
|
| 111: | return $mtime;
|
| 112: | }
|
| 113: |
|
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: |
|
| 126: | protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content)
|
| 127: | {
|
| 128: | $this->save->execute(
|
| 129: | array('id' => $id,
|
| 130: | 'name' => $name,
|
| 131: | 'cache_id' => $cache_id,
|
| 132: | 'compile_id' => $compile_id,
|
| 133: | 'content' => $content,)
|
| 134: | );
|
| 135: | return !!$this->save->rowCount();
|
| 136: | }
|
| 137: |
|
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: |
|
| 148: | protected function delete($name, $cache_id, $compile_id, $exp_time)
|
| 149: | {
|
| 150: |
|
| 151: | if ($name === null && $cache_id === null && $compile_id === null && $exp_time === null) {
|
| 152: |
|
| 153: | $query = $this->db->query('TRUNCATE TABLE output_cache');
|
| 154: | return -1;
|
| 155: | }
|
| 156: |
|
| 157: | $where = array();
|
| 158: |
|
| 159: | if ($name !== null) {
|
| 160: | $where[] = 'name = ' . $this->db->quote($name);
|
| 161: | }
|
| 162: |
|
| 163: | if ($compile_id !== null) {
|
| 164: | $where[] = 'compile_id = ' . $this->db->quote($compile_id);
|
| 165: | }
|
| 166: |
|
| 167: | if ($exp_time !== null) {
|
| 168: | $where[] = 'modified < DATE_SUB(NOW(), INTERVAL ' . intval($exp_time) . ' SECOND)';
|
| 169: | }
|
| 170: |
|
| 171: | if ($cache_id !== null) {
|
| 172: | $where[] =
|
| 173: | '(cache_id = ' .
|
| 174: | $this->db->quote($cache_id) .
|
| 175: | ' OR cache_id LIKE ' .
|
| 176: | $this->db->quote($cache_id . '|%') .
|
| 177: | ')';
|
| 178: | }
|
| 179: |
|
| 180: | $query = $this->db->query('DELETE FROM output_cache WHERE ' . join(' AND ', $where));
|
| 181: | return $query->rowCount();
|
| 182: | }
|
| 183: | }
|
| 184: | |