phplib.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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: Ulf Wendel <ulf.wendel@phpdoc.de> |
  16. // | Sebastian Bergmann <sb@sebastian-bergmann.de> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: phplib.php,v 1.3 2003/01/04 11:54:46 mj Exp $
  20. require_once 'Cache/Container.php';
  21. /**
  22. * Stores cache data into a database table using PHPLibs DB abstraction.
  23. *
  24. * WARNING: Other systems might or might not support certain datatypes of
  25. * the tables shown. As far as I know there's no large binary
  26. * type in SQL-92 or SQL-99. Postgres seems to lack any
  27. * BLOB or TEXT type, for MS-SQL you could use IMAGE, don't know
  28. * about other databases. Please add sugestions for other databases to
  29. * the inline docs.
  30. *
  31. * The field 'changed' is used by the garbage collection. Depending on
  32. * your databasesystem you might have to subclass fetch() and garbageCollection().
  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 DEFAUL '',
  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. *
  50. * @author Ulf Wendel <ulf.wendel@phpdoc.de>, Sebastian Bergmann <sb@sebastian-bergmann.de>
  51. * @version $Id: phplib.php,v 1.3 2003/01/04 11:54:46 mj Exp $
  52. * @package Cache
  53. * @see save()
  54. */
  55. class Cache_Container_phplib extends Cache_Container {
  56. /**
  57. * Name of the DB table to store caching data
  58. *
  59. * @see Cache_Container_file::$filename_prefix
  60. */
  61. var $cache_table = 'cache';
  62. /**
  63. * PHPLib object
  64. *
  65. * @var object PEAR_DB
  66. */
  67. var $db;
  68. /**
  69. * Name of the PHPLib DB class to use
  70. *
  71. * @var string
  72. * @see $db_path, $local_path
  73. */
  74. var $db_class = '';
  75. /**
  76. * Filename of your local.inc
  77. *
  78. * If empty, 'local.inc' is assumed.
  79. *
  80. * @var string
  81. */
  82. var $local_file = '';
  83. /**
  84. * Include path for you local.inc
  85. *
  86. * HINT: If your're not using PHPLib's prepend.php you must
  87. * take care that all classes (files) references by you
  88. * local.inc are included automatically. So you might
  89. * want to write a new local2.inc that only referrs to
  90. * the database class (file) you're using and includes all required files.
  91. *
  92. * @var string path to your local.inc - make sure to add a trailing slash
  93. * @see $local_file
  94. */
  95. var $local_path = '';
  96. /**
  97. * Creates an instance of a phplib db class to use it for storage.
  98. *
  99. * @param mixed If empty the object tries to used the
  100. * preconfigured class variables. If given it
  101. * must be an array with:
  102. * db_class => name of the DB class to use
  103. * optional:
  104. * db_file => filename of the DB class
  105. * db_path => path to the DB class
  106. * local_file => kind of local.inc
  107. * local_patk => path to the local.inc
  108. * see $local_path for some hints.s
  109. * @see $local_path
  110. */
  111. function Cache_Container_phplib($options = '') {
  112. if (is_array($options))
  113. $this->setOptions($options, array_merge($this->allowed_options, array('db_class', 'db_file', 'db_path', 'local_file', 'local_path')));
  114. if (!$this->db_class)
  115. return new Cache_Error('No database class specified.', __FILE__, __LINE__);
  116. // include the required files
  117. if ($this->db_file)
  118. include_once($this->db_path . $this->db_file);
  119. if ($this->local_file)
  120. include_once($this->local_path . $this->local_file);
  121. // create a db object
  122. $this->db = new $this->db_class;
  123. } // end constructor
  124. function fetch($id, $group) {
  125. $query = sprintf("SELECT expires, cachedata, userdata FROM %s WHERE id = '%s' AND cachegroup = '%s'",
  126. $this->cache_table,
  127. $id,
  128. $group
  129. );
  130. $this->db->query($query);
  131. if (!$this->db->Next_Record())
  132. return array(NULL, NULL, NULL);
  133. // last used required by the garbage collection
  134. // WARNING: might be MySQL specific
  135. $query = sprintf("UPDATE %s SET changed = (NOW() + 0) WHERE id = '%s' AND cachegroup = '%s'",
  136. $this->cache_table,
  137. $id,
  138. $group
  139. );
  140. $this->db->query($query);
  141. return array($this->db->f('expires'), $this->decode($this->db->f('cachedata')), $this->db->f('userdata'));
  142. } // end func fetch
  143. /**
  144. * Stores a dataset.
  145. *
  146. * WARNING: we use the SQL command REPLACE INTO this might be
  147. * MySQL specific. As MySQL is very popular the method should
  148. * work fine for 95% of you.
  149. */
  150. function save($id, $data, $expires, $group) {
  151. $this->flushPreload($id, $group);
  152. $query = sprintf("REPLACE INTO %s (cachedata, expires, id, cachegroup) VALUES ('%s', %d, '%s', '%s')",
  153. $this->cache_table,
  154. addslashes($this->encode($data)),
  155. $this->getExpiresAbsolute($expires) ,
  156. $id,
  157. $group
  158. );
  159. $this->db->query($query);
  160. return (boolean)$this->db->affected_rows();
  161. } // end func save
  162. function remove($id, $group) {
  163. $this->flushPreload($id, $group);
  164. $this->db->query(
  165. sprintf("DELETE FROM %s WHERE id = '%s' AND cachegroup = '%s'",
  166. $this->cache_table,
  167. $id,
  168. $group
  169. )
  170. );
  171. return (boolean)$this->db->affected_rows();
  172. } // end func remove
  173. function flush($group) {
  174. $this->flushPreload();
  175. if ($group) {
  176. $this->db->query(sprintf("DELETE FROM %s WHERE cachegroup = '%s'", $this->cache_table, $group));
  177. } else {
  178. $this->db->query(sprintf("DELETE FROM %s", $this->cache_table));
  179. }
  180. return $this->db->affected_rows();
  181. } // end func flush
  182. function idExists($id, $group) {
  183. $this->db->query(
  184. sprintf("SELECT id FROM %s WHERE ID = '%s' AND cachegroup = '%s'",
  185. $this->cache_table,
  186. $id,
  187. $group
  188. )
  189. );
  190. return (boolean)$this->db->nf();
  191. } // end func isExists
  192. function garbageCollection($maxlifetime) {
  193. $this->flushPreload();
  194. $this->db->query(
  195. sprintf("DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)",
  196. $this->cache_table,
  197. time(),
  198. $maxlifetime
  199. )
  200. );
  201. //check for total size of cache
  202. $query = sprintf('select sum(length(cachedata)) as CacheSize from %s',
  203. $this->cache_table
  204. );
  205. $this->db->query($query);
  206. $this->db->Next_Record();
  207. $cachesize = $this->db->f('CacheSize');
  208. //if cache is to big.
  209. if ($cachesize > $this->highwater)
  210. {
  211. //find the lowwater mark.
  212. $query = sprintf('select length(cachedata) as size, changed from %s order by changed DESC',
  213. $this->cache_table
  214. );
  215. $this->db->query($query);
  216. $keep_size=0;
  217. while ($keep_size < $this->lowwater && $this->db->Next_Record() )
  218. {
  219. $keep_size += $this->db->f('size');
  220. }
  221. //delete all entries, which were changed before the "lowwwater mark"
  222. $query = sprintf('delete from %s where changed <= '.$this->db->f('changed'),
  223. $this->cache_table
  224. );
  225. $this->db->query($query);
  226. }
  227. } // end func garbageCollection
  228. }
  229. ?>