dbx.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license, |
  8. // | that is bundled with this package in the file LICENSE, and is |
  9. // | available at through the world-wide-web at |
  10. // | http://www.php.net/license/2_02.txt. |
  11. // | If you did not receive a copy of the PHP license and are unable to |
  12. // | obtain it through the world-wide-web, please send a note to |
  13. // | license@php.net so we can mail you a copy immediately. |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Christian Stocker <chregu@phant.ch> |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: dbx.php,v 1.4 2003/01/04 11:54:46 mj Exp $
  19. require_once 'Cache/Container.php';
  20. /**
  21. * ext/dbx Cache Container.
  22. *
  23. * WARNING: Other systems might or might not support certain datatypes of
  24. * the tables shown. As far as I know there's no large binary
  25. * type in SQL-92 or SQL-99. Postgres seems to lack any
  26. * BLOB or TEXT type, for MS-SQL you could use IMAGE, don't know
  27. * about other databases. Please add sugestions for other databases to
  28. * the inline docs.
  29. *
  30. * The field 'changed' has no meaning for the Cache itself. It's just there
  31. * because it's a good idea to have an automatically updated timestamp
  32. * field for debugging in all of your tables.
  33. *
  34. * For _MySQL_ you need this DB table:
  35. *
  36. * CREATE TABLE cache (
  37. * id CHAR(32) NOT NULL DEFAULT '',
  38. * cachegroup VARCHAR(127) NOT NULL DEFAULT '',
  39. * cachedata BLOB NOT NULL DEFAULT '',
  40. * userdata VARCHAR(255) NOT NULL DEFAULT '',
  41. * expires INT(9) NOT NULL DEFAULT 0,
  42. *
  43. * changed TIMESTAMP(14) NOT NULL,
  44. *
  45. * INDEX (expires),
  46. * PRIMARY KEY (id, cachegroup)
  47. * )
  48. *
  49. * @author Christian Stocker <chregu@phant.ch>
  50. * @version $Id: dbx.php,v 1.4 2003/01/04 11:54:46 mj Exp $
  51. * @package Cache
  52. */
  53. class Cache_Container_dbx extends Cache_Container {
  54. /**
  55. * Name of the DB table to store caching data
  56. *
  57. * @see Cache_Container_file::$filename_prefix
  58. */
  59. var $cache_table = '';
  60. /**
  61. * DBx module to use
  62. *
  63. * at the moment only mysql or odbc
  64. *
  65. * @var string
  66. */
  67. var $module = '';
  68. /**
  69. * DB host to use
  70. *
  71. * @var string
  72. */
  73. var $host = '';
  74. /**
  75. * DB database to use
  76. *
  77. * @var string
  78. */
  79. var $db = '';
  80. /**
  81. * DB username to use
  82. *
  83. * @var string
  84. */
  85. var $username = '';
  86. /**
  87. * DB password to use
  88. *
  89. * @var string
  90. */
  91. var $password = '';
  92. /**
  93. * DBx handle object
  94. *
  95. * @var object DBx handle
  96. */
  97. var $db;
  98. /**
  99. * Establish a persistent connection?
  100. *
  101. * @var boolean
  102. */
  103. var $persistent = true;
  104. function Cache_Container_dbx($options)
  105. {
  106. if (!is_array($options) ) {
  107. return new Cache_Error('No options specified!', __FILE__, __LINE__);
  108. }
  109. $this->setOptions($options, array_merge($this->allowed_options, array('module','host','db','username','password', 'cache_table', 'persistent')));
  110. if (!$this->module)
  111. return new Cache_Error('No module specified!', __FILE__, __LINE__);
  112. $this->db = dbx_connect($this->module, $this->host, $this->db, $this->username, $this->password, $this->persistent);
  113. if (dbx_error($this->db)) {
  114. return new Cache_Error('DBx connect failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  115. } else {
  116. //not implemented yet in dbx
  117. //$this->db->setFetchMode(DB_FETCHMODE_ASSOC);
  118. }
  119. }
  120. function fetch($id, $group)
  121. {
  122. $query = sprintf("SELECT cachedata, userdata, expires FROM %s WHERE id = '%s' AND cachegroup = '%s'",
  123. $this->cache_table,
  124. addslashes($id),
  125. addslashes($group)
  126. );
  127. $res = dbx_query($this->db, $query);
  128. if (dbx_error($this->db))
  129. return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  130. $row = $res->data[0];
  131. if (is_array($row))
  132. $data = array($row['expires'], $this->decode($row['cachedata']), $row['userdata']);
  133. else
  134. $data = array(NULL, NULL, NULL);
  135. // last used required by the garbage collection
  136. // WARNING: might be MySQL specific
  137. $query = sprintf("UPDATE %s SET changed = (NOW() + 0) WHERE id = '%s' AND cachegroup = '%s'",
  138. $this->cache_table,
  139. addslashes($id),
  140. addslashes($group)
  141. );
  142. $res = dbx_query($this->db, $query);
  143. if (dbx_error($this->db))
  144. return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  145. return $data;
  146. }
  147. /**
  148. * Stores a dataset.
  149. *
  150. * WARNING: we use the SQL command REPLACE INTO this might be
  151. * MySQL specific. As MySQL is very popular the method should
  152. * work fine for 95% of you.
  153. */
  154. function save($id, $data, $expires, $group, $userdata)
  155. {
  156. $this->flushPreload($id, $group);
  157. $query = sprintf("REPLACE INTO %s (userdata, cachedata, expires, id, cachegroup) VALUES ('%s', '%s', %d, '%s', '%s')",
  158. $this->cache_table,
  159. addslashes($userdata),
  160. addslashes($this->encode($data)),
  161. $this->getExpiresAbsolute($expires) ,
  162. addslashes($id),
  163. addslashes($group)
  164. );
  165. $res = dbx_query($this->db, $query);
  166. if (dbx_error($this->db)) {
  167. return new Cache_Error('DBx query failed: ' . dbx_error($this->db) , __FILE__, __LINE__);
  168. }
  169. }
  170. function remove($id, $group)
  171. {
  172. $this->flushPreload($id, $group);
  173. $query = sprintf("DELETE FROM %s WHERE id = '%s' and cachegroup = '%s'",
  174. $this->cache_table,
  175. addslashes($id),
  176. addslashes($group)
  177. );
  178. $res = dbx_query($this->db, $query);
  179. if (dbx_error($this->db))
  180. return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  181. }
  182. function flush($group = '')
  183. {
  184. $this->flushPreload();
  185. if ($group) {
  186. $query = sprintf("DELETE FROM %s WHERE cachegroup = '%s'", $this->cache_table, addslashes($group));
  187. } else {
  188. $query = sprintf("DELETE FROM %s", $this->cache_table);
  189. }
  190. $res = dbx_query($this->db,$query);
  191. if (dbx_error($this->db))
  192. return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  193. }
  194. function idExists($id, $group)
  195. {
  196. $query = sprintf("SELECT id FROM %s WHERE ID = '%s' AND cachegroup = '%s'",
  197. $this->cache_table,
  198. addslashes($id),
  199. addslashes($group)
  200. );
  201. $res = dbx_query($this->db, $query);
  202. if (dbx_error($this->db))
  203. return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  204. $row = $res[0];
  205. if (is_array($row)) {
  206. return true;
  207. } else {
  208. return false;
  209. }
  210. }
  211. function garbageCollection($maxlifetime)
  212. {
  213. $this->flushPreload();
  214. $query = sprintf('DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)',
  215. $this->cache_table,
  216. time(),
  217. $maxlifetime
  218. );
  219. $res = dbx_query($this->db, $query);
  220. if (dbx_error($this->db))
  221. return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  222. $query = sprintf('select sum(length(cachedata)) as CacheSize from %s',
  223. $this->cache_table
  224. );
  225. $res = dbx_query($this->db, $query);
  226. //if cache is to big.
  227. if ($res->data[0][CacheSize] > $this->highwater)
  228. {
  229. //find the lowwater mark.
  230. $query = sprintf('select length(cachedata) as size, changed from %s order by changed DESC',
  231. $this->cache_table
  232. );
  233. $res = dbx_query($this->db, $query);
  234. $keep_size=0;
  235. $i=0;
  236. while ($keep_size < $this->lowwater && $i < $res->rows )
  237. {
  238. $keep_size += $res->data[$i][size];
  239. $i++;
  240. }
  241. //delete all entries, which were changed before the "lowwwater mark"
  242. $query = sprintf('delete from %s where changed <= %s',
  243. $this->cache_table,
  244. $res->data[$i][changed]
  245. );
  246. $res = dbx_query($this->db, $query);
  247. }
  248. }
  249. }
  250. ?>