Skip to content

Instantly share code, notes, and snippets.

@sihugh
Created May 13, 2015 14:22
Show Gist options
  • Save sihugh/020c160a6e02318aa934 to your computer and use it in GitHub Desktop.
Save sihugh/020c160a6e02318aa934 to your computer and use it in GitHub Desktop.
A version of http://madskristensen.net/post/cache-busting-in-aspnet for when you can't easily mess with URL rewriting
using System.IO;
using System.Web;
using System.Web.Hosting;
using System.Web.Caching;
namespace Hughesdon.Cache {
public static class Fingerprint
{
public static string Tag(string path)
{
if(HttpRuntime.Cache[path] == null)
{
string absolute = HostingEnvironment.MapPath("~" + path);
var lastModifiedDate = File.GetLastWriteTime(absolute);
string result = path + "?v=" + lastModifiedDate.Ticks;
HttpRuntime.Cache.Insert(path, result, new CacheDependency(absolute));
}
return HttpRuntime.Cache[path] as string;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment