加緩存注意的坑

通過參數作爲key值的坑

    /**
           通過用戶id、城市編碼、經緯度  來獲取列表
           獲取規則:
               1、如果有經緯度,通過經緯度獲取列表
               2、沒有經緯度,如果有城市編碼,通過城市編碼獲取列表
               3、如果都沒有,通過uid獲取列表
    */
    function getIds($uid, $city_code=0, $lon=0, $lat=0, $is_cache = false,   $page = 1, $count = -1  ){
            if( empty($uid) || !is_numeric($uid) || $uid == 0 ) { 
            Debug::setErrorMessage("uid不能爲空");
            return false;
        }  
        if ( $city_code === 0 ) {
            if( intval($lon) === 0 && intval($lat) === 0 )  {
                $info = getInfoByUid($uid);
            } else {
                $info = getInfoByLatLon($lat,$lon);
            }
            $city_code = $info['city_code'];
        }
        if($city_code === 0 ) {
            $city_code = 110100;
        }

        $ids = array();

        if( empty($city_code) || $city_code == 0 ) { 
            $city_code =  110100;
        } 

        $args = func_get_args();
        array_shift($args);
        $key = md5( serialize($args));
        //$key = md5( $city_code.serialize($args));
        $key = "xxxx".$key;

        if( true === $is_cache ) {
            $result = ControllCache::getCache($key);
            if ( false !== $result ) {
                return $result;
            }
        }

        $ids = getIdsByCityCode($city_code);
        ControllCache::setCache($ids, 60, $key);
        return $ids;
    }
  • 這個bug最不容易發現的地方在:當用戶只傳uid的時候,不同的用戶通過uid獲取的城市列表會不一樣,但是除了uid之後,傳的參數一樣,結果會導致當前用戶中的緩存時,是別的uid存進去的數據,導致獲取城市列表不正確。

測試與線上的坑

  • 測試機上測試時,不能將數據放入緩存,這樣會影響線上獲取線上的數據
  • 做版本控制時,需要將版本控制也融入到key值進行緩存,否則會影響線上服務
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章