Source for file Session.php

  1. <?php
  2.  
  3. /**
  4.  * Custom Session for work with Memcache
  5.  *
  6.  * @package Session
  7.  * @use core_view_classes_Memcache_Memcache
  8.  *
  9.  */
  10.  
  11. if (version_compare(PHP_VERSION'5')) {
  12.     die('This file was generated for PHP 5');
  13. }
  14.  
  15. /**
  16.  * include core_view_classes_Memcache_Memcache
  17.  *
  18.  */
  19. require_once(CFG_BASE_ROOT '/core/view/classes/Memcache/Memcache.php');
  20.  
  21. /* user defined includes */
  22. // section -64--88-1--106--58e7bf35:12d1e1d4191:-8000:0000000000001FBC-includes begin
  23. // section -64--88-1--106--58e7bf35:12d1e1d4191:-8000:0000000000001FBC-includes end
  24.  
  25. /* user defined constants */
  26. // section -64--88-1--106--58e7bf35:12d1e1d4191:-8000:0000000000001FBC-constants begin
  27. // section -64--88-1--106--58e7bf35:12d1e1d4191:-8000:0000000000001FBC-constants end
  28.  
  29. /**
  30.  * Custom Session for work with Memcache
  31.  *
  32.  * @package Session
  33.  * @use core_view_classes_Memcache_Memcache
  34.  *
  35.  * @access public
  36.  */
  37. {
  38.     // --- ASSOCIATIONS ---
  39.  
  40.  
  41.     // --- ATTRIBUTES ---
  42.  
  43.     /**
  44.      * The instance of the class Memcache
  45.      *
  46.      * @access private
  47.      * @var Memcache 
  48.      */
  49.     private static $MC null;
  50.  
  51.     /**
  52.      * The session expiration time
  53.      *
  54.      * @access private
  55.      * @var int 
  56.      */
  57.     private static $sessionTime 1440;
  58.  
  59.     /**
  60.      * Unique ID as a prefix for session key
  61.      *
  62.      * @access private
  63.      * @var string 
  64.      */
  65.     private static $keyPrefix '';
  66.  
  67.     /**
  68.      * The waiting timeout unlocking session
  69.      *
  70.      * @access private
  71.      * @var int 
  72.      */
  73.     private static $sleepTime 100000;
  74.  
  75.     /**
  76.      * The session_key_lock expiration time
  77.      *
  78.      * @access private
  79.      * @var int 
  80.      */
  81.     private static $lockTime 10;
  82.  
  83.     /**
  84.      * The level of compression
  85.      *
  86.      * @access private
  87.      * @var int 
  88.      */
  89.     private static $gzSize 300;
  90.  
  91.     /**
  92.      * The variable to store the CAS token in
  93.      *
  94.      * @access private
  95.      * @var int 
  96.      */
  97.     private static $version null;
  98.  
  99.     /**
  100.      * The session key after prepare
  101.      *
  102.      * @access public
  103.      * @var string 
  104.      */
  105.     public static $session_key '';
  106.  
  107.     // --- OPERATIONS ---
  108.  
  109.     /**
  110.      * Create a new instance of the class Memcache
  111.      *
  112.      * @access public
  113.      * @param  string host The hostname of the memcache server
  114.      * @param  string port The port on which memcache is running
  115.      * @param  string mPrefix Unique ID as a prefix for memcache key
  116.      * @param  string sPrefix Unique ID as a prefix for session key
  117.      */
  118.     public static function Init($host$port$mPrefix$sPrefix{
  119.         // section -64--88-1--106--4b532b66:12d287c133d:-8000:0000000000001FE8 begin
  120.         
  121.         self::$MC new core_view_classes_Memcache_Memcache($host$port$mPrefix);
  122.         self::$MC->lockTime self::$lockTime;
  123.         
  124.         self::$keyPrefix $sPrefix;
  125.         
  126.         self::WriteLog(__FUNCTION__var_export(array($host$port$mPrefix$sPrefix)true));
  127.         
  128.         // section -64--88-1--106--4b532b66:12d287c133d:-8000:0000000000001FE8 end
  129.     }
  130.  
  131.     /**
  132.      * Reads the requested session data
  133.      *
  134.      * @access public
  135.      * @param  string key Unique session ID
  136.      * @param  boolean withLock Read session with lock as default value
  137.      * @return string 
  138.      */
  139.     public static function Read($key$withLock true{
  140.         $returnValue = (string) '';
  141.  
  142.         // section -64--88-1--106--58e7bf35:12d1e1d4191:-8000:0000000000001FC0 begin
  143.         
  144.         self::WriteLog(__FUNCTION__$key.', '$withLock.', version - '.self::$version);
  145.         
  146.         if ($withLock{
  147.             self::$session_key self::PrepareKey($key);
  148.             while (!self::$MC->Lock(self::$session_keyself::$version)) {
  149.                 usleep(self::$sleepTime);
  150.             }
  151.         }
  152.  
  153.         $returnValue self::$MC->get($key);
  154.  
  155.         if ($returnValue{
  156.             if (@$tmp gzuncompress($returnValue)) {
  157.                 $returnValue $tmp;
  158.             }
  159.         }
  160.         
  161.         // section -64--88-1--106--58e7bf35:12d1e1d4191:-8000:0000000000001FC0 end
  162.  
  163.         return (string) $returnValue;
  164.     }
  165.  
  166.     /**
  167.      * Writes the provided session data with the requested key
  168.      *
  169.      * @access public
  170.      * @param  string key Unique session ID
  171.      * @param  value The value to store
  172.      * @return boolean 
  173.      */
  174.     public static function Write($key$value{
  175.         $returnValue = (bool) false;
  176.  
  177.         // section -64--88-1--106--58e7bf35:12d1e1d4191:-8000:0000000000001FC2 begin
  178.         
  179.         self::WriteLog(__FUNCTION__$key.', 'var_export($valuetrue).', version - '.self::$version);
  180.  
  181.         $session_key self::PrepareKey($key);
  182.         if (self::$MC->CheckLock($session_keyself::$version)) {
  183.             if (mb_strlen($value)>self::$gzSize{
  184.                 $value gzcompress($value6);
  185.             }
  186.             $returnValue self::$MC->set($key$valueself::$sessionTime);
  187.             self::$MC->Unlock($session_key);
  188.         }
  189.         
  190.         // section -64--88-1--106--58e7bf35:12d1e1d4191:-8000:0000000000001FC2 end
  191.  
  192.         return (bool) $returnValue;
  193.     }
  194.  
  195.     /**
  196.      * Destroys the requested session
  197.      *
  198.      * @access public
  199.      * @param  string key Unique session ID
  200.      * @return mixed 
  201.      */
  202.     public static function Destroy($key{
  203.         // section -64--88-1--106--44318241:12d1e485c5b:-8000:0000000000001FCC begin
  204.         
  205.         self::WriteLog(__FUNCTION__$key.', '$key.', version - '.self::$version);
  206.         
  207.         $session_key self::PrepareKey($key);
  208.         if (self::$MC->CheckLock($session_keyself::$version)) {
  209.             $returnValue self::$MC->delete($key);
  210.             self::$MC->Unlock($session_key);
  211.         }
  212.         
  213.         // section -64--88-1--106--44318241:12d1e485c5b:-8000:0000000000001FCC end
  214.     }
  215.  
  216.     /**
  217.      * Make new key for lock session
  218.      *
  219.      * @access public
  220.      * @param  string key Unique session ID
  221.      * @return string Return a new key with prefix
  222.      */
  223.     public static function PrepareKey($key{
  224.         $returnValue = (string) '';
  225.  
  226.         // section -64--88-1--106--1adfd58c:12d2c271cbb:-8000:000000000000200F begin
  227.  
  228.         $returnValue md5(self::$keyPrefix $key);
  229.         
  230.         self::WriteLog(__FUNCTION__$returnValue);
  231.  
  232.         // section -64--88-1--106--1adfd58c:12d2c271cbb:-8000:000000000000200F end
  233.  
  234.         return (string) $returnValue;
  235.     }
  236.  
  237.     /**
  238.      * Write a text to the log file
  239.      *
  240.      * @access protected
  241.      * @param  string name The function name
  242.      * @param  string text The string that is to be written
  243.      * @return mixed 
  244.      */
  245.     protected static function WriteLog($name$text ''{
  246.         // section -64--88-1--106-42af027e:12d32ad9c50:-8000:0000000000002029 begin
  247.         
  248.         Logs::Write(__CLASS__$name$text);
  249.         
  250.         // section -64--88-1--106-42af027e:12d32ad9c50:-8000:0000000000002029 end
  251.     }
  252.  
  253.     /**
  254.      * Prolong expiration time
  255.      *
  256.      * @access public
  257.      * @param  int time Prolong time in second
  258.      * @return boolean 
  259.      */
  260.     public static function Prolong($time 10{
  261.         $returnValue = (bool) false;
  262.  
  263.         // section -64--88-1--106-3b746a61:12de6c64a8f:-8000:00000000000023F6 begin
  264.         
  265.         if (self::$MC->CheckLock(self::$session_keyself::$version)) {
  266.             $returnValue self::$MC->ProlongLock(self::$session_keyself::$version$time);
  267.         }
  268.         
  269.         // section -64--88-1--106-3b746a61:12de6c64a8f:-8000:00000000000023F6 end
  270.  
  271.         return (bool) $returnValue;
  272.     }
  273.  
  274.     /**
  275.      * Create a new instance of the class Memcache
  276.      *
  277.      * @access private
  278.      * @return mixed 
  279.      */
  280.     private function __construct({
  281.         // section -64--88-1--106--7edefe2a:12de72d82df:-8000:000000000000218E begin
  282.         // section -64--88-1--106--7edefe2a:12de72d82df:-8000:000000000000218E end
  283.     }
  284.  
  285. /* end of class core_view_classes_Session */
  286.  
  287. ?>

Documentation generated on Thu, 03 Mar 2011 20:14:16 +0300 by phpDocumentor 1.4.3