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: 26: 27: 28: 29: 30:
31: class Horde_Cache_Storage_Sql extends Horde_Cache_Storage_Base
32: {
33: 34: 35: 36: 37:
38: protected $_db;
39:
40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51:
52: public function __construct($params = array())
53: {
54: if (!isset($params['db'])) {
55: throw new Horde_Cache_Exception('Missing db parameter.');
56: }
57: $this->_db = $params['db'];
58: unset($params['db']);
59:
60: $params = array_merge(array(
61: 'table' => 'horde_cache',
62: ), $params);
63:
64: parent::__construct($params);
65: }
66:
67: 68: 69:
70: public function __destruct()
71: {
72:
73: if (rand(0, 999) != 0) {
74: return;
75: }
76:
77: $query = 'DELETE FROM ' . $this->_params['table'] .
78: ' WHERE cache_expiration < ? AND cache_expiration <> 0';
79: $values = array(time());
80:
81: try {
82: $this->_db->delete($query, $values);
83: } catch (Horde_Db_Exception $e) {}
84: }
85:
86: 87:
88: public function get($key, $lifetime = 0)
89: {
90: $okey = $key;
91: $key = hash('md5', $key);
92:
93: $timestamp = time();
94: $maxage = $timestamp - $lifetime;
95:
96:
97: $query = 'SELECT cache_data FROM ' . $this->_params['table'] .
98: ' WHERE cache_id = ?';
99: $values = array($key);
100:
101:
102: if ($lifetime != 0) {
103: $query .= ' AND cache_timestamp >= ?';
104: $values[] = $maxage;
105: }
106:
107: try {
108: $result = $this->_db->selectValue($query, $values);
109: } catch (Horde_Db_Exception $e) {
110: return false;
111: }
112:
113: if (!$result) {
114:
115: if ($this->_logger) {
116: $this->_logger->log(sprintf('Cache miss: %s (Id %s newer than %d)', $okey, $key, $maxage), 'DEBUG');
117: }
118: return false;
119: }
120:
121: if ($this->_logger) {
122: $this->_logger->log(sprintf('Cache hit: %s (Id %s newer than %d)', $okey, $key, $maxage), 'DEBUG');
123: }
124:
125: return $result;
126: }
127:
128: 129:
130: public function set($key, $data, $lifetime = 0)
131: {
132: $okey = $key;
133: $key = hash('md5', $key);
134:
135: $timestamp = time();
136:
137:
138: $expiration = ($lifetime === 0)
139: ? 0
140: : ($lifetime + $timestamp);
141:
142: if ($this->_logger) {
143: $this->_logger->log(sprintf('Cache set: %s (Id %s set at %d expires at %d)', $okey, $key, $timestamp, $expiration), 'DEBUG');
144: }
145:
146:
147: $query = 'DELETE FROM ' . $this->_params['table'] . ' WHERE cache_id=?';
148: $values = array($key);
149: try {
150: $this->_db->delete($query, $values);
151: } catch (Horde_Db_Exception $e) {}
152:
153:
154: $query = 'INSERT INTO ' . $this->_params['table'] .
155: ' (cache_id, cache_timestamp, cache_expiration, cache_data)' .
156: ' VALUES (?, ?, ?, ?)';
157: $values = array($key, $timestamp, $expiration, $data);
158:
159: try {
160: $this->_db->insert($query, $values);
161: } catch (Horde_Db_Exception $e) {
162: throw new Horde_Cache_Exception($e);
163: }
164: }
165:
166: 167:
168: public function exists($key, $lifetime = 0)
169: {
170: $okey = $key;
171: $key = hash('md5', $key);
172:
173:
174: $query = 'SELECT 1 FROM ' . $this->_params['table'] .
175: ' WHERE cache_id = ?';
176: $values = array($key);
177:
178:
179: if ($lifetime != 0) {
180: $query .= ' AND cache_timestamp >= ?';
181: $values[] = time() - $lifetime;
182: }
183:
184: try {
185: $result = $this->_db->selectValue($query, $values);
186: } catch (Horde_Db_Exception $e) {
187: return false;
188: }
189:
190: $timestamp = time();
191: if (empty($result)) {
192: if ($this->_logger) {
193: $this->_logger->log(sprintf('Cache exists() miss: %s (Id %s newer than %d)', $okey, $key, $timestamp), 'DEBUG');
194: }
195: return false;
196: }
197:
198: if ($this->_logger) {
199: $this->_logger->log(sprintf('Cache exists() hit: %s (Id %s newer than %d)', $okey, $key, $timestamp), 'DEBUG');
200: }
201:
202: return true;
203: }
204:
205: 206:
207: public function expire($key)
208: {
209: $key = hash('md5', $key);
210:
211: $query = 'DELETE FROM ' . $this->_params['table'] .
212: ' WHERE cache_id = ?';
213: $values = array($key);
214:
215: try {
216: $this->_db->delete($query, $values);
217: } catch (Horde_Db_Exception $e) {
218: return false;
219: }
220:
221: return true;
222: }
223:
224: 225:
226: public function clear()
227: {
228: $query = 'DELETE FROM ' . $this->_params['table'];
229:
230: try {
231: $this->_db->delete($query);
232: } catch (Horde_Db_Exception $e) {
233: throw new Horde_Cache_Exception($e);
234: }
235: }
236:
237: }
238: