数据库token缓存

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容