解决WEB集群session同步的方案,Redis内存缓存

来源:互联网 发布:linux安装glibc2.14 编辑:程序博客网 时间:2024/06/06 00:42

转自:http://www.sziwap.com/archives/75.html

最近公司WEB服务器换集群方式,集群所带来直接的问题就是session共享。
如果用PHP自带的session处理方式,又要达到一致性,我已知的解决方案是NFS方法,不过担心磁盘性能以及session的处理机制,决定放弃这种方法,最后决定用内存缓存服务器来实现。

公司目前主要缓存的使用已经全部转至Redis下面(主要因为我的极力推荐,呵呵)。所以几简单写了个类实现了对session的操作,后续还要进行优化和扩展,前期没办法呀,公司催得紧呀。。。。

下面把代码贴出来,大家也分享一下了。呵呵。有啥意见也可以提提,别拍砖。呵呵。

[php] view plaincopyprint?
  1. /** 
  2.  * @author shenjun 
  3.  * @Createdate 2010-10-14 
  4.  * @todo session机制,存在redis内存中,解决web集群中session共享问题 
  5.  */  
  6. class Session{  
  7.   
  8.     static protected $connect = FALSE;  
  9.     protected $redis = NULL;  
  10.     protected $redis_host = '192.168.1.107';  
  11.     protected $redis_port = '6379';  
  12.     protected $sess_id = NULL ;  
  13.     protected $sess_life = 300 ;  
  14.     protected $sessions = array () ;  
  15.     ##是否自动保存session,默认为自动保存  
  16.     protected $auto_save = true ;  
  17.     ##判断是否有修改过session 中的值  
  18.     protected $changed = false;  
  19.     /** 
  20.      * @todo redis初始化方法,单例入口 
  21.      * @desc 自动判断系统是否带redis,则是否有编译redis的客户端环境 
  22.      */  
  23.     static public function singleton()  
  24.     {  
  25.         if ( self::$connect == FALSE )  
  26.         {  
  27.             self::$connect = new Session();  
  28.         }  
  29.         return self::$connect;  
  30.     }  
  31.     /** 
  32.      * @todo 构造函数 
  33.      * @desc 建立redis连接,取得已有sessionID,并取得所有session的值 
  34.      */  
  35.     protected function __construct()  
  36.     {  
  37.         if ( class_exists'redis' ) )  
  38.         {  
  39.             $redis = new Redis (  );  
  40.             $conn = $redis->connect( $this->redis_host  , $this->redis_port );  
  41.         } else {  
  42.             require_once dirname(__FILE__). DIRECTORY_SEPARATOR . 'PhpRedis.php';  
  43.             $redis = new PhpRedis ( $this->redis_host  , $this->redis_port );  
  44.             $conn = $redis->connect();  
  45.         }  
  46.         if ( $conn )  
  47.         {  
  48.             $this->redis = $redis ;  
  49.         } else {  
  50.             trigger_error( '无法正常连接缓存服务器!' , E_USER_ERROR );  
  51.         }  
  52.         $sess_name = $this->GetSessionName();  
  53.         #取得session ID  
  54.         if ( isset( $_COOKIE$sess_name ] ) && !empty$_COOKIE$sess_name ] ) )  
  55.         {  
  56.             $this->sess_id = $_COOKIE$sess_name ] ;  
  57.             ##如果已经有session ID则取出其中的值  
  58.             $this->sessions = (array)json_decode( $this->redis->get( $this->sess_id ) );  
  59.         } else {  
  60.             $this->sess_id = $this->GetSessionID() ;  
  61.             ##如果没有cookie则建立cookie  
  62.             setcookie( $sess_name , $this->sess_id );  
  63.         }  
  64.         return $this;  
  65.     }  
  66.     /** 
  67.      * @todo 取得session name 
  68.      */  
  69.     public function GetSessionName ()  
  70.     {  
  71.         //sessionname 的名称用客户端的IP加上浏览器的信息  
  72.         $name = $_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT'];  
  73.         return hash ( 'crc32' , $name );  
  74.     }  
  75.   
  76.     /** 
  77.      * @todo 取得sessionID 
  78.      * @return string 返回sessionID 
  79.      */  
  80.     public function GetSessionID( )  
  81.     {  
  82.         if ( $this->sess_id == null )  
  83.         {  
  84.             $id = time().$_SERVER['HTTP_USER_AGENT'];  
  85.             $this->sess_id = hash( 'md5' , $id  );  
  86.         }  
  87.         return $this->sess_id;  
  88.     }  
  89.     /** 
  90.      * @todo 设置session 值 
  91.      * @desc 每次设置的值不会马上写入缓存,不过会记录在内存中,所以写入的值在当次也会有效 
  92.      * @param string $name 相当于$_SESSION[$name] 这中间的变量 
  93.      * @param any $value Session的值 
  94.      */  
  95.     public function Set ( $name , $value )  
  96.     {  
  97.         $this->sessions[ $name ] = $value;  
  98.         $this->changed = true ;  
  99.     }  
  100.   
  101.     public function __call( $name , $param )  
  102.     {  
  103.         trigger_error( sprintf( '您调用了不存的session方法%s!' , $name ) , E_USER_ERROR );  
  104.     }  
  105.   
  106.     public function info()  
  107.     {  
  108.         return $this->redis->info();  
  109.     }  
  110.     /** 
  111.      * @todo 取得session中所有的字段 
  112.      * @desc 私有方法,不供外部使用 
  113.      * @return array session中的值,如果空session则为空数组 
  114.      */  
  115.     protected function GetAll(  )  
  116.     {  
  117.         return count$this->sessions ) > 0 ? $this->sessions : json_decode( $this->redis->get( $this->sess_id ) );  
  118.     }  
  119.     /** 
  120.      * @todo 取得session中的值 
  121.      * @desc 如果$name 为空,则返回全部session,如果不为空则返回对应key的值,如果key不存在,则返回空 
  122.      * @param string $name session中的key 
  123.      * @return array or string Session的值 
  124.      */  
  125.     public function Get( $name = '' )  
  126.     {  
  127.         if ( empty$name ) )  
  128.             return $this->sessions;  
  129.         if ( isset( $this->sessions[ $name ] ) )  
  130.             return $this->sessions[ $name ];  
  131.         return null ;  
  132.     }  
  133.     /** 
  134.      * @todo 删除session中的值 
  135.      * @param string $name session中的key 
  136.      * @return 无返回值 
  137.      */  
  138.     public function Del( $name = '' )  
  139.     {  
  140.         if ( empty$name ) )  
  141.             $this->sessions = array();  
  142.         if ( isset( $this->sessions[ $name ] ) )  
  143.             unset ( $this->sessions[ $name ] );  
  144.         return false ;  
  145.     }  
  146.     /** 
  147.      * @todo 保存session数据至缓存中 
  148.      * @return 无返回值 
  149.      */  
  150.     public function Save()  
  151.     {  
  152.         if ($this->changed === true)  
  153.         {  
  154.             $this->redis->set( $this->sess_id , json_encode( $this->sessions ) );  
  155.             ##更新过期时间  
  156.             $this->redis->expire( $this->sess_id , $this->sess_life );  
  157.             ##当保存过以后,就设置修改标记为假  
  158.             $this->changed = false;  
  159.         }  
  160.     }  
  161.     protected function Expire()  
  162.     {  
  163.         $this->redis->expire( $this->sess_id , $this->sess_life );  
  164.     }  
  165.     /** 
  166.      * @todo 取得session的生命周期 
  167.      * @desc 如果已过期则返回-1 
  168.      */  
  169.     public function GetExpire()  
  170.     {  
  171.         return $this->redis->ttl( $this->sess_id );  
  172.     }  
  173.     /** 
  174.      * @todo 方法结束时,将session值写入缓存 
  175.      */  
  176.     public function __destruct()  
  177.     {  
  178.         $this->auto_save && $this->Save();  
  179.     }  
  180. }  
[php] view plaincopyprint?
  1. Session::singleton()->Set('name','shenjun');  
  2. echo Session::singleton()->Get() ;  
  3. echo Session::singleton()->Get( 'name' ) ;  
  4. echo Session::singleton()->GetExpire();  

0 0
原创粉丝点击