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.
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).
There is NO API to invalidate an Azure CDN.
Workaround:
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
Upload and rename the new file with a timestamp or random value. for example: /images/background.20140917225200.png
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
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
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'
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.
© 2022 - 2024 — McMap. All rights reserved.