数据库ticket缓存

//做ticket数据库缓存
function getJsApiTicket(){
//查询数据库是否有数据
//人为规定id1是token,id2是ticket
$res = getone("cachedata",$this->links,"id=2");
$token = $this->getAccessToken();
if($res){
//有存储ticket:判断时间是否过期
if($res['passtime'] > time()){
//ticket没有过期
$ticket = $res['datainfo'];
}else{
//ticket已经过期:重新获取ticket更新ticket
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$token";
$res1 = httpGet($url);
$res = json_decode($res1,true);
$ticket = $res['ticket'];
$passtime = time() + 7000;
$arr['datainfo'] = $ticket;
$arr['passtime'] = $passtime;
update($arr,$this->links,"cachedata","id=2");
}
}else{
//没有存储ticket:获取ticket添加到数据库
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$token";
$res1 = httpGet($url);
$res = json_decode($res1,true);
$ticket = $res['ticket'];
$passtime = time() + 7000;
//将数据添加到数据库
$arr['datainfo'] = $ticket;
$arr['passtime'] = $passtime;
$arr['id'] = 2;
$res = add($arr,$this->links,"cachedata");
// if($res){
// echo "添加成功";
// }else{
// echo "添加失败";
// }
}
return $ticket;
}

你可能感兴趣的:(数据库ticket缓存)