通過參數作爲key值的坑
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 = "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值進行緩存,否則會影響線上服務