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

Source for file phplib.php

Documentation is available at phplib.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. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: phplib.php,v 1.5 2004/12/15 09:09:30 dufuz Exp $
  20.  
  21. require_once 'Cache/Container.php';
  22.  
  23. /**
  24. * Stores cache data into a database table using PHPLibs DB abstraction.
  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' is used by the garbage collection. Depending on
  34. * your databasesystem you might have to subclass fetch() and garbageCollection().
  35. *
  36. * For _MySQL_ you need this DB table:
  37. *
  38. * CREATE TABLE cache (
  39. *   id          CHAR(32) NOT null DEFAULT '',
  40. *   cachegroup  VARCHAR(127) NOT null DEFAULT '',
  41. *   cachedata   BLOB NOT null DEFAULT '',
  42. *   userdata    VARCHAR(255) NOT null DEFAUL '',
  43. *   expires     INT(9) NOT null DEFAULT 0,
  44. *  
  45. *   changed     TIMESTAMP(14) NOT null,
  46. *  
  47. *   INDEX (expires),
  48. *   PRIMARY KEY (id, cachegroup)
  49. * )
  50. *
  51. * 
  52. * @author   Ulf Wendel  <ulf.wendel@phpdoc.de>, Sebastian Bergmann <sb@sebastian-bergmann.de>
  53. * @version  $Id: phplib.php,v 1.5 2004/12/15 09:09:30 dufuz Exp $
  54. * @package  Cache
  55. * @see      save()
  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 = 'cache';
  64.  
  65.     /**
  66.     * PHPLib object
  67.     * 
  68.     * @var  object PEAR_DB 
  69.     */
  70.     var $db;
  71.  
  72.     /**
  73.     * Name of the PHPLib DB class to use
  74.     * 
  75.     * @var  string 
  76.     * @see  $db_path, $local_path
  77.     */
  78.     var $db_class = '';
  79.  
  80.     /**
  81.     * Filename of your local.inc
  82.     * 
  83.     * If empty, 'local.inc' is assumed.
  84.     *
  85.     * @var       string 
  86.     */
  87.     var $local_file = '';
  88.  
  89.     /**
  90.     * Include path for you local.inc
  91.     *
  92.     * HINT: If your're not using PHPLib's prepend.php you must
  93.     * take care that all classes (files) references by you
  94.     * local.inc are included automatically. So you might
  95.     * want to write a new local2.inc that only referrs to
  96.     * the database class (file) you're using and includes all required files.
  97.     *
  98.     * @var  string  path to your local.inc - make sure to add a trailing slash
  99.     * @see  $local_file
  100.     */
  101.     var $local_path = '';
  102.  
  103.     /**
  104.     * Creates an instance of a phplib db class to use it for storage.
  105.     *
  106.     * @param    mixed   If empty the object tries to used the
  107.     *                    preconfigured class variables. If given it
  108.     *                    must be an array with:
  109.     *                      db_class => name of the DB class to use
  110.     *                    optional:
  111.     *                      db_file => filename of the DB class
  112.     *                      db_path => path to the DB class
  113.     *                      local_file => kind of local.inc
  114.     *                      local_patk => path to the local.inc
  115.     *                    see $local_path for some hints.s
  116.     * @see  $local_path
  117.     */
  118.     function Cache_Container_phplib($options = '')
  119.     {
  120.         if (is_array($options)) {
  121.             $this->setOptions($options,  array_merge($this->allowed_options, array('db_class', 'db_file', 'db_path', 'local_file', 'local_path')));
  122.         }
  123.         if (!$this->db_class) {
  124.             return new Cache_Error('No database class specified.', __FILE__, __LINE__);
  125.         }
  126.         // include the required files
  127.         if ($this->db_file) {
  128.             include_once $this->db_path . $this->db_file;
  129.         }
  130.         if ($this->local_file) {
  131.             include_once $this->local_path . $this->local_file;
  132.         }
  133.         // create a db object 
  134.         $this->db = new $this->db_class;
  135.     } // end constructor
  136.  
  137.     function fetch($id, $group)
  138.     {
  139.         $query = sprintf("SELECT expires, cachedata, userdata FROM %s WHERE id = '%s' AND cachegroup = '%s'",
  140.                             $this->cache_table, 
  141.                             $id,
  142.                             $group
  143.                          );
  144.         $this->db->query($query);
  145.         if (!$this->db->Next_Record()) {
  146.             return array(null, null, null);
  147.         }
  148.         // last used required by the garbage collection   
  149.         // WARNING: might be MySQL specific         
  150.         $query = sprintf("UPDATE %s SET changed = (NOW() + 0) WHERE id = '%s' AND cachegroup = '%s'",
  151.                             $this->cache_table,
  152.                             $id,
  153.                             $group
  154.                           );
  155.         $this->db->query($query);
  156.         return array($this->db->f('expires'), $this->decode($this->db->f('cachedata')), $this->db->f('userdata'));
  157.     } // end func fetch
  158.  
  159.     /**
  160.     * Stores a dataset.
  161.     * 
  162.     * WARNING: we use the SQL command REPLACE INTO this might be
  163.     * MySQL specific. As MySQL is very popular the method should
  164.     * work fine for 95% of you.
  165.     */
  166.     function save($id, $data, $expires, $group)
  167.     {
  168.         $this->flushPreload($id, $group);
  169.  
  170.         $query = sprintf("REPLACE INTO %s (cachedata, expires, id, cachegroup) VALUES ('%s', %d, '%s', '%s')",
  171.                             $this->cache_table,
  172.                             addslashes($this->encode($data)),
  173.                             $this->getExpiresAbsolute($expires) ,
  174.                             $id,
  175.                             $group
  176.                          );
  177.         $this->db->query($query);
  178.  
  179.         return (boolean)$this->db->affected_rows(); 
  180.     } // end func save
  181.  
  182.     function remove($id, $group)
  183.     {
  184.         $this->flushPreload($id, $group);
  185.         $this->db->query(
  186.                         sprintf("DELETE FROM %s WHERE id = '%s' AND cachegroup = '%s'",
  187.                             $this->cache_table,
  188.                             $id,
  189.                             $group
  190.                           )
  191.                     );
  192.  
  193.         return (boolean)$this->db->affected_rows();
  194.     } // end func remove
  195.  
  196.     function flush($group)
  197.     {
  198.         $this->flushPreload();
  199.  
  200.         if ($group) {
  201.             $this->db->query(sprintf("DELETE FROM %s WHERE cachegroup = '%s'", $this->cache_table, $group));    
  202.         } else {
  203.             $this->db->query(sprintf("DELETE FROM %s", $this->cache_table));    
  204.         }
  205.  
  206.         return $this->db->affected_rows();
  207.     } // end func flush
  208.  
  209.     function idExists($id, $group)
  210.     {
  211.         $this->db->query(
  212.                         sprintf("SELECT id FROM %s WHERE ID = '%s' AND cachegroup = '%s'", 
  213.                             $this->cache_table,
  214.                             $id, 
  215.                             $group
  216.                         )   
  217.                     );
  218.  
  219.         return (boolean)$this->db->nf();                         
  220.     } // end func isExists
  221.  
  222.     function garbageCollection($maxlifetime)
  223.     {
  224.         $this->flushPreload();
  225.  
  226.         $this->db->query( 
  227.                         sprintf("DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)",
  228.                             $this->cache_table, 
  229.                             time(),
  230.                             $maxlifetime
  231.                         )
  232.                     );
  233.  
  234.         //check for total size of cache
  235.         $query = sprintf('select sum(length(cachedata)) as CacheSize from %s',
  236.                          $this->cache_table
  237.                        );
  238.  
  239.         $this->db->query($query);
  240.         $this->db->Next_Record();
  241.         $cachesize = $this->db->f('CacheSize');
  242.         //if cache is to big.
  243.         if ($cachesize > $this->highwater) {
  244.             //find the lowwater mark.
  245.             $query = sprintf('select length(cachedata) as size, changed from %s order by changed DESC',
  246.                                      $this->cache_table
  247.                        );
  248.             $this->db->query($query);
  249.  
  250.             $keep_size=0;
  251.             while ($keep_size < $this->lowwater && $this->db->Next_Record()) {
  252.                 $keep_size += $this->db->f('size');
  253.             }
  254.             //delete all entries, which were changed before the "lowwwater mark"
  255.             $query = sprintf('delete from %s where changed <= '.$this->db->f('changed'),
  256.                                      $this->cache_table
  257.                                    );
  258.  
  259.             $this->db->query($query);
  260.         }
  261.     } // end func garbageCollection
  262. }
  263. ?>

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