Last active
July 10, 2017 14:25
-
-
Save stefanolsen/cbad66bbc1b30f7dfc3f2caa1ac66a05 to your computer and use it in GitHub Desktop.
UrlExtension for rendering a Content URL to a physical file, adding the modification date as a URL parameter. Explanation at https://stefanolsen.com/posts/cache-busting-with-aspnet-mvc/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class UrlExtensions | |
{ | |
private const string FileDateTicksCacheKeyFormat = "FileDateTicks_{0}"; | |
private static long GetFileDateTicks(this UrlHelper urlHelper, string filename) | |
{ | |
var context = urlHelper.RequestContext.HttpContext; | |
string cacheKey = string.Format(FileDateTicksCacheKeyFormat, filename); | |
// Check if we already cached the ticks in the cache. | |
if (context.Cache[cacheKey] != null) | |
{ | |
return (long)context.Cache[cacheKey]; | |
} | |
var physicalPath = context.Server.MapPath(filename); | |
var fileInfo = new FileInfo(physicalPath); | |
var dependency = new CacheDependency(physicalPath); | |
// If file exists, read number of ticks from last write date. Or fall back to 0. | |
long ticks = fileInfo.Exists ? fileInfo.LastWriteTime.Ticks : 0; | |
// Add the number of ticks to cache for 12 hours. | |
// The cache dependency will remove the entry if file is changed or deleted. | |
context.Cache.Add(cacheKey, ticks, dependency, | |
DateTime.Now.AddHours(12), TimeSpan.Zero, | |
CacheItemPriority.Normal, null); | |
return ticks; | |
} | |
public static string ContentVersioned(this UrlHelper urlHelper, string contentPath) | |
{ | |
string url = urlHelper.Content(contentPath); | |
long fileTicks = GetFileDateTicks(urlHelper, url); | |
return $"{url}?v={fileTicks}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment