今天更改了一個線上的css文件,由于項目部署了cdn,需要對緩存進行清理。有兩種方式;
一、清除cdn緩存
二、更改頁面引入文件地址,如加入版本號
第一種方式需要登錄阿里云,然后進行緩存清理操作,比較麻煩;
第二種方式如果是涉及的頁面數量過多,在更改起來也是非常麻煩的。
我們使用的阿里云的cdn產品,記得以往的產品都有api接口,想通過阿里云cdn接口模式對鏈接進行緩存清除操作。
下面是php實例:
一、安裝SDK
使用composer進行安裝,命令:
composer require alibabacloud/client
二、獲取阿里云參數
1、accessKeyId
2、accessSecret
建議使用子密鑰
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/10/29 0029
* Time: 10:10
*/
namespace Appindexcontroller;
use AlibabaCloudClientAlibabaCloud;
use AlibabaCloudClientExceptionClientException;
use AlibabaCloudClientExceptionServerException;
use thinkController;
use thinkRequest;
class Cdn extends Controller
{
protected $accessKeyId;
protected $accessSecret;
public function __construct(Request $request = null)
{
parent::__construct($request);
$this->accessKeyId = '123456789';
$this->accessSecret = '123456789';
}
public function index()
{
$url = 'http://www.demo.com/css/index.css';
AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessSecret)
->regionId('cn-hangzhou')
->asDefaultClient();
try {
$result = AlibabaCloud::rpc()
->product('Cdn')
// ->scheme('https') // https | http
->version('2018-05-10')
->action('RefreshObjectCaches')
->method('POST')
->host('cdn.aliyuncs.com')
->options([
'query' => [
'RegionId' => "cn-hangzhou",
'ObjectPath' => $url,
],
])
->request();
$res = $result->toArray();
if(isset($res['RefreshTaskId'])) {
echo '刷新成功';
} else {
echo '刷新失敗';
}
}
catch(ClientException $e) {
echo $e->getErrorMessage() . PHP_EOL;
}
catch(ServerException $e) {
echo $e->getErrorMessage() . PHP_EOL;
}
}
}
建議在提交刷新后,2分鐘后查看效果,注意要清除瀏覽器緩存哦!






