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

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