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

Source for file msession.php

Documentation is available at msession.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. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: msession.php,v 1.4 2003/01/04 11:54:46 mj Exp $
  19.  
  20. require_once 'Cache/Container.php';
  21.  
  22. /**
  23. * Stores cache contents in msessions.
  24. *
  25. * WARNING: experimental, untested
  26. *
  27. @author   Ulf Wendel  <ulf.wendel@phpdoc.de>
  28. @version  $Id: msession.php,v 1.4 2003/01/04 11:54:46 mj Exp $
  29. */
  30.  
  31.  
  32.     /**
  33.     * Length of the Cache-Identifier
  34.     *
  35.     * Note that the PEAR-Cache prefixes the ID with an md5() value
  36.     * of the cache-group. A good value for the id_length
  37.     * depends on the maximum number of entries per cache group.
  38.     *
  39.     * @var  int 
  40.     */
  41.     var $id_length = 32;
  42.     
  43.     
  44.     /**
  45.     * Use msession_uniq to create a unique SID.
  46.     * 
  47.     * @var  boolean 
  48.     */
  49.     var $uniq = true;
  50.     
  51.     
  52.     /**
  53.     * Establish a connection to a msession server?
  54.     *
  55.     * @var  boolean 
  56.     */
  57.     var $connect = true;
  58.    
  59.    
  60.     /**
  61.     * msession host
  62.     *
  63.     * @var  string 
  64.     */  
  65.     var $host = NULL;
  66.     
  67.    
  68.     /**
  69.     * msession port
  70.     *
  71.     * @var  string 
  72.     */
  73.     var $port = NULL;
  74.     
  75.     
  76.     /**
  77.     * mesession server connection
  78.     *
  79.     * @var  resource msession
  80.     */
  81.     var $ms = NULL;
  82.  
  83.     
  84.     function Cache_Container_msession($options ''{
  85.         if (is_array($options))
  86.             $this->setOptions($optionsarray_merge($this->allowed_optionsarray('id_lenght''uniq''host''port''connect')));
  87.  
  88.         if ($connect{            
  89.             if (NULL == $this->host)
  90.                 new Cache_Error('No host specified.'__FILE____LINE__);
  91.             if (NULL == $this->port)
  92.                 new Cache_Error('No port specified.'__FILE____LINE__);
  93.         
  94.             if (!($this->ms = msession_connect($this->host$this->port)))
  95.                 new Cache_Error('Can not connect to the sever using host "' $this->host . '" on port "' $this->port . '"'__FILE____LINE__);
  96.         }
  97.         
  98.     // end func contructor
  99.  
  100.     function fetch($id$group{
  101.     
  102.         $id strtoupper(md5($group)) $id;
  103.         $group = msession_get($id'_pear_cache_data'NULL);
  104.         
  105.         if (NULL == $data)
  106.             return array(NULLNULLNULL);
  107.         
  108.         return array($data['expires']$data['cachedata']$data['userdata']);
  109.     // end func fetch
  110.  
  111.     /**
  112.     * Stores a dataset.
  113.     *
  114.     * WARNING: If you supply userdata it must not contain any linebreaks,
  115.     * otherwise it will break the filestructure.
  116.     */
  117.     function save($id$cachedata$expires$group$userdata{
  118.         $this->flushPreload($id$group);
  119.         
  120.         $cachedata      $this->encode($cachedata);
  121.         $expires_abs    $this->getExpiresAbsolute($expires);
  122.  
  123.         $size = 1 + strlen($cachedatastrlen($expires_absstrlen($userdatastrlen($group);
  124.         $size += strlen($size);
  125.         
  126.         $data = array(
  127.                     'cachedata' => $cachedata
  128.                     'expires'   => $expires_abs,
  129.                     'userdata'  => $userdata
  130.                   );
  131.         $id strtoupper(md5($group)) $id;
  132.                             
  133.         msession_lock($id);
  134.         
  135.         if (!msession_set($id'_pear_cache'true)) {
  136.             msession_unlock($id);
  137.             return new Cache_Error("Can't write cache data."__FILE____LINE__);
  138.         }
  139.         
  140.         if (!msession_set($id'_pear_cache_data'$data)) {
  141.             msession_unlock($id);
  142.             return new Cache_Error("Can't write cache data."__FILE____LINE__);
  143.         }
  144.         
  145.         if (!msession_set($id'_pear_cache_group'$group)) {
  146.             msession_unlock($id);
  147.             return new Cache_Error("Can't write cache data."__FILE____LINE__);
  148.         }
  149.         
  150.         if (!msession_set($id'_pear_cache_size'$size)) {
  151.             msession_unlock($id);
  152.             return new Cache_Error("Can't write cache data."__FILE____LINE__);
  153.         }
  154.         
  155.         // let msession do some GC as well
  156.         // note that msession works different from the PEAR Cache.
  157.         // msession deletes an entry if it has not been used for n-seconds.
  158.         // PEAR Cache deletes after n-seconds.
  159.         if (0 != $expires)
  160.             msession_timeout($id$expires);
  161.             
  162.         msession_unlock($id);
  163.  
  164.         return true;
  165.     // end func save
  166.  
  167.     function remove($id$group{
  168.         $this->flushPreload($id$group);
  169.     
  170.         return msession_destroy(strtoupper(md5($group)) $id);
  171.     // end func remove
  172.  
  173.     function flush($group{
  174.         $this->flushPreload();
  175.       
  176.         $sessions = msession_find('_pear_cache_group'$group);
  177.         if (empty($sessions))
  178.             return 0;
  179.         
  180.         foreach ($sessions as $k => $id)
  181.             messsion_destroy($id);
  182.  
  183.         return count($sessions);
  184.     // end func flush
  185.  
  186.     function idExists($id$group{
  187.         
  188.         return (NULL == msession_get(strtoupper(md5($group)) $id'_pear_cache_group'NULL)) ? false : true;
  189.     // end func idExists
  190.  
  191.     /**
  192.     * Deletes all expired files.
  193.     *
  194.     * Note: garbage collection should cause lot's of network traffic.
  195.     *
  196.     * @param    integer Maximum lifetime in seconds of an no longer used/touched entry
  197.     * @throws   Cache_Error
  198.     */
  199.     function garbageCollection($maxlifetime{
  200.         $this->flushPreload();
  201.         
  202.         $sessions = msession_find('_pear_cache'true);
  203.         if (empty($sessions))
  204.             return true;
  205.         
  206.         $total = 0;
  207.         $entries = array();
  208.         
  209.         foreach ($sessions as $k => $id{
  210.             $data = msession_get($id'_pear_cache_data'NULL);
  211.             if (NULL == $data)
  212.                 continue;
  213.                 
  214.             if ($data['expires'<= time()) {
  215.                 msession_destroy($id);
  216.                 continue;
  217.             }
  218.             
  219.             $size = msession_get($id'_pear_cache_size'NULL);
  220.             $total += $size;
  221.             $entries[$data['expires']] = array($id$size);
  222.         }
  223.         
  224.         if ($total $this->highwater{
  225.             
  226.             krsort($entries);
  227.             reset($entries);
  228.             
  229.             while ($total $this->lowwater && list($expires$entryeach($entries)) {
  230.                 msession_destroy($entry[0]);
  231.                 $total -= $entry[1];
  232.             }
  233.             
  234.         }
  235.         
  236.         return true;
  237.     // end func garbageCollection
  238.     
  239. // end class file
  240. ?>

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