-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCacheEntry.cs
70 lines (57 loc) · 2.21 KB
/
CacheEntry.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
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
namespace Tavis.HttpCache
{
public class CacheEntry
{
public CacheKey Key { get; private set; }
public Guid VariantId { get; private set; }
public DateTimeOffset Expires { get; set; }
public DateTime Date { get; set; }
public CacheControlHeaderValue CacheControl { get; set; }
public bool HasValidator { get; set; }
public DateTimeOffset? LastModified { get; set; }
public string Etag { get; set; }
private List<string> Vary { get; set; }
private Dictionary<string, IEnumerable<string>> ResponseVaryHeaders { get; set; }
public CacheEntry(CacheKey key, HttpResponseMessage response)
{
Key = key;
Vary = response.Headers.Vary.Select(v=> v.ToLowerInvariant()).ToList();
ResponseVaryHeaders = response.RequestMessage.Headers
.Where(h => Vary.Contains(h.Key.ToLowerInvariant()))
.ToDictionary(k => k.Key.ToLowerInvariant(), v => v.Value);
VariantId = Guid.NewGuid();
HasValidator = response.Headers.ETag != null || (response.Content != null && response.Content.Headers.LastModified != null);
CacheControl = response.Headers.CacheControl ?? new CacheControlHeaderValue();
}
public bool IsFresh()
{
return Expires > DateTime.UtcNow;
}
public bool Match(HttpRequestMessage request)
{
foreach (var h in Vary)
{
if (h != "*")
{
IEnumerable<string> newheader = null;
request.Headers.TryGetValues(h, out newheader);
var oldheader = ResponseVaryHeaders[h];
if (newheader == null || !newheader.SequenceEqual(oldheader))
{
return false;
}
}
else
{
return false;
}
}
return true;
}
}
}