Servlet data cache and servlet Cache

Source: Internet
Author: User

Servlet data cache and servlet Cache

Caching is a necessary way to improve data access capabilities and reduce server pressure. Today I want to talk about two data caching methods, 1 --> session caches data on a single data access interface page. 2 --> Singleton mode caches public data on the entire servlet page. 3 --> cookie local cache

1. What is cache?

What is cache? In order to give readers a better understanding, I will illustrate it with my own understanding. For example, we are currently working on a mall app, the app homepage needs to load and display many items with a large amount of data. Such data interfaces are most suitable for caching them, because the data provided by such data interfaces is public (everyone uses it and does not load different data according to different users ). Before we add cache, the servlet will access the database and retrieve data from the database whenever the user clicks the corresponding interface call page on the app. However, after a user accesses this interface database, the data will be saved to the cache after the user adds the cache, and the user calls this interface the next time, the servlet will read the stored data from the cache and then feed it back to the user.

Ii. session

Session indicates the point cache, Which is saved in the cache and cannot be accessed on other pages. The data is stored in the server memory. Let's use the code below.

1 // query all current data of the game table. 2 else if (Type. equals ("3") 3 {4 5 6 HttpSession session = request. getSession (); 7 // determine whether the value of allGamesDataList in the session is null. Because the following code sets the expiration time for the session, as long as it expires, servlet 8 // This value is also automatically set to null 9 if (session. getAttribute ("allGamesDataList ")! = Null) {10 11 returnJsonObject = (JSONObject) session. getAttribute ("allGamesDataList"); 12 response. getWriter (). println (returnJsonObject. toString (); 13 return; 14} else {15 16 17 18} 19 20 mysqlParameter = new String [] {"account "}; 23 returndata1_mysqlhepler.exe cuteQueryT ("select _ from _ where __! =? ", MysqlParameter); 24 25 try {26 returnJsonObject = new JSONObject (); 27 returnJsonArray = ResultToJsonTool. resultSetToJsonArry (returnData); 28 returnJsonObject. put ("Rows", returnJsonArray); 31 returnJsonObject. put ("GetType", "0"); 32 returnJsonObject. put ("Success", "1"); 33 // The cache expires for 5 minutes. If someone calls this interface within 5 minutes, the data is directly obtained from the cache, 5 minutes later, you must access the database again to obtain data and save it to the cache for 37 sessions. setMaxInactiveInterval (5*60); 38 session. setAttribute ("allGamesDataList", returnJsonObject); 39 response. getWriter (). println (returnJsonObject. toString (); 40 41} catch (SQLException | JSONException e1) {42 43 44 e1.printStackTrace (); 45} 46 47}

Ii. Singleton Mode

Here we use Singleton as the cache, because the cache saved by the session cannot be accessed across pages, so we thought of singleton. Of course there must be other better methods. Here, I use the singleton to save the user's token. A Random token generated when the user logs on is reported to the user, and the user's login id is the key, store the toke In the servlet singleton. Here, the token is saved to the cache because the token is used in all the data encryption verification pages, if the token is not saved, all encrypted interface requests will be queried in the database once. This will definitely affect the efficiency. In this case, such data must be cached.

Singleton code

 1 package Helper; 2 import org.json.JSONObject; 3 public class ShareSingleton      4 {      5   public     JSONObject UsrTokenDictionary;  6   public   static ShareSingleton instance=null;  7   public static      ShareSingleton getInstance(){      8         if(instance == null){   9             instance = new ShareSingleton();  10               instance.UsrTokenDictionary=new JSONObject();11              return instance;12             13         }else{     14             return instance;     15         }     16     }    17    18   public void VerifyTokenForInterface(String RequestToken)19   {20       21      22   }23 }   

 

Save the token to The Singleton.

1 // use uuid to generate a Unique User token 2 ecryptToKenUUID = UUID. randomUUID (). toString (); 3 mysqlParameter = new String [] {ecryptToKenUUID, LoginId}; 4 if (MySqlHepler.exe cuteUpdate ("update _ set _ =? Where _ =? ", MysqlParameter)> 0) 5 {6 7 UserInfoObject = array. getJSONObject (0); 8 UserInfoObject. put ("encryptToken", ecryptToKenUUID); 9/* AddicationDictionary: The value here is */10 returnJsonObject to be saved to app defaultusr. put ("AddicationDictionary", UserInfoObject); 11 returnJsonObject. put ("GetType", "1"); 12 returnJsonObject. put ("Success", "1"); 13 14 // Save the generated token to 15 ShareSingleton in singleton mode before the servlet returns the operation result. getInstance (). usrTokenDictionary. put (LoginId, ecryptToKenUUID); 16 17 response. getWriter (). println (returnJsonObject. toString (); 18}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.