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