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.3 2003/01/04 11:54:46 mj 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.3 2003/01/04 11:54:46 mj 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.         if (is_array($options))
  119.             $this->setOptions($options,  array_merge($this->allowed_optionsarray('db_class''db_file''db_path''local_file''local_path')));
  120.  
  121.         if (!$this->db_class)
  122.             return new Cache_Error('No database class specified.'__FILE____LINE__);
  123.  
  124.         // include the required files
  125.         if ($this->db_file)
  126.             include_once($this->db_path $this->db_file);
  127.  
  128.         if ($this->local_file)
  129.             include_once($this->local_path . $this->local_file);
  130.  
  131.         // create a db object 
  132.         $this->db = new $this->db_class;
  133.     // end constructor
  134.  
  135.     function fetch($id$group{
  136.         $query sprintf("SELECT expires, cachedata, userdata FROM %s WHERE id = '%s' AND cachegroup = '%s'",
  137.                             $this->cache_table
  138.                             $id,
  139.                             $group
  140.                          );
  141.         $this->db->query($query);
  142.         if (!$this->db->Next_Record())
  143.             return array(NULLNULLNULL);
  144.  
  145.         // last used required by the garbage collection   
  146.         // WARNING: might be MySQL specific         
  147.         $query sprintf("UPDATE %s SET changed = (NOW() + 0) WHERE id = '%s' AND cachegroup = '%s'",
  148.                             $this->cache_table,
  149.                             $id,
  150.                             $group
  151.                           );
  152.         $this->db->query($query);
  153.         return array($this->db->f('expires')$this->decode($this->db->f('cachedata'))$this->db->f('userdata'));
  154.     // end func fetch
  155.  
  156.     /**
  157.     * Stores a dataset.
  158.     * 
  159.     * WARNING: we use the SQL command REPLACE INTO this might be
  160.     * MySQL specific. As MySQL is very popular the method should
  161.     * work fine for 95% of you.
  162.     */
  163.     function save($id$data$expires$group{
  164.         $this->flushPreload($id$group);
  165.  
  166.         $query sprintf("REPLACE INTO %s (cachedata, expires, id, cachegroup) VALUES ('%s', %d, '%s', '%s')",
  167.                             $this->cache_table,
  168.                             addslashes($this->encode($data)),
  169.                             $this->getExpiresAbsolute($expires,
  170.                             $id,
  171.                             $group
  172.                          );
  173.         $this->db->query($query);
  174.  
  175.         return (boolean)$this->db->affected_rows()
  176.     // end func save
  177.  
  178.     function remove($id$group{
  179.         $this->flushPreload($id$group);
  180.         $this->db->query(
  181.                         sprintf("DELETE FROM %s WHERE id = '%s' AND cachegroup = '%s'",
  182.                             $this->cache_table,
  183.                             $id,
  184.                             $group
  185.                           )
  186.                     );
  187.  
  188.         return (boolean)$this->db->affected_rows();
  189.     // end func remove
  190.  
  191.     function flush($group{
  192.         $this->flushPreload();
  193.  
  194.         if ($group{
  195.             $this->db->query(sprintf("DELETE FROM %s WHERE cachegroup = '%s'"$this->cache_table$group));    
  196.         else {
  197.             $this->db->query(sprintf("DELETE FROM %s"$this->cache_table));    
  198.         }
  199.  
  200.         return $this->db->affected_rows();
  201.     // end func flush
  202.  
  203.     function idExists($id$group{
  204.         $this->db->query(
  205.                         sprintf("SELECT id FROM %s WHERE ID = '%s' AND cachegroup = '%s'"
  206.                             $this->cache_table,
  207.                             $id
  208.                             $group
  209.                         )   
  210.                     );
  211.  
  212.         return (boolean)$this->db->nf();                         
  213.     // end func isExists
  214.  
  215.     function garbageCollection($maxlifetime{
  216.         $this->flushPreload();
  217.  
  218.         $this->db->query
  219.                         sprintf("DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)",
  220.                             $this->cache_table
  221.                             time(),
  222.                             $maxlifetime
  223.                         )
  224.                     );
  225.  
  226.         //check for total size of cache
  227.         $query sprintf('select sum(length(cachedata)) as CacheSize from %s',
  228.                          $this->cache_table
  229.                        );
  230.  
  231.         $this->db->query($query);
  232.         $this->db->Next_Record();
  233.         $cachesize $this->db->f('CacheSize');
  234.         //if cache is to big.
  235.         if ($cachesize $this->highwater)
  236.         {
  237.             //find the lowwater mark.
  238.             $query sprintf('select length(cachedata) as size, changed from %s order by changed DESC',
  239.                                      $this->cache_table
  240.                        );
  241.             $this->db->query($query);
  242.  
  243.             $keep_size=0;
  244.             while ($keep_size $this->lowwater && $this->db->Next_Record() )
  245.             {
  246.                 $keep_size += $this->db->f('size');
  247.             }
  248.             //delete all entries, which were changed before the "lowwwater mark"
  249.             $query sprintf('delete from %s where changed <= '.$this->db->f('changed'),
  250.                                      $this->cache_table
  251.                                    );
  252.  
  253.             $this->db->query($query);
  254.         }
  255.     // end func garbageCollection
  256. }
  257. ?>

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