加缓存注意的坑

来源:互联网 发布:风炎汉服社 知乎 编辑:程序博客网 时间:2024/06/05 16:21

通过参数作为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值进行缓存,否则会影响线上服务
0 0
原创粉丝点击