Cache Busting in .NET

<script src=”~/jquery.js?dt=@(DateTime.Now.Ticks)”></script>
Adding a timestamp to file
Timestamp change in every request
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Caching;using System.Web.Hosting;using System.IO;namespace CacheBurstingMVC{public class CacheUrlExtension{public static string Extend(string relativePath){if (HttpRuntime.Cache[relativePath] == null){string absolutePath = HostingEnvironment.MapPath(“~” + relativePath);DateTime date = File.GetLastWriteTime(absolutePath);int index = relativePath.LastIndexOf(‘/’);string result = relativePath.Insert(index, “/v-” + date.Ticks);HttpRuntime.Cache.Insert(relativePath, result, new CacheDependency(absolutePath));}return HttpRuntime.Cache[relativePath] as string;}}}
@{Layout = null;}<!DOCTYPE html><html><head><meta name=”viewport” content=”width=device-width” /><title></title><link rel=”stylesheet” href=@CacheBurstingMVC.CacheUrlExtension.Extend(“/Content/styles.css”)/></head><body><div>Home</div></body></html>
<system.webServer><rewrite><rules><rule name=”fingerprint”><match url=”([\S]+)(/v-[0–9]+/)([\S]+)([\S]+)” /><action type=”Rewrite” url=”{R:1}/{R:3}” /></rule></rules></rewrite></system.webServer>
After deploying to IIS first load
Version Information
Changing style file and deploying again
Version Information
<script src=”~/jquery.js?v=@(AppVersionNumber)”></script>

--

--

--

Technical Speaker | Computer Engineer | Full Stack Web Developer | ML Enthusiast | * Knowledge Shared = Knowledge² *

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Product managing an integrated hardware product at Square

Ways to write MongoDB Aggregation Pipeline using Java and Spring Boot

Use of Black — code formatter

How to spot the warning signs for technical debt

Config Mailtrap in Laravel 8

My Background and Computer Science Passion!

HOW GOOGLE SCHOLARSHIP WITH ALC AT UDACITY OPENED MY WORLD TO NEW POSSIBILITIES

Installing Ruby on Rails with mySQL on macOS (Intel & Apple Silicon).

Rails 7.0.2.2 welcome screen

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vaibhav Bhapkar

Vaibhav Bhapkar

Technical Speaker | Computer Engineer | Full Stack Web Developer | ML Enthusiast | * Knowledge Shared = Knowledge² *

More from Medium

The Facilis framework for dotnet core — the repository

C# Interface JSON Serialization and Deserialization

Bending DateTime in .NET to Test Your Code Better

Manage Hierarchies with Composite Pattern