2013-03-06 64 views
0

我這是關係到邏輯不是技術問題,這裏是一個場景,(我用Spring + Hibernate的)如何數據字段從數據庫Spring MVC中存儲

我需要閱讀一些數據從數據庫返回到每個獲取請求的頁面,但我想這裏有一些破解,如果使用某個腳本某人非常頻繁地重新加載頁面,這會導致多次調用服務器,因爲我認爲讀取數據並將它們放入全局變量或類變量,通過這樣做我最終編寫了很奇怪的代碼許多全局變量和愚蠢的方式來給它們初始值,就像變量用戶狀態這是一個字節型變量我已經給-2作爲初始值,所以我的內部邏輯可以理解爲數據庫中的這個變量沒有設置值,下面是我的代碼

@Controller 
    /* @Secured("hasRole('ROLE_USERS')") */ 
    @RequestMapping("member") 
    public class ApplyRoles { 

     @Autowired 
     private UserInformationForAccessApplication checkUserStatus; 

     // we will initialize variables to avoid auto-initialize by constructor 
     private byte userStatus = Constant.IntializationOfGlobalVariable.GLOBALINIT, 
       requesttype = Constant.IntializationOfGlobalVariable.GLOBALINIT, 
       access = Constant.IntializationOfGlobalVariable.GLOBALINIT; 

     Map<String, Object> accessnrole; 
     Map<String, String> country; 
     Map<String, String> roleArray; 

     @Autowired 
     StudentEnrollmentApplication enrollmentApplication; 

     @Autowired 
     SystemProperties systemProperties; 

     @Autowired 
     EmployeeEnrollmentApplicationResume employeeEnrollmentApplicationResume; 

     @Autowired 
     AccessEnrollmentProcessing accessEnrollmentProcessing; 

     private String role = Constant.IntializationOfGlobalVariable.ROLENOTSET, 
       fname, lname; 

     @RequestMapping(value = "/user", method = RequestMethod.GET) 
     public String checkingUserStatus(Model model, HttpSession session, 
       Authentication authentication) { 

      String sessionemail = "[email protected]";// (String) session 
      // .getAttribute(Constant.SessionAttributes.LOGGEDINUSER); 

      // first check global value, if found set than don't fetch from database 
      if (userStatus == Constant.IntializationOfGlobalVariable.GLOBALINIT) { 
       // get user status from MySQL Database 
       userStatus = checkUserStatus.checkStatus(sessionemail).get(0); 
       if (!(userStatus == Constant.UserRoleApplicationStatus.NOTAPPLIED)) { 
        access = checkUserStatus.checkStatus(sessionemail).get(1); 
        model.addAttribute(Constant.SystemName.ACCESS, access); 
       } 
      } 

      if (!(userStatus >= Constant.UserRoleApplicationStatus.NOTAPPLIED || userStatus <= Constant.UserRoleApplicationStatus.REJECTED)) { 
       model.addAttribute("error", "User status is not avaible"); 
       return "redirect:error/pagenotfound"; 
      } else if (userStatus == Constant.UserRoleApplicationStatus.NOTAPPLIED) { 
       if (requesttype == Constant.IntializationOfGlobalVariable.GLOBALINIT) { 
        // get request type from MongoDB database 
        requesttype = checkUserStatus.getRequestType(sessionemail); 
       } 

       if (!(requesttype == Constant.RequestType.NORMALEBIT || requesttype == Constant.RequestType.INVITEBIT)) { 
        model.addAttribute("error", 
          "Facing Technichal Issue, Please try again"); 
        return "redirect:error/pagenotfound"; 
       } 

       if (requesttype == Constant.RequestType.INVITEBIT) { 
        if (!(Byte.parseByte((String) accessnrole 
          .get(Constant.SystemName.ACCESS)) == Constant.Access.USERBIT)) { 
         accessnrole = checkUserStatus 
           .getAccessAndRole(sessionemail); 
        } 

        if (accessnrole.get(Constant.SystemName.ACCESS).equals(
          Constant.Database.ERRORMESSAGE) 
          || accessnrole.get(Constant.SystemName.ROLE).equals(
            Constant.Database.ERRORMESSAGE)) { 

         model.addAttribute("error", 
           "Facing Technichal Issue, Please try again"); 
         return "redirect:error/pagenotfound"; 
        } 

        model.addAttribute(Constant.SystemName.ACCESSNROLE, accessnrole); 
        model.addAttribute(Constant.SystemName.REQUESTTYPE, requesttype); 
       } 
      } 

      model.addAttribute(Constant.SystemName.USERSTATUS, userStatus); 


      return "member/user"; 

     } 
} 

避免全局變量我想起訴的餅乾,因爲我不想讓調用數據庫在同一會話的每個頁面重新加載,一旦其加載會話做的比我不用調用數據庫做。

任何可以幫助重新設計的代碼上面的部分是非常讚賞

感謝

+0

這完全是錯誤的級別,試圖抵禦DDOS攻擊。如果你的java應用程序服務器暴露在公共互聯網上,它應該在apache後面被代理,並且apache應該防禦它。 – Affe 2013-03-06 20:42:14

+0

@Affe我同意你在Apache(Web服務器)上應該處理這種廢話,但這也與性能有關,比方說,用戶每2分鐘重新加載一次頁面,我認爲Apache應該不會有這種行爲的任何問題。我正在通過ehcahe採取什麼措施,使用ehcahe緩存結果,一旦寫入就通過緩存提供服務。是緩存機制像Ecache是​​會話相關 – Yashprit 2013-03-06 20:54:53

回答

0

你在想什麼被稱爲「緩存」。這是一種標準的計算機科學方法,他們一直在研究如何在有計算機的情況下使用高速緩存。

你可能想去做一些關於這個主題的閱讀。我發現這個由谷歌搜索「緩存教程java」http://javalandscape.blogspot.com/2009/01/cachingcaching-algorithms-and-caching.html

最簡單的說法(一個項目緩存),你想要的是存儲一些數據對象,你最近花了一些時間想出來。但是你也必須有某種標識符,這樣你才能知道下一個請求是否要求相同的數據。如果不是,你必須完成所有的工作。如果它是相同的數據,您只需再次返回。

所以算法工作在這個簡單的例子是這樣的:

if (storedData != null && storedRequestInfo == userRequest.requestInfo) { 
    return storedData; 
} 

storedData = youCalculateTheRequestedData(); 
storedRequestInfo = userRequest.requestInfo; 

return storedData; 

它沒有任何真正的編程語言,只是要告訴你它是如何工作的。

requestInfo就是用來查找數據庫內容的請求。任何計算後,您都可以將它保存在storedRequestInfo中。

這表明它將一些數據返回給用戶,這就是storedData中的內容。

這是一個簡單的單元素緩存。 (爲了擴展這個,你可以在會話中存儲storedRequestInfo和storedData,並且你最終爲每個用戶存儲一個這樣的數據,你也可以使用一個Java Map並存儲一堆storedData。問題是決定如何限制你的記憶使用如果你爲每個用戶保存了太多的記憶,你會佔用太多的記憶,所以你要限制每個用戶可以按大小或數量來分配,然後你必須決定哪一個在簡單的情況下,你總是刪除存儲的內容並存儲新的內容

我注意到你的評論ECache只是一個很大的花哨地圖我用過的術語我不知道它是否自然會依賴於會話,但是可以通過將會話ID添加到緩存鍵來實現。)

+0

謝謝,剛開始這個問題後,我來了解一下Ehcache,有沒有其他工具可用於java中的緩存依賴於會話或從我指的任何博客/ URL。在java的最後一天配置結束時要做的事情。 – Yashprit 2013-03-06 21:04:26

+0

您認爲您需要爲任何單個用戶存儲多少個同一事物的不同實例?如果它通常只有一個,那麼在會議中只有一個'最後一個'。在這種情況下,真正的緩存是矯枉過正的。 – 2013-03-06 21:06:45

1

實際上有兩件事情,你正在考慮,正確我,如果我錯了,但:

  1. 緩存服務器(在Java應用程序)來避免做數據庫查詢多次爲相同的數據。
  2. 避免客戶端(瀏覽器)向服務器發送多個請求。

第一個可以使用緩存來解決,它可以在春天使用任何給定的方法使用註釋。該文檔可用here

第二個有點棘手,我暫時離開它,除非你發現性能問題。在Spring中再次可以做到這一點,並利用HTTP頭中可用的HTTP協議和緩存控制來通知瀏覽器緩存響應的時間。

+0

你讓我正確的一點是我現在想的。但有興趣瞭解更多關於第2點的信息,我沒有得到它,請你分享一下第2點的URL。 – Yashprit 2013-03-06 21:00:38

+1

看到這個http://www.i-develop.be/blog/2010/07/30 /彈簧MVC-3-0-HTTP-緩存控制報頭/ – ramsinb 2013-03-06 21:09:23

相關問題