A trap for PHP cache application

Source: Internet
Author: User
A trap used by the PHP cache

Look at the code first:

/** * Get settings information */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 of using the Tair memory cache, in which the cache is set and the cache time is 3,600 seconds. The data is obtained from the API, what if the writing is problematic? If:

$result = $taomanyiApiService->getcoinsetting ();


$result get the data is empty, because the $result data is from the HTTP request, the data is not normal is more common things. In this situation, the HTTP request fails, then the interface data is not requested, and the next process is to set the cache

$cache->set ($ckey, Json_encode ($result), 3600);


We will find that because of the failure of an interface HTTP request, we accidentally cache the empty data for 3,600 seconds. This will appear on the page, such as the classification of data gaps, affecting the entire business process

We do the following optimizations:


  • Related Article

    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.