First look at a piece of code:
[Php]
/**
* Get settings
*/
Public function getCoinSetting (){
$ Cache = Common: getTair ();
$ Ckey = Common: hashKey ("Hello ");
$ Ret = $ cache-> get ($ ckey );
If ($ ret) return json_decode ($ ret, true );
$ TaomanyiApiService = $ this-> _ getTmiApiService ();
$ Result = $ taomanyiApiService-> getCoinSetting ();
$ Cache-> set ($ ckey, json_encode ($ result), 3600 );
Return $ result;
}
This is an instance that uses Tair memory cache. In this Code, cache is set to 3600 seconds. The data is obtained from the Api. What will happen if the data is written like this? Suppose:
[Php]
$ Result = $ taomanyiApiService-> getCoinSetting ();
The data obtained by $ result is null, because $ result data is obtained from an HTTP request, and abnormal data is also a common issue. In this case, the HTTP request fails, and the interface data cannot be requested. The next process is to set the cache
[Php]
$ Cache-> set ($ ckey, json_encode ($ result), 3600 );
We will find that due to an HTTP request failure, we accidentally cache the empty data for 3600 seconds. In this way, the page appears. For example, the blank data in the category affects the entire business process.
We have made the following optimizations:
[Php]
If ($ result) $ cache-> set ($ ckey, json_encode ($ result), 3600 );
Author: initphp