Cache
[ class tree: Cache ] [ index: Cache ] [ all elements ]

Source for file db.php

Documentation is available at db.php

  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. // |          Chuck Hagenbuch <chuck@horde.org>                           |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: db.php,v 1.5 2003/01/04 11:54:46 mj Exp $
  21.  
  22. require_once 'DB.php';
  23. require_once 'Cache/Container.php';
  24.  
  25. /**
  26. * PEAR/DB Cache Container.
  27. *
  28. * WARNING: Other systems might or might not support certain datatypes of
  29. * the tables shown. As far as I know there's no large binary
  30. * type in SQL-92 or SQL-99. Postgres seems to lack any
  31. * BLOB or TEXT type, for MS-SQL you could use IMAGE, don't know
  32. * about other databases. Please add sugestions for other databases to
  33. * the inline docs.
  34. *
  35. * The field 'changed' has no meaning for the Cache itself. It's just there
  36. * because it's a good idea to have an automatically updated timestamp
  37. * field for debugging in all of your tables.
  38. *
  39. * For _MySQL_ you need this DB table:
  40. *
  41. * CREATE TABLE cache (
  42. *   id          CHAR(32) NOT NULL DEFAULT '',
  43. *   cachegroup  VARCHAR(127) NOT NULL DEFAULT '',
  44. *   cachedata   BLOB NOT NULL DEFAULT '',
  45. *   userdata    VARCHAR(255) NOT NULL DEFAUL '',
  46. *   expires     INT(9) NOT NULL DEFAULT 0,
  47. *
  48. *   changed     TIMESTAMP(14) NOT NULL,
  49. *
  50. *   INDEX (expires),
  51. *   PRIMARY KEY (id, cachegroup)
  52. * )
  53. *
  54. @author   Sebastian Bergmann <sb@sebastian-bergmann.de>
  55. @version  $Id: db.php,v 1.5 2003/01/04 11:54:46 mj Exp $
  56. @package  Cache
  57. */
  58.  
  59.     /**
  60.     * Name of the DB table to store caching data
  61.     *
  62.     * @see  Cache_Container_file::$filename_prefix
  63.     */
  64.     var $cache_table = '';
  65.  
  66.     /**
  67.     * PEAR DB dsn to use.
  68.     *
  69.     * @var  string 
  70.     */
  71.     var $dsn = '';
  72.  
  73.     /**
  74.     * PEAR DB object
  75.     *
  76.     * @var  object PEAR_DB 
  77.     */
  78.     var $db;
  79.  
  80.     function Cache_Container_db($options)
  81.     {
  82.         if (!is_array($options|| !isset($options['dsn'])) {
  83.             return new Cache_Error('No dsn specified!'__FILE____LINE__);
  84.         }
  85.  
  86.         $this->setOptions($options,  array_merge($this->allowed_optionsarray('dsn''cache_table')));
  87.  
  88.         if (!$this->dsn)
  89.             return new Cache_Error('No dsn specified!'__FILE____LINE__);
  90.  
  91.         $this->db = DB::connect($this->dsntrue);
  92.         if (DB::isError($this->db)) {
  93.             return new Cache_Error('DB::connect failed: ' . DB::errorMessage($this->db)__FILE____LINE__);
  94.         else {
  95.             $this->db->setFetchMode(DB_FETCHMODE_ASSOC);
  96.         }
  97.     }
  98.  
  99.     function fetch($id$group)
  100.     {
  101.         $query sprintf("SELECT cachedata, userdata, expires FROM %s WHERE id = '%s' AND cachegroup = '%s'",
  102.                          $this->cache_table,
  103.                          addslashes($id),
  104.                          addslashes($group)
  105.                          );
  106.  
  107.         $res $this->db->query($query);
  108.  
  109.         if (DB::isError($res))
  110.             return new Cache_Error('DB::query failed: ' . DB::errorMessage($res)__FILE____LINE__);
  111.  
  112.         $row $res->fetchRow();
  113.         if (is_array($row))
  114.             $data = array($row['expires']$this->decode($row['cachedata'])$row['userdata']);
  115.         else
  116.             $data = array(NULLNULLNULL);
  117.  
  118.         // last used required by the garbage collection
  119.         // WARNING: might be MySQL specific
  120.         $query sprintf("UPDATE %s SET changed = (NOW() + 0) WHERE id = '%s' AND cachegroup = '%s'",
  121.                             $this->cache_table,
  122.                             addslashes($id),
  123.                             addslashes($group)
  124.                           );
  125.  
  126.         $res $this->db->query($query);
  127.  
  128.         if (DB::isError($res))
  129.             return new Cache_Error('DB::query failed: ' . DB::errorMessage($res)__FILE____LINE__);
  130.  
  131.         return $data;
  132.     }
  133.  
  134.     /**
  135.     * Stores a dataset.
  136.     *
  137.     * WARNING: we use the SQL command REPLACE INTO this might be
  138.     * MySQL specific. As MySQL is very popular the method should
  139.     * work fine for 95% of you.
  140.     */
  141.     function save($id$data$expires$group$userdata)
  142.     {
  143.         $this->flushPreload($id$group);
  144.  
  145.         $query sprintf("REPLACE INTO %s (userdata, cachedata, expires, id, cachegroup) VALUES ('%s', '%s', %d, '%s', '%s')",
  146.                          $this->cache_table,
  147.                          addslashes($userdata),
  148.                          addslashes($this->encode($data)),
  149.                           $this->getExpiresAbsolute($expires,
  150.                          addslashes($id),
  151.                          addslashes($group)
  152.                         );
  153.  
  154.         $res $this->db->query($query);
  155.  
  156.         if (DB::isError($res)) {
  157.             return new Cache_Error('DB::query failed: ' . DB::errorMessage($res__FILE____LINE__);
  158.         }
  159.     }
  160.  
  161.     function remove($id$group)
  162.     {
  163.         $this->flushPreload($id$group);
  164.  
  165.         $query sprintf("DELETE FROM %s WHERE id = '%s' and cachegroup = '%s'",
  166.                          $this->cache_table,
  167.                          addslashes($id),
  168.                          addslashes($group)
  169.                         );
  170.  
  171.         $res $this->db->query($query);
  172.  
  173.         if (DB::isError($res))
  174.             return new Cache_Error('DB::query failed: ' . DB::errorMessage($res)__FILE____LINE__);
  175.     }
  176.  
  177.     function flush($group '')
  178.     {
  179.         $this->flushPreload();
  180.  
  181.          if ($group{
  182.             $query sprintf("DELETE FROM %s WHERE cachegroup = '%s'"$this->cache_tableaddslashes($group));
  183.         else {
  184.             $query sprintf("DELETE FROM %s"$this->cache_table);
  185.         }
  186.  
  187.         $res $this->db->query($query);
  188.  
  189.         if (DB::isError($res))
  190.             return new Cache_Error('DB::query failed: ' . DB::errorMessage($res)__FILE____LINE__);
  191.     }
  192.  
  193.     function idExists($id$group)
  194.     {
  195.         $query sprintf("SELECT id FROM %s WHERE ID = '%s' AND cachegroup = '%s'",
  196.                          $this->cache_table,
  197.                          addslashes($id),
  198.                          addslashes($group)
  199.                         );
  200.  
  201.         $res $this->db->query($query);
  202.  
  203.         if (DB::isError($res))
  204.             return new Cache_Error('DB::query failed: ' . DB::errorMessage($res)__FILE____LINE__);
  205.  
  206.         $row $res->fetchRow();
  207.  
  208.         if (is_array($row)) {
  209.             return true;
  210.         else {
  211.             return false;
  212.         }
  213.     }
  214.  
  215.     function garbageCollection($maxlifetime)
  216.     {
  217.         $this->flushPreload();
  218.  
  219.         $query sprintf('DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= %d',
  220.                          $this->cache_table,
  221.                          time(),
  222.                          time($maxlifetime
  223.                        );
  224.  
  225.         $res $this->db->query($query);
  226.  
  227.         $query sprintf('select sum(length(cachedata)) as CacheSize from %s',
  228.                          $this->cache_table
  229.                        );
  230.         $cachesize $this->db->GetOne($query);
  231.         if (DB::isError($cachesize)) {
  232.             return new Cache_Error('DB::query failed: ' . DB::errorMessage($cachesize)__FILE____LINE__);
  233.         }
  234.  
  235.         //if cache is to big.
  236.         if ($cachesize $this->highwater)
  237.         {
  238.             //find the lowwater mark.
  239.             $query sprintf('select length(cachedata) as size, changed from %s order by changed DESC',
  240.                                      $this->cache_table
  241.                        );
  242.             $res $this->db->query($query);
  243.             if (DB::isError($res)) {
  244.                 return new Cache_Error('DB::query failed: ' . DB::errorMessage($res)__FILE____LINE__);
  245.             }
  246.  
  247.             $numrows $this->db->numRows($res);
  248.             $keep_size = 0;
  249.             while ($keep_size $this->lowwater && $numrows--{
  250.                 $entry $res->fetchRow(DB_FETCHMODE_ASSOC);
  251.                 $keep_size += $entry['size'];
  252.             }
  253.  
  254.             //delete all entries, which were changed before the "lowwwater mark"
  255.             $query sprintf('delete from %s where changed <= '.($entry['changed'$entry['changed': 0),
  256.                                      $this->cache_table
  257.                                    );
  258.             $res $this->db->query($query);
  259.         }
  260.  
  261.         if (DB::isError($res)) {
  262.             return new Cache_Error('DB::query failed: ' . DB::errorMessage($res)__FILE____LINE__);
  263.         }
  264.     }
  265.  
  266. }
  267. ?>

Documentation generated on Mon, 11 Mar 2019 10:14:23 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.