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

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