-
Notifications
You must be signed in to change notification settings - Fork 84
feat: HTTP 缓存 #2682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
feat: HTTP 缓存 #2682
Changes from 5 commits
b9482a6
7af8c97
eb57eb6
3fcdb41
1ea1345
63a6fa0
4f3c32c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Net.Http.Headers; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using PCL.Core.IO.Net.Http.Cache.Models; | ||
|
|
||
| namespace PCL.Core.IO.Net.Http.Cache; | ||
|
|
||
| /// <summary> | ||
| /// HTTP 缓存处理器 | ||
| /// </summary> | ||
| public class HttpCacheHandler:DelegatingHandler | ||
| { | ||
| private HttpCacheRepository _repository; | ||
| public HttpCacheHandler(HttpMessageHandler invoker, HttpCacheRepository repo) | ||
| { | ||
| InnerHandler = invoker; | ||
| _repository = repo; | ||
| } | ||
|
|
||
| protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) | ||
| { | ||
| return SendAsync(request, cancellationToken).GetAwaiter().GetResult(); | ||
|
sourcery-ai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
| { | ||
| if(!_repository.TryGetCacheData(request.RequestUri!.ToString(),out var details)) | ||
| return await base.SendAsync(request, cancellationToken); | ||
| if (details.ExpiredAt is not null && | ||
| details.LastUpdate.AddSeconds((double)details.ExpiredAt) < DateTimeOffset.Now | ||
| && _repository.TryGetCacheResponse(request,out var cacheResponse) && !details.EnsureValidate | ||
| ) | ||
| return cacheResponse; | ||
|
|
||
| if(details.Tag is not null) request.Headers.IfNoneMatch.Add(new EntityTagHeaderValue(details.Tag)); | ||
| if(details.LastModify is not null) request.Headers.IfModifiedSince = DateTimeOffset.Parse(details.LastModify); | ||
| var response = await base.SendAsync(request, cancellationToken); | ||
| if (response.Headers.CacheControl?.NoStore ?? false) return response; | ||
| if(response.StatusCode == HttpStatusCode.NotModified && _repository.TryGetCacheResponse(request,out cacheResponse)) | ||
| return cacheResponse; | ||
| var handle = await _repository.TryBeginUpdateAsync(request.RequestUri.ToString()); | ||
| var newDetails = handle?.Details; | ||
| newDetails?.RequestUri = request.RequestUri.ToString(); | ||
| newDetails?.LastUpdate = DateTimeOffset.Now; | ||
| newDetails?.EnsureValidate = response.Headers.CacheControl?.NoCache ?? false; | ||
|
Comment on lines
+35
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): Last-Modified 处理使用的是 Date 头而不是 Last-Modified,并且可能解析/存储无效的值。
建议实现如下: if(details.Tag is not null) request.Headers.IfNoneMatch.Add(new EntityTagHeaderValue(details.Tag));
if (details.LastModify is not null &&
DateTimeOffset.TryParse(
details.LastModify,
CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind,
out var lastModified))
{
request.Headers.IfModifiedSince = lastModified;
}
var response = await base.SendAsync(request, cancellationToken); var handle = await _repository.TryBeginUpdateAsync(request.RequestUri.ToString());
var newDetails = handle?.Details;
newDetails?.RequestUri = request.RequestUri.ToString();
newDetails?.LastUpdate = DateTimeOffset.Now;
newDetails?.EnsureValidate = response.Headers.CacheControl?.NoCache ?? false;
if (newDetails is not null)
{
var lastModifiedHeader =
response.Content?.Headers.LastModified
?? response.Headers.LastModified
?? response.Headers.Date;
newDetails.LastModify = lastModifiedHeader.HasValue
? lastModifiedHeader.Value.ToString("o", CultureInfo.InvariantCulture)
: null;
}
Original comment in Englishsuggestion (bug_risk): Last-modified handling uses the Date header instead of Last-Modified and may parse/store invalid values.
Suggested implementation: if(details.Tag is not null) request.Headers.IfNoneMatch.Add(new EntityTagHeaderValue(details.Tag));
if (details.LastModify is not null &&
DateTimeOffset.TryParse(
details.LastModify,
CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind,
out var lastModified))
{
request.Headers.IfModifiedSince = lastModified;
}
var response = await base.SendAsync(request, cancellationToken); var handle = await _repository.TryBeginUpdateAsync(request.RequestUri.ToString());
var newDetails = handle?.Details;
newDetails?.RequestUri = request.RequestUri.ToString();
newDetails?.LastUpdate = DateTimeOffset.Now;
newDetails?.EnsureValidate = response.Headers.CacheControl?.NoCache ?? false;
if (newDetails is not null)
{
var lastModifiedHeader =
response.Content?.Headers.LastModified
?? response.Headers.LastModified
?? response.Headers.Date;
newDetails.LastModify = lastModifiedHeader.HasValue
? lastModifiedHeader.Value.ToString("o", CultureInfo.InvariantCulture)
: null;
}
|
||
| newDetails?.LastModify = response.Content.Headers.LastModified.ToString(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): 对
建议:
Original comment in Englishissue (bug_risk): Using ToString() on LastModified without null checks can throw or produce culture-dependent values.
Recommend:
|
||
| newDetails?.Tag = response.Headers.ETag?.Tag; | ||
| if (handle is not null) | ||
| response.Content = new StreamContent(new CacheStream(handle, | ||
| await response.Content.ReadAsStreamAsync(cancellationToken))); | ||
| return response; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.