-
Notifications
You must be signed in to change notification settings - Fork 1
/
CacheBuster.cs
61 lines (54 loc) · 2.19 KB
/
CacheBuster.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
namespace Webapp
{
public static class CacheBuster
{
public static bool DoNotCacheJsFileNameResolver =
ConfigurationManager.AppSettings["DoNotCacheJsFileNameResolver"] != null;
public static string JsFileNameResolver(
HttpServerUtility server,
string fileName)
{
return (DoNotCacheJsFileNameResolver)?
GetCacheBustedFileName((p, f) => FilesIn(server, p, f),fileName):
GetCacheBustedFileNameCached((p, f) => FilesIn(server, p, f), fileName);
}
public static ConcurrentDictionary<string, string> CacheBustedFileNameCache =
new ConcurrentDictionary<string, string>();
public static string GetCacheBustedFileNameCached(
Func<string, string, List<string>> fileResolver,
// path, fileMatchingPattern, returns matching files (usually CacheBuster.FilesIn)
string appBundleJsFilename)
{
if (CacheBustedFileNameCache.ContainsKey(appBundleJsFilename))
return CacheBustedFileNameCache[appBundleJsFilename];
var result = GetCacheBustedFileName(fileResolver, appBundleJsFilename);
CacheBustedFileNameCache[appBundleJsFilename] = result;
return result;
}
public static string GetCacheBustedFileName(
Func<string, string, List<string>> fileResolver,
// path, fileMatchingPattern, returns matching files.
//(fileResolver is usually CacheBuster.FilesIn, except for testing)
string appBundleJsFilename)
{
var serverPath = Path.GetDirectoryName(appBundleJsFilename);
var extension = Path.GetExtension(appBundleJsFilename);
var fileNameMinusExtension = Path.GetFileNameWithoutExtension(appBundleJsFilename);
var matchingFiles = fileResolver(serverPath, string.Format("{0}.*{1}", fileNameMinusExtension, extension));
var result = Path.Combine(serverPath, matchingFiles.Single());
return result.Replace("\\", "/");
}
public static List<string> FilesIn(HttpServerUtility server, string serverPath, string matchingPattern)
{
DirectoryInfo diFiles = new DirectoryInfo(server.MapPath(serverPath));
return diFiles.GetFiles(matchingPattern).Select(x => x.Name).ToList();
}
}
}