zoukankan      html  css  js  c++  java
  • 记录一段QQ关于 UNIGUI 的Session 时间设定

    记录一段QQ关于 UNIGUI 的Session 时间设定,来自[台中]cmj(31365722):

     [重點說明]
     1.UniGUI的Session就是UniMainModule。
       2.SessionTimeOut預設600000 即600秒,其代表 Session在完全沒有動作下只能存
    活600秒,所謂動作是鍵盤、滑鼠、事件觸發,。
       3.SessionTimeOut 600秒,若Session在600秒死了,系統也要等該Session滿600
         秒後才會回收,可能造成無用的Session占用系統資源一段時間。
       4.SessionTimeout設為5秒鐘,Session 5秒鐘就快速回收,不會有無用Session大
    量存在問題,但存活時間只有5秒鐘。
       5.以上兩點都有問題,解決方式是加一個次數,即5秒x120次數=600秒,再利用
         Session展延機制,這樣Session可以存活600秒,沒用的Session也在5秒鐘
    快速回收。
    [UNIGui Session回收流程]
       SessionTimeout時間到→TUniMainModule.UniGUIMainModuleSessionTimeout事件→是否展延?
        否: TUniMainModule.UniGUIMainModuleDestroy,回收.
        是: 繼續存活一個SessionTimeout時間 
     
    -----------------------------
     [實作]
     [UniServerModule]
     屬性: SessionTimeout: Session存在時間(完全沒有動作之下),不可改變.
                           只能在UniGUIServerModuleBeforeInit事件設定

     加入變數
     public
           SessionTimeCount:Integer; //SessionTimeout 存活次數
        LastSessionCreateTime:TDateTime; //最後Session建立時間

    procedure TUniServerModule.UniGUIServerModuleBeforeInit(Sender: 
    TObject);
    begin
      Self.SessionTimeout:=5*1000; //存活一次時間5秒
      Self.SessionTimeCount:=60;   //60次,總存活時間=5x60=300秒
       Self.LastSessionCreateTime:=now; //第一個Session建立時間
    end;

    [UniMainModule]
     屬性: TerminateOnBrowserClose 一定設為True;
     加入變數
     public
        SessionTimeCount: Integer; //Session存活次數,一次為一SessionTimeOut
        IsALive:Boolean;           //Session是否活著
        
     procedure TUniMainModule.UniGUIMainModuleBrowserClose(Sender: TObject);
    begin
      Self.SessionTimeCount:=0; //Session展延次數取消,準備回收
    end;

     procedure TUniMainModule.UniGUIMainModuleCreate(Sender: TObject);
    var dt:TDateTime; //Session建立時間
              DSec:integer; //Session與前一個Session建立的時間差
          begin
       dt:=Now;
    DSec:=SecondsBetween(Dt , UniServerModule.LastSessionCreateTime); 

    //兩個Session建立間隔需大於2秒,以防止2秒內大量建Session
      if (DSec<=2) then UniSession.Terminate();

      //記錄最後一個Session建立時間
       UniServerModule.LastSessionCreateTime:=Dt; 

          Self.SessionTimeCount:=UniServerModule.SessionTimeCount; //存活次數
            if UniServerModule.SessionManager.Sessions.SessionList.Count>500 then 
    //最多500 Session建立
             begin
               //UniServerModule.MyLog.Add('Session數>500');
               UniSession.Terminate();
             end;
          end;

    procedure TUniMainModule.UniGUIMainModuleHandleRequest(ASession:
     TObject;var Handled: Boolean);
     var  Session:TUniGUISession;
          begin
            Self.SessionTimeCount:=UniServerModule.SessionTimeCount; 
    //Keyboard,mouse,or其他動作重設SessionTimeout次數
     end;

     procedure TUniMainModule.UniGUIMainModuleSessionTimeout(ASession: 
    TObject;  var ExtendTimeOut: Integer);
     var Session:TUniGUISession;
     begin
    //Session:= TUniGUISession(ASession);
    //Session已死,未曾執行UniLoginFormAfterShow或UniFormCreate事件
    if not Self.IsALive then Self.SessionTimeCount:=0;

        //存活次數大於0時就展延
        if (Self.SessionTimeCount>0) then 
             begin
               ExtendTimeOut:=UniServerModule.SessionTimeout
         end
        else
            UniSession.Terminate(); //立即回收Session

          //展延次數減一次
        Self.SessionTimeCount:=Self.SessionTimeCount-1;
      end;

         [LoginForm]
          若沒有LoginForm改為MainForm的AfterShow事件
     procedure TLoginForm.UniLoginFormAfterShow(Sender: TObject);
    begin
           //在瀏覽器連續F5或重新整理5次,則產生5個Session,前4個Session是死
    //的不會執行本事件
        TUniMainModule(UniSession.UniMainModule).IsALive:=True; 
      end;

    [MainForm]
     顯示所有Session
     拖入三件控件,一個TuniListBox Name=LbSession,
          一個TuniButton  Name=UniButton3 Caption=Session List
          一個TuniButton  Name=UniButton2 Caption=Clear

          procedure TMainForm.UniFormAfterShow(Sender: TObject);
     begin
            //在瀏覽器連續F5 5次,則產生5個Session,前4個Session是死的,不會執行
    //本事件
       TUniMainModule(UniSession.UniMainModule).IsALive:=True;
     end;

     procedure TMainForm.UniButton3Click(Sender: TObject);
     var UserList:TList; //登入Session List
         i:integer;
          Session:TUniGUISession;
          m:TUniMainModule;
          begin
            UserList:=UniServerModule.SessionManager.Sessions.SessionList;
       for i:=0 to UserList.Count-1 do
        begin
          Session:=TUniGUISession(Userlist[i]);
         m:=TUniMainModule(Session.UniMainModule);
            Self.LbSession.Items.Add(inttostr(i)+': '+
                 Session.SessionId +' , '+
                 inttostr(m.SessionTimeCount)+',Is Alive='+
    GetEnumName( TypeInfo(Boolean) , Ord(m.IsALive))
                 );
        end;
    Self.LbSession.Items.Add('--------------------------------------');
     end;

     procedure TMainForm.UniButton2Click(Sender: TObject);
          begin
            Self.LbSession.Clear;
     end;

    [測試]
     開兩個瀏覽器
     瀏覽器1 監看Session
     瀏覽器2 登入後快速重整多次,以模擬大量產生Session
    ================
     原本是session預設一次600秒,session死了要600秒才剔除
    改為
    5秒x120次,Session死了,5秒就剔除
    也可以改
    3秒x200次,
     
  • 相关阅读:
    [ USACO 2018 OPEN ] Out of Sorts (Platinum)
    [ USACO 2018 OPEN ] Out of Sorts (Gold)
    [ USACO 2018 OPEN ] Out of Sorts (Silver)
    [ BZOJ 4236 ] JOIOJI
    [ HAOI 2012 ] 容易题
    [ HAOI 2008 ] 玩具取名
    「BZOJ 4502」串
    Codeforces 493 E.Devu and Birthday Celebration
    「TJOI 2018」教科书般的亵渎
    「TJOI 2018」游园会 Party
  • 原文地址:https://www.cnblogs.com/hopesun/p/9249693.html
Copyright © 2011-2022 走看看