How do I force Azure CDN content to be purged or invalidated?

天大地大妈咪最大 提交于 2019-12-18 21:54:25

问题


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.


回答1:


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).




回答2:


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




回答3:


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'



回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/8754611/how-do-i-force-azure-cdn-content-to-be-purged-or-invalidated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!