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

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