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

发布于 2024-12-24 22:55:56 字数 146 浏览 2 评论 0原文

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

I have content that rarely changes that I want to serve over the Azure CDN for performance reasons. When the content does change, though, it's important that the updated data is immediately available. Ideally, I'd be able to set a long TTL but then proactively tell the CDN to expire content when I update it. How can I accomplish this? There is no cache invalidation or purge API right now, and I would rather not set a short TTL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

祁梦 2024-12-31 22:55:56

2015 年 12 月,Azure 团队添加了通过 REST API 刷新或清除 CDN 的功能 (https://msdn .microsoft.com/en-us/library/mt634451.aspx)。
一开始,此功能仅适用于使用新的 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)。

In 2015 December the Azure team added the ability to refresh or purge the CDN via REST API (https://msdn.microsoft.com/en-us/library/mt634451.aspx).
In the beginning, this function is only available for endpoints created with the new Azure Portal (http://portal.azure.com), however, CDNs created with the old management surface will be migrated in the beginning of 2016 (https://feedback.azure.com/forums/169397-cdn/suggestions/556307-ability-to-force-the-cdn-to-refresh-any-cached-con).

绝不服输 2024-12-31 22:55:56

没有 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

There is NO API to invalidate an Azure CDN.

Workaround:

  1. Enable "Query String Status" on your CDN in Azure portal. Then you can append a new query string name and random value eg. /images/background.png?v=1234

  2. Upload and rename the new file with a timestamp or random value. for example: /images/background.20140917225200.png

  3. Set a shorter cache header and wait for it to expire. Here's an article from the Azure team http://msdn.microsoft.com/en-us/library/azure/gg680306.aspx

雨的味道风的声音 2024-12-31 22:55:56

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

az cdn endpoint purge 是 root 命令

示例

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

您可以找到有关 cli 命令的更多信息 此处

You can use the az cli to purge the cdn endpoint

az cdn endpoint purge is the root command

an example

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

You can find more information on the cli command here

掩饰不了的爱 2024-12-31 22:55:56

您还可以通过 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'

You can also purge CDN content via PowerShell:

$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'
萌面超妹 2024-12-31 22:55:56

您无法强制 CDN 清除。

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

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

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

You cannot force CDN purge.

Best practice is to append version/date info to your file name, and design your app to dynamically get the current file name.

For product photos as an example, append the version to the blob name, store the blob name in a table, and then serve a link to the name from the table, instead of hard-coding the filename.

This way you can set max-expiry on the cache headers, and Azure will just clean up stale content when it needs to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文