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 316194 2011-09-05 20:35:25Z cweiske $
  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 316194 2011-09-05 20:35:25Z cweiske $
  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.         $this->setAllowedOptions(
  86.             array('id_length''uniq''host''port''connect')
  87.         );
  88.         $this->setOptions($options)
  89.         if ($connect{
  90.             if ($this->host == null{
  91.                 new Cache_Error('No host specified.'__FILE____LINE__);
  92.             }
  93.             if (!$this->hasBeenSet('port')) {
  94.                 new Cache_Error('No port specified.'__FILE____LINE__);
  95.             }
  96.             if (!($this->ms = msession_connect($this->host$this->port))) {
  97.                 new Cache_Error(
  98.                     'Can not connect to the sever using host "' 
  99.                     $this->host . '" on port "' $this->port . '"',
  100.                     __FILE____LINE__
  101.                 );
  102.             }
  103.         }
  104.         
  105.     // end func contructor
  106.  
  107.     function fetch($id$group)
  108.     {
  109.         $id strtoupper(md5($group)) $id;
  110.         $group = msession_get($id'_pear_cache_data'null);
  111.         
  112.         if ($data == null{
  113.             return array(nullnullnull);
  114.         }
  115.         return array($data['expires']$data['cachedata']$data['userdata']);
  116.     // end func fetch
  117.  
  118.     /**
  119.     * Stores a dataset.
  120.     *
  121.     * WARNING: If you supply userdata it must not contain any linebreaks,
  122.     * otherwise it will break the filestructure.
  123.     */
  124.     function save($id$cachedata$expires$group$userdata)
  125.     {
  126.         $this->flushPreload($id$group);
  127.         
  128.         $cachedata      $this->encode($cachedata);
  129.         $expires_abs    $this->getExpiresAbsolute($expires);
  130.  
  131.         $size = 1 + strlen($cachedatastrlen($expires_absstrlen($userdatastrlen($group);
  132.         $size += strlen($size);
  133.         
  134.         $data = array(
  135.                     'cachedata' => $cachedata
  136.                     'expires'   => $expires_abs,
  137.                     'userdata'  => $userdata
  138.                   );
  139.         $id strtoupper(md5($group)) $id;
  140.                             
  141.         msession_lock($id);
  142.         
  143.         if (!msession_set($id'_pear_cache'true)) {
  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_data'$data)) {
  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_group'$group)) {
  154.             msession_unlock($id);
  155.             return new Cache_Error("Can't write cache data."__FILE____LINE__);
  156.         }
  157.         
  158.         if (!msession_set($id'_pear_cache_size'$size)) {
  159.             msession_unlock($id);
  160.             return new Cache_Error("Can't write cache data."__FILE____LINE__);
  161.         }
  162.         
  163.         // let msession do some GC as well
  164.         // note that msession works different from the PEAR Cache.
  165.         // msession deletes an entry if it has not been used for n-seconds.
  166.         // PEAR Cache deletes after n-seconds.
  167.         if ($expires != 0{
  168.             msession_timeout($id$expires);
  169.         }
  170.         msession_unlock($id);
  171.  
  172.         return true;
  173.     // end func save
  174.  
  175.     function remove($id$group)
  176.     {
  177.         $this->flushPreload($id$group);
  178.         return msession_destroy(strtoupper(md5($group)) $id);
  179.     // end func remove
  180.  
  181.     function flush($group)
  182.     {
  183.         $this->flushPreload();
  184.       
  185.         $sessions = msession_find('_pear_cache_group'$group);
  186.         if (empty($sessions)) {
  187.             return 0;
  188.         }
  189.  
  190.         foreach ($sessions as $k => $id)
  191.             messsion_destroy($id);
  192.  
  193.         return count($sessions);
  194.     // end func flush
  195.  
  196.     function idExists($id$group)
  197.     {
  198.         return (msession_get(strtoupper(md5($group)) $id'_pear_cache_group'null== null? false : true;
  199.     // end func idExists
  200.  
  201.     /**
  202.     * Deletes all expired files.
  203.     *
  204.     * Note: garbage collection should cause lot's of network traffic.
  205.     *
  206.     * @param    integer Maximum lifetime in seconds of an no longer used/touched entry
  207.     * @throws   Cache_Error
  208.     */
  209.     function garbageCollection($maxlifetime)
  210.     {
  211.         $this->flushPreload();
  212.         
  213.         $sessions = msession_find('_pear_cache'true);
  214.         if (empty($sessions))
  215.             return true;
  216.         
  217.         $total = 0;
  218.         $entries = array();
  219.         
  220.         foreach ($sessions as $k => $id{
  221.             $data = msession_get($id'_pear_cache_data'null);
  222.             if (null == $data{
  223.                 continue;
  224.             }
  225.  
  226.             if ($data['expires'<= time()) {
  227.                 msession_destroy($id);
  228.                 continue;
  229.             }
  230.             
  231.             $size = msession_get($id'_pear_cache_size'null);
  232.             $total += $size;
  233.             $entries[$data['expires']] = array($id$size);
  234.         }
  235.         
  236.         if ($total $this->highwater{
  237.             
  238.             krsort($entries);
  239.             reset($entries);
  240.             
  241.             while ($total $this->lowwater && list($expires$entryeach($entries)) {
  242.                 msession_destroy($entry[0]);
  243.                 $total -= $entry[1];
  244.             }
  245.             
  246.         }
  247.         
  248.         return true;
  249.     // end func garbageCollection
  250.     
  251. // end class file
  252. ?>

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