Invalidate cache on every build/deploy/startup?

I am loading some parts of my css into a lazy cache, and provide a cache key, is there a / what type of variable/id is best to use for the cache key to invalidate on every build or startup?
Litium version: 7.3

In general the css/js that should be loaded should have a version query-string parameter to make it the url unique for each change. The Litium FingerprintExtension.ContentFingerprint extensions method are doing this and calculating an hash of the content in the file.

if that not solves your question I need to have little more information about how your setup is working to see if I can find another solution.

We are doing this in the HeadViewModelBuilder and we cache it so we don’t touch the file on disk, but dose the fingerprint read the file on every pageview?

	private string BuildCriticalCss(WebsiteModel websiteModel)
	{

		if (websiteModel.Website.IsElevenWebsite())
		{
			var cachingService = new CachingService();
			var path = HttpContext.Current.Server.MapPath("/ui/Eleven/css/site.min.css");
			string cacheKey = $"css_" + FingerprintExtension.ContentFingerprint(path);
			return cachingService.GetOrAdd(cacheKey, () => GetCss(path), DateTimeOffset.UtcNow.AddMinutes(60 * 24));
		}
		return "";
	}

	private string GetCss(string filePath){
			return File.ReadAllText(filePath);
	}

If the fingerprint reads the file on disk, this might be just as effetive? @Html.Raw(File.ReadAllText(Server.MapPath("/ui/Eleven/css/critical.min.css")))

The FingerprintExtension.ContentFingerprint is using the HttpContext.Cache with a file system dependency so when the file is changed the cache is removed; otherwise it using a cached fingerprint to not need to recalculate that for every request.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.