PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/include/Cache/Container/dbx.php

https://github.com/usagi-project/mynets1
PHP | 302 lines | 149 code | 47 blank | 106 comment | 19 complexity | c19f90049f40fc5f480022072ce612a2 MD5 | raw file
  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.6 2004/12/15 09:09:29 dufuz 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.6 2004/12/15 09:09:29 dufuz Exp $
  51. * @package Cache
  52. */
  53. class Cache_Container_dbx extends Cache_Container
  54. {
  55. /**
  56. * Name of the DB table to store caching data
  57. *
  58. * @see Cache_Container_file::$filename_prefix
  59. */
  60. var $cache_table = '';
  61. /**
  62. * DBx module to use
  63. *
  64. * at the moment only mysql or odbc
  65. *
  66. * @var string
  67. */
  68. var $module = '';
  69. /**
  70. * DB host to use
  71. *
  72. * @var string
  73. */
  74. var $host = '';
  75. /**
  76. * DB database to use
  77. *
  78. * @var string
  79. */
  80. var $db = '';
  81. /**
  82. * DB username to use
  83. *
  84. * @var string
  85. */
  86. var $username = '';
  87. /**
  88. * DB password to use
  89. *
  90. * @var string
  91. */
  92. var $password = '';
  93. /**
  94. * DBx handle object
  95. *
  96. * @var object DBx handle
  97. */
  98. var $db;
  99. /**
  100. * Establish a persistent connection?
  101. *
  102. * @var boolean
  103. */
  104. var $persistent = true;
  105. function Cache_Container_dbx($options)
  106. {
  107. if (!is_array($options) ) {
  108. return new Cache_Error('No options specified!', __FILE__, __LINE__);
  109. }
  110. $this->setOptions($options, array_merge($this->allowed_options, array('module','host','db','username','password', 'cache_table', 'persistent')));
  111. if (!$this->module)
  112. return new Cache_Error('No module specified!', __FILE__, __LINE__);
  113. $this->db = dbx_connect($this->module, $this->host, $this->db, $this->username, $this->password, $this->persistent);
  114. if (dbx_error($this->db)) {
  115. return new Cache_Error('DBx connect failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  116. } else {
  117. //not implemented yet in dbx
  118. //$this->db->setFetchMode(DB_FETCHMODE_ASSOC);
  119. }
  120. }
  121. function fetch($id, $group)
  122. {
  123. $query = sprintf("SELECT cachedata, userdata, expires FROM %s WHERE id = '%s' AND cachegroup = '%s'",
  124. $this->cache_table,
  125. addslashes($id),
  126. addslashes($group)
  127. );
  128. $res = dbx_query($this->db, $query);
  129. if (dbx_error($this->db)) {
  130. return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  131. }
  132. $row = $res->data[0];
  133. if (is_array($row)) {
  134. $data = array($row['expires'], $this->decode($row['cachedata']), $row['userdata']);
  135. } else {
  136. $data = array(null, null, null);
  137. }
  138. // last used required by the garbage collection
  139. // WARNING: might be MySQL specific
  140. $query = sprintf("UPDATE %s SET changed = (NOW() + 0) WHERE id = '%s' AND cachegroup = '%s'",
  141. $this->cache_table,
  142. addslashes($id),
  143. addslashes($group)
  144. );
  145. $res = dbx_query($this->db, $query);
  146. if (dbx_error($this->db)) {
  147. return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  148. }
  149. return $data;
  150. }
  151. /**
  152. * Stores a dataset.
  153. *
  154. * WARNING: we use the SQL command REPLACE INTO this might be
  155. * MySQL specific. As MySQL is very popular the method should
  156. * work fine for 95% of you.
  157. */
  158. function save($id, $data, $expires, $group, $userdata)
  159. {
  160. $this->flushPreload($id, $group);
  161. $query = sprintf("REPLACE INTO %s (userdata, cachedata, expires, id, cachegroup) VALUES ('%s', '%s', %d, '%s', '%s')",
  162. $this->cache_table,
  163. addslashes($userdata),
  164. addslashes($this->encode($data)),
  165. $this->getExpiresAbsolute($expires) ,
  166. addslashes($id),
  167. addslashes($group)
  168. );
  169. $res = dbx_query($this->db, $query);
  170. if (dbx_error($this->db)) {
  171. return new Cache_Error('DBx query failed: ' . dbx_error($this->db) , __FILE__, __LINE__);
  172. }
  173. }
  174. function remove($id, $group)
  175. {
  176. $this->flushPreload($id, $group);
  177. $query = sprintf("DELETE FROM %s WHERE id = '%s' and cachegroup = '%s'",
  178. $this->cache_table,
  179. addslashes($id),
  180. addslashes($group)
  181. );
  182. $res = dbx_query($this->db, $query);
  183. if (dbx_error($this->db)) {
  184. return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  185. }
  186. }
  187. function flush($group = '')
  188. {
  189. $this->flushPreload();
  190. if ($group) {
  191. $query = sprintf("DELETE FROM %s WHERE cachegroup = '%s'", $this->cache_table, addslashes($group));
  192. } else {
  193. $query = sprintf("DELETE FROM %s", $this->cache_table);
  194. }
  195. $res = dbx_query($this->db,$query);
  196. if (dbx_error($this->db)) {
  197. return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  198. }
  199. }
  200. function idExists($id, $group)
  201. {
  202. $query = sprintf("SELECT id FROM %s WHERE ID = '%s' AND cachegroup = '%s'",
  203. $this->cache_table,
  204. addslashes($id),
  205. addslashes($group)
  206. );
  207. $res = dbx_query($this->db, $query);
  208. if (dbx_error($this->db)) {
  209. return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  210. }
  211. $row = $res[0];
  212. if (is_array($row)) {
  213. return true;
  214. }
  215. return false;
  216. }
  217. function garbageCollection($maxlifetime)
  218. {
  219. $this->flushPreload();
  220. $query = sprintf('DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)',
  221. $this->cache_table,
  222. time(),
  223. $maxlifetime
  224. );
  225. $res = dbx_query($this->db, $query);
  226. if (dbx_error($this->db)) {
  227. return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__);
  228. }
  229. $query = sprintf('select sum(length(cachedata)) as CacheSize from %s',
  230. $this->cache_table
  231. );
  232. $res = dbx_query($this->db, $query);
  233. //if cache is to big.
  234. if ($res->data[0][CacheSize] > $this->highwater) {
  235. //find the lowwater mark.
  236. $query = sprintf('select length(cachedata) as size, changed from %s order by changed DESC',
  237. $this->cache_table
  238. );
  239. $res = dbx_query($this->db, $query);
  240. $keep_size = 0;
  241. $i = 0;
  242. while ($keep_size < $this->lowwater && $i < $res->rows ) {
  243. $keep_size += $res->data[$i][size];
  244. $i++;
  245. }
  246. //delete all entries, which were changed before the "lowwwater mark"
  247. $query = sprintf('delete from %s where changed <= %s',
  248. $this->cache_table,
  249. $res->data[$i][changed]
  250. );
  251. $res = dbx_query($this->db, $query);
  252. }
  253. }
  254. }
  255. ?>