Single case design mode global Cache Accesstoken

Source: Internet
Author: User

Using the JS-SDK development of small partners, read the document often see this sentence: (Below is a part of the development document, the exact words)

The phrase is: The developer must be in their own service global cache Access_token,jsapi_ticket

The following is a global cache using singleton mode. (Of course you can also deposit the database, which is called instantiation.) Then every time from the database, and then get a timer refresh, the landlord used this way, but found that it is a waste of time, wasting energy, not recommended. This is a good way to give you the following. )

Singleton mode: As the name implies, there is only one instance of a class in the system. Regardless of how many classes are calling it, it is a. This allows the data to be shared when each class is called.

/** * @authorXulei * @description single case design mode cache Accesstoken Jsapi_ticket*/ Public classSingleton {//The cache Accesstoken Map,map contains a accesstoken and a cached timestamp    Privatemap<string, string> map =NewHashmap<string,string>(); PrivateSingleton () {}Private StaticSingleton single =NULL; //Static Factory Method     Public StaticSingleton getinstance () {if(single =NULL) { single=NewSingleton (); }        returnSingle ; }     PublicMap<string, string>Getmap () {returnmap; }     Public voidSetmap (map<string, string>map) {         This. Map =map; }     Public StaticSingleton Getsingle () {returnSingle ; }     Public Static voidSetSingle (Singleton single) {Singleton.single=Single ; }        /*** Get Accesstoken Jsapi_ticket has been added to the cache mechanism *@paramAppID *@paramAppsecret *@return     */     PublicMap<string,object>Getaccesstokenandjsapiticket (String AppID, String appsecret) {Map<String,Object> result =NewHashmap<string,object>(); Singleton Singleton=singleton.getinstance (); Map<string, string> map =Singleton.getmap (); String Time= Map.get ("Time");//take the data from the cacheString Accesstoken = Map.get ("Access_token");//take the data from the cacheString Jsapiticket = Map.get ("Jsapiticket");//take the data from the cacheLong nowdate =NewDate (). GetTime (); //set the expiration time here 3000*1000 just fine.        if(Accesstoken! =NULL&& Time! =NULL&& Nowdate-long.parselong (Time) < 3000 * 1000) {System.out.println ("-----read Access_token from cache:" +Accesstoken); //take data from the cache to assign a value to the returned resultResult.put ("Access_token", Accesstoken); Result.put ("Jsapiticket", Jsapiticket); } Else{Map<String,Object> info =Jsdkutil.getjsapiticket (AppID, Appsecret);//actually here you have to call the interface to get Accesstoken and Jsapiticket.//placing information in the cacheMap.put ("Time", Nowdate + "" "); Map.put ("Access_token", String.valueof (Info.get ("Access_token"))); Map.put ("Jsapiticket", String.valueof (Info.get ("Jsapiticket"))); //assign a value to the returned resultResult.put ("Access_token", Info.get ("Access_token")); Result.put ("Jsapiticket", Info.get ("Jsapiticket")); }        returnresult; }}


Then in the place used directly: Singleton.getinstance (). Getaccesstokenandjsapiticket (Appid,appsecret);

Single case design mode global Cache Accesstoken

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.