Skip to content

Commit 4749f1f

Browse files
committed
Allow ModifiedUtc to vary between variation and response
1 parent 2d9f701 commit 4749f1f

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

src/HttpClient.Cache/Files/FileCache.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ TimeProvider timeProvider
156156
return null;
157157
}
158158

159-
fileInfo = FindJsonFile(responseKey);
159+
fileInfo = FindJsonFile(responseKey, filename.EtagHash);
160160
if (fileInfo is null)
161161
{
162162
return null;
@@ -397,7 +397,7 @@ DateTimeOffset expiration
397397
return;
398398
}
399399

400-
fileInfo = FindJsonFile(responseKey);
400+
fileInfo = FindJsonFile(responseKey, filename.EtagHash);
401401
if (fileInfo is null)
402402
{
403403
return;
@@ -422,6 +422,16 @@ DateTimeOffset expiration
422422
return _rootDirectory.EnumerateFiles($"{hash}_*.json").MaxBy(x => x.Name);
423423
}
424424

425+
private FileInfo? FindJsonFile(string key, string? etagHash)
426+
{
427+
var hash = Hash.ComputeHash(key);
428+
429+
// Rely on that the file name includes the "modified" timestamp right after the hash
430+
return _rootDirectory
431+
.EnumerateFiles($"{hash}_*_{etagHash ?? ""}.*.json")
432+
.MaxBy(x => x.Name);
433+
}
434+
425435
public void Clear()
426436
{
427437
foreach (var fileInfo in _rootDirectory.GetFiles("*.json", SearchOption.AllDirectories))

src/HttpClient.Cache/Files/FileName.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ public static FileName FromFileInfo(FileInfo fileInfo)
110110
);
111111
Debug.Assert(modifiedUtc.Kind == DateTimeKind.Utc);
112112

113-
var etagHash = basename[(index + 1 + DateTimeFormat.Length + 1)..].ToString();
113+
var etagHash = basename[(index + 1 + DateTimeFormat.Length + 1)..];
114114

115-
return new(hash, modifiedUtc, etagHash, extension);
115+
return new(hash, modifiedUtc, etagHash.Length > 0 ? etagHash.ToString() : null, extension);
116116
}
117117

118118
public static implicit operator string(FileName filename) => filename.ToString();

src/HttpClient.Cache/HttpClient.Cache.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<None Include="..\..\README.md" Pack="true" PackagePath="">
2020
<Link>Properties\README.md</Link>
2121
</None>
22+
<InternalsVisibleTo Include="HttpClient.Cache.Tests" />
2223
</ItemGroup>
2324
<ItemGroup>
2425
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.6" />
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using HttpClientCache.Files;
2+
3+
namespace HttpClientCache.Tests.File;
4+
5+
public class FileNameTests
6+
{
7+
[Fact]
8+
public void CanParseFileName_WithEtagHash()
9+
{
10+
var fileInfo = new FileInfo(
11+
"efed661293e37fe7141f28819252c3365aadae65_2025-06-24T102811Z_c6e5a96ed499f364089ebe92d16e13280ef750ce.response.json"
12+
);
13+
var fileName = FileName.FromFileInfo(fileInfo);
14+
15+
Assert.Equal("efed661293e37fe7141f28819252c3365aadae65", fileName.KeyHash);
16+
Assert.Equal(new DateTime(2025, 6, 24, 10, 28, 11, DateTimeKind.Utc), fileName.ModifiedUtc);
17+
Assert.Equal("c6e5a96ed499f364089ebe92d16e13280ef750ce", fileName.EtagHash);
18+
}
19+
20+
[Fact]
21+
public void CanParseFileName_WithoutEtag()
22+
{
23+
var fileInfo = new FileInfo(
24+
"efed661293e37fe7141f28819252c3365aadae65_2025-06-24T102811Z_.response.json"
25+
);
26+
var fileName = FileName.FromFileInfo(fileInfo);
27+
28+
Assert.Equal("efed661293e37fe7141f28819252c3365aadae65", fileName.KeyHash);
29+
Assert.Equal(new DateTime(2025, 6, 24, 10, 28, 11, DateTimeKind.Utc), fileName.ModifiedUtc);
30+
Assert.Null(fileName.EtagHash);
31+
}
32+
}

0 commit comments

Comments
 (0)