From 671590b071cccfb889e68c3f74581fcf15a02921 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 13 Mar 2020 17:58:32 +0800 Subject: Add cache for timeline post data. --- Timeline/Controllers/TimelineController.cs | 9 +++-- Timeline/Controllers/UserAvatarController.cs | 1 - Timeline/Helpers/DataCacheHelper.cs | 25 +++++++++++--- Timeline/Services/TimelineService.cs | 49 ++++++++++++++++++++++++++-- 4 files changed, 74 insertions(+), 10 deletions(-) (limited to 'Timeline') diff --git a/Timeline/Controllers/TimelineController.cs b/Timeline/Controllers/TimelineController.cs index 58390c29..8bc0345f 100644 --- a/Timeline/Controllers/TimelineController.cs +++ b/Timeline/Controllers/TimelineController.cs @@ -9,6 +9,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; using Timeline.Filters; +using Timeline.Helpers; using Timeline.Models; using Timeline.Models.Http; using Timeline.Models.Validation; @@ -114,7 +115,6 @@ namespace Timeline.Controllers return result; } - // TODO: Make cache available. [HttpGet("timelines/{name}/posts/{id}/data")] public async Task>> PostDataGet([FromRoute][GeneralTimelineName] string name, [FromRoute] long id) { @@ -125,8 +125,11 @@ namespace Timeline.Controllers try { - var data = await _service.GetPostData(name, id); - return File(data.Data, data.Type, data.LastModified, new EntityTagHeaderValue($"\"{data.ETag}\"")); + return await DataCacheHelper.GenerateActionResult(this, () => _service.GetPostDataETag(name, id), async () => + { + var data = await _service.GetPostData(name, id); + return data; + }); } catch (TimelinePostNotExistException) { diff --git a/Timeline/Controllers/UserAvatarController.cs b/Timeline/Controllers/UserAvatarController.cs index f78dcb08..b5f4be1e 100644 --- a/Timeline/Controllers/UserAvatarController.cs +++ b/Timeline/Controllers/UserAvatarController.cs @@ -32,7 +32,6 @@ namespace Timeline.Controllers } [HttpGet("users/{username}/avatar")] - [ResponseCache(NoStore = false, Location = ResponseCacheLocation.None, Duration = 0)] public async Task Get([FromRoute][Username] string username) { long id; diff --git a/Timeline/Helpers/DataCacheHelper.cs b/Timeline/Helpers/DataCacheHelper.cs index c13aaddb..574d90b4 100644 --- a/Timeline/Helpers/DataCacheHelper.cs +++ b/Timeline/Helpers/DataCacheHelper.cs @@ -66,11 +66,25 @@ namespace Timeline.Helpers public static class DataCacheHelper { - public static async Task GenerateActionResult(Controller controller, ICacheableDataProvider provider) + public static async Task GenerateActionResult(Controller controller, ICacheableDataProvider provider, TimeSpan? maxAge = null) { + const string CacheControlHeaderKey = "Cache-Control"; const string IfNonMatchHeaderKey = "If-None-Match"; const string ETagHeaderKey = "ETag"; + string GenerateCacheControlHeaderValue() + { + var cacheControlHeader = new CacheControlHeaderValue() + { + NoCache = true, + NoStore = false, + MaxAge = maxAge ?? TimeSpan.FromDays(14), + Private = true, + MustRevalidate = true + }; + return cacheControlHeader.ToString(); + } + var loggerFactory = controller.HttpContext.RequestServices.GetRequiredService(); var logger = loggerFactory.CreateLogger(typeof(DataCacheHelper)); @@ -89,20 +103,23 @@ namespace Timeline.Helpers if (eTagList.FirstOrDefault(e => e.Equals(eTag)) != null) { - controller.Response.Headers.Add(ETagHeaderKey, eTagValue); logger.LogInformation(LogResultNotModified); + controller.Response.Headers.Add(ETagHeaderKey, eTagValue); + controller.Response.Headers.Add(CacheControlHeaderKey, GenerateCacheControlHeaderValue()); + return controller.StatusCode(StatusCodes.Status304NotModified); } } var data = await provider.GetData(); logger.LogInformation(LogResultData); + controller.Response.Headers.Add(CacheControlHeaderKey, GenerateCacheControlHeaderValue()); return controller.File(data.Data, data.Type, data.LastModified, eTag); } - public static Task GenerateActionResult(Controller controller, Func> getDataETagDelegate, Func> getDataDelegate) + public static Task GenerateActionResult(Controller controller, Func> getDataETagDelegate, Func> getDataDelegate, TimeSpan? maxAge = null) { - return GenerateActionResult(controller, new DelegateCacheableDataProvider(getDataETagDelegate, getDataDelegate)); + return GenerateActionResult(controller, new DelegateCacheableDataProvider(getDataETagDelegate, getDataDelegate), maxAge); } } } diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index 301a1d97..b26016e5 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -7,6 +7,7 @@ using System.Globalization; using System.Linq; using System.Threading.Tasks; using Timeline.Entities; +using Timeline.Helpers; using Timeline.Models; using Timeline.Models.Validation; using static Timeline.Resources.Services.TimelineService; @@ -32,14 +33,14 @@ namespace Timeline.Services public long UserId { get; set; } } - public class PostData + public class PostData : ICacheableData { #pragma warning disable CA1819 // Properties should not return arrays public byte[] Data { get; set; } = default!; #pragma warning restore CA1819 // Properties should not return arrays public string Type { get; set; } = default!; public string ETag { get; set; } = default!; - public DateTime LastModified { get; set; } = default!; + public DateTime? LastModified { get; set; } } /// @@ -91,6 +92,20 @@ namespace Timeline.Services /// See remarks of . Task> GetPosts(string name); + /// + /// Get the etag of data of a post. + /// + /// See remarks of . + /// The id of the post. + /// The etag of the data. + /// Thrown when is null. + /// See remarks of . + /// See remarks of . + /// Thrown when post of does not exist or has been deleted. + /// Thrown when post has no data. See remarks. + /// + Task GetPostDataETag(string name, long postId); + /// /// Get the data of a post. /// @@ -105,6 +120,7 @@ namespace Timeline.Services /// /// Use this method to retrieve the image of image post. /// + /// Task GetPostData(string name, long postId); /// @@ -402,6 +418,29 @@ namespace Timeline.Services } return posts; } + + public async Task GetPostDataETag(string name, long postId) + { + if (name == null) + throw new ArgumentNullException(nameof(name)); + + var timelineId = await FindTimelineId(name); + var postEntity = await Database.TimelinePosts.Where(p => p.LocalId == postId).SingleOrDefaultAsync(); + + if (postEntity == null) + throw new TimelinePostNotExistException(name, postId); + + if (postEntity.Content == null) + throw new TimelinePostNotExistException(name, postId, true); + + if (postEntity.ContentType != TimelinePostContentTypes.Image) + throw new InvalidOperationException(ExceptionGetDataNonImagePost); + + var tag = postEntity.Content; + + return tag; + } + public async Task GetPostData(string name, long postId) { if (name == null) @@ -1014,6 +1053,12 @@ namespace Timeline.Services return s.GetPosts(realName); } + public Task GetPostDataETag(string name, long postId) + { + var s = BranchName(name, out var realName); + return s.GetPostDataETag(realName, postId); + } + public Task GetPostData(string name, long postId) { var s = BranchName(name, out var realName); -- cgit v1.2.3