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

Source for file dbx.php

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

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