-
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 all 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,52 @@ | ||
| 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 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; | ||
| 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; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (bug_risk): Last-Modified 处理使用的是 Date 头而不是 Last-Modified,并且可能解析/存储无效的值。
LastModify通过If-Modified-Since头发送,但在更新时你使用的是response.Headers.Date并直接调用ToString()。这会忽略Last-Modified头部、可能为 null,并产生后续可能无法稳定解析的字符串。建议优先使用response.Content.Headers.LastModified(或根据需求使用response.Headers.LastModified),并将其存为DateTimeOffset或标准化字符串(例如ToString("o")),在下次构建请求时使用不变区域性进行解析。建议实现如下:
HttpCacheHandler.cs顶部添加using System.Globalization;,以便CultureInfo和DateTimeStyles能够正确解析。LastModify是一个可空的string。如果你更倾向将其存为DateTimeOffset?,请相应更新Details模型,并去掉.ToString("o", ...)与解析调用,直接使用DateTimeOffset?赋值。Original comment in English
suggestion (bug_risk): Last-modified handling uses the Date header instead of Last-Modified and may parse/store invalid values.
LastModifyis sent asIf-Modified-Since, but when updating you useresponse.Headers.DateandToString(). That ignores theLast-Modifiedheader, may be null, and produces a value that might not parse reliably later. Preferresponse.Content.Headers.LastModified(orresponse.Headers.LastModifiedif intended), store it as aDateTimeOffsetor a standardized string (e.g.ToString("o")), and parse using invariant culture when creating the next request.Suggested implementation:
using System.Globalization;is present at the top ofHttpCacheHandler.cssoCultureInfoandDateTimeStylesresolve correctly.LastModifyis a nullablestring. If you prefer to store it asDateTimeOffset?, update theDetailsmodel accordingly and remove the.ToString("o", ...)/parse calls, assigning theDateTimeOffset?directly instead.