本站使用WordPress 博客系统,腾讯云CDN+云解析 ,为追求速度,全站静态缓存。
但在发布和更新文章、提交及审核评论后,CDN缓存导致首页不能及时显示更新,在张戈博客发现了这段“自动清理腾讯云CDN缓存”的代码,马上用上——网络高手们确实多啊。
完整的 php 代码如下:
$secretKey = '此处填写SECRET_KEY';
$secretId = '此处填写SECRET_ID';
add_action('publish_post', 'Clean_By_Publish', 0);
add_action('comment_post', 'Clean_By_Comments',0);
add_action('comment_unapproved_to_approved', 'Clean_By_Approved',0);
function Clean_By_Publish($post_ID){
global $secretKey,$secretId;
$url = get_permalink($post_ID);
$action='RefreshCdnUrl';
$PRIVATE_PARAMS = array(
'urls.0' => home_url(),
'urls.1' => $url ,
);
$HttpUrl="cdn.api.qcloud.com";
$HttpMethod="POST";
$isHttps =true;
$COMMON_PARAMS = array(
'Nonce' => rand(),
'Timestamp' =>time(NULL),
'Action' =>$action,
'SecretId' => $secretId,
);
CreateRequest($HttpUrl,$HttpMethod,$COMMON_PARAMS,$secretKey, $PRIVATE_PARAMS, $isHttps);
}
function Clean_By_Comments($comment_id)
{
global $secretKey,$secretId;
$comment = get_comment($comment_id);
$url = get_permalink($comment->comment_post_ID);
$action='RefreshCdnUrl';
$PRIVATE_PARAMS = array(
'urls.0' => $url,
);
$HttpUrl="cdn.api.qcloud.com";
$HttpMethod="POST";
$isHttps =true;
$COMMON_PARAMS = array(
'Nonce' => rand(),
'Timestamp' =>time(NULL),
'Action' =>$action,
'SecretId' => $secretId,
);
CreateRequest($HttpUrl,$HttpMethod,$COMMON_PARAMS,$secretKey, $PRIVATE_PARAMS, $isHttps);
}
function Clean_By_Approved($comment)
{
global $secretKey,$secretId;
$url = get_permalink($comment->comment_post_ID);
$action='RefreshCdnUrl';
$PRIVATE_PARAMS = array(
'urls.0' => $url,
);
$HttpUrl="cdn.api.qcloud.com";
$HttpMethod="POST";
$isHttps =true;
$COMMON_PARAMS = array(
'Nonce' => rand(),
'Timestamp' =>time(NULL),
'Action' =>$action,
'SecretId' => $secretId,
);
CreateRequest($HttpUrl,$HttpMethod,$COMMON_PARAMS,$secretKey, $PRIVATE_PARAMS, $isHttps);
}
function CreateRequest($HttpUrl,$HttpMethod,$COMMON_PARAMS,$secretKey, $PRIVATE_PARAMS, $isHttps)
{
$FullHttpUrl = $HttpUrl."/v2/index.php";
$ReqParaArray = array_merge($COMMON_PARAMS, $PRIVATE_PARAMS);
ksort($ReqParaArray);
$SigTxt = $HttpMethod.$FullHttpUrl."?";
$isFirst = true;
foreach ($ReqParaArray as $key => $value)
{
if (!$isFirst)
{
$SigTxt = $SigTxt."&";
}
$isFirst= false;
if(strpos($key, '_'))
{
$key = str_replace('_', '.', $key);
}
$SigTxt=$SigTxt.$key."=".$value;
}
$Signature = base64_encode(hash_hmac('sha1', $SigTxt, $secretKey, true));
$Req = "Signature=".urlencode($Signature);
foreach ($ReqParaArray as $key => $value)
{
$Req=$Req."&".$key."=".urlencode($value);
}
if($HttpMethod === 'GET')
{
if($isHttps === true)
{
$Req="https://".$FullHttpUrl."?".$Req;
}
else
{
$Req="http://".$FullHttpUrl."?".$Req;
}
$Rsp = file_get_contents($Req);
}
else
{
if($isHttps === true)
{
$Rsp= SendPost("https://".$FullHttpUrl,$Req,$isHttps);
}
else
{
$Rsp= SendPost("http://".$FullHttpUrl,$Req,$isHttps);
}
}
return json_decode($Rsp,true);
}
function SendPost($FullHttpUrl, $Req, $isHttps)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $Req);
curl_setopt($ch, CURLOPT_URL, $FullHttpUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1 );
if ($isHttps === true) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
$result = curl_exec($ch);
return $result;
先访问 腾讯云的 API 密钥 创建或获取你在腾讯云的 API 密钥:
然后正确替换上述代码中的8,9行的 secretKey 和 secretId 值,比如:
/* 需要填写你的密钥,访问 https://console.qcloud.com/capi 获取 SecretId 及 $secretKey */
$secretKey = 'ABCD123CSDFSBSDFDSF';
$secretId = 'CDAABBBSDFA683N23423N4';
部署后,博客发布或更新文章、评论的提交或审核都会调用 API 去清理 CDN 缓存,其中文章和评论的提交可能会比没有部署略微卡一点,评论的审核是异步提交,所以感知不到什么。