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

Documentation generated on Mon, 11 Mar 2019 15:44:49 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.