如何强制清除或无效Azure CDN内容?

时间:2012-01-06 07:16:12

标签: cdn azure-storage azure-storage-blobs microsoft-cdn

由于性能原因,我的内容很少会更改我想要通过Azure CDN提供的内容。但是,当内容确实发生变化时,更新数据立即可用非常重要。理想情况下,我可以设置一个长TTL但是当我更新它时主动告诉CDN使内容过期。我怎么能做到这一点?现在没有缓存失效或清除API,我宁愿不设置短TTL。

5 个答案:

答案 0 :(得分:10)

2015年12月,Azure团队添加了通过REST API(https://msdn.microsoft.com/en-us/library/mt634451.aspx)刷新或清除CDN的功能。 开始时,此功能仅适用于使用新Azure门户(http://portal.azure.com)创建的端点,但是,使用旧管理表面创建的CDN将在2016年初迁移(https://feedback.azure.com/forums/169397-cdn/suggestions/556307-ability-to-force-the-cdn-to-refresh-any-cached-con)。

答案 1 :(得分:8)

没有API可以使Azure CDN无效。

解决方法:

  1. 在Azure门户中的CDN上启用“查询字符串状态”。然后你可以附加一个新的查询字符串名称和随机值,例如。 /images/background.png?v=1234

  2. 使用时间戳或随机值上传并重命名新文件。例如:/ images / background.20140917225200.png

  3. 设置较短的缓存标头并等待它过期。以下是Azure团队http://msdn.microsoft.com/en-us/library/azure/gg680306.aspx

  4. 中的一篇文章

答案 2 :(得分:0)

您无法强行清除CDN。

最佳做法是将版本/日期信息附加到您的文件名,并设计您的应用以动态获取当前文件名。

以产品照片为例,将版本附加到blob名称,将blob名称存储在表格中,然后提供表格中名称的链接,而不是对文件名进行硬编码。

通过这种方式,您可以在缓存标头上设置max-expiry,而Azure只会在需要时清理过时的内容。

答案 3 :(得分:0)

您还可以通过PowerShell清除CDN内容:

$AzureCdnResourceGroupName = "--RESOURCE GROUP--"
$AzureCdnEndpoint = "--ENDPOINT NAME--"
$AzureCdnProfileName = "--CDN PROFILE NAME--"

#In case there's multiple subscriptions on the account
Set-AzureRmContext -SubscriptionId $AzureCdnSubscriptionId
$AzureCdnSubscriptionId = "--SUBSCRIPTION GUID--"

Write-Host "Purging CDN $AzureCdnProfileName/$AzureCdnEndpoint"

Invoke-AzureRmResourceAction `
    -ResourceGroupName $AzureCdnResourceGroupName `
    -ResourceType 'Microsoft.Cdn/profiles/endpoints' `
    -ResourceName $AzureCdnProfileName/$AzureCdnEndpoint `
    -ApiVersion '2015-06-01' `
    -Action 'Purge' `
    -Parameters @{ ContentPaths = @('/static/js/*','/static/css/*') } `
    -Force

Write-Host 'Purging completed'

答案 4 :(得分:0)

您可以使用az cli清除CDN端点

az cdn endpoint purge是根命令

一个例子

az cdn endpoint purge -g group -n endpoint \
--profile-name profile-name \
--content-paths '/scripts/app.js' '/styles/*'

您可以找到有关cli命令here

的更多信息。