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 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'Timeline/Controllers/TimelineController.cs') 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) { -- cgit v1.2.3 From 1af4f51abcd09be68bc0e2ad77b163569a50a6b3 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 13 Mar 2020 18:03:29 +0800 Subject: Clean code. --- Timeline.Tests/PasswordGenerator.cs | 3 +-- Timeline/Controllers/TimelineController.cs | 1 - Timeline/Controllers/UserAvatarController.cs | 2 -- Timeline/Helpers/Log.cs | 1 - Timeline/Models/Http/UserController.cs | 1 - Timeline/Models/Validation/Validator.cs | 5 +---- Timeline/Services/Clock.cs | 3 --- Timeline/Services/PathProvider.cs | 4 ---- Timeline/Startup.cs | 2 +- 9 files changed, 3 insertions(+), 19 deletions(-) (limited to 'Timeline/Controllers/TimelineController.cs') diff --git a/Timeline.Tests/PasswordGenerator.cs b/Timeline.Tests/PasswordGenerator.cs index 6c07836b..863439b5 100644 --- a/Timeline.Tests/PasswordGenerator.cs +++ b/Timeline.Tests/PasswordGenerator.cs @@ -1,5 +1,4 @@ -using System; -using Timeline.Services; +using Timeline.Services; using Xunit; using Xunit.Abstractions; diff --git a/Timeline/Controllers/TimelineController.cs b/Timeline/Controllers/TimelineController.cs index 8bc0345f..f1781ff3 100644 --- a/Timeline/Controllers/TimelineController.cs +++ b/Timeline/Controllers/TimelineController.cs @@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using Microsoft.Net.Http.Headers; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; diff --git a/Timeline/Controllers/UserAvatarController.cs b/Timeline/Controllers/UserAvatarController.cs index b5f4be1e..4062837b 100644 --- a/Timeline/Controllers/UserAvatarController.cs +++ b/Timeline/Controllers/UserAvatarController.cs @@ -2,9 +2,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using Microsoft.Net.Http.Headers; using System; -using System.Linq; using System.Threading.Tasks; using Timeline.Auth; using Timeline.Filters; diff --git a/Timeline/Helpers/Log.cs b/Timeline/Helpers/Log.cs index 68c975fa..af0b7e13 100644 --- a/Timeline/Helpers/Log.cs +++ b/Timeline/Helpers/Log.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Text; namespace Timeline.Helpers diff --git a/Timeline/Models/Http/UserController.cs b/Timeline/Models/Http/UserController.cs index e4c95cbd..5ee02a95 100644 --- a/Timeline/Models/Http/UserController.cs +++ b/Timeline/Models/Http/UserController.cs @@ -1,7 +1,6 @@ using AutoMapper; using System.ComponentModel.DataAnnotations; using Timeline.Models.Validation; -using Timeline.Services; namespace Timeline.Models.Http { diff --git a/Timeline/Models/Validation/Validator.cs b/Timeline/Models/Validation/Validator.cs index ead7dbef..db139448 100644 --- a/Timeline/Models/Validation/Validator.cs +++ b/Timeline/Models/Validation/Validator.cs @@ -1,8 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Localization; -using System; +using System; using System.ComponentModel.DataAnnotations; -using Timeline.Helpers; using static Timeline.Resources.Models.Validation.Validator; namespace Timeline.Models.Validation diff --git a/Timeline/Services/Clock.cs b/Timeline/Services/Clock.cs index 0499c0c6..040f9304 100644 --- a/Timeline/Services/Clock.cs +++ b/Timeline/Services/Clock.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace Timeline.Services { diff --git a/Timeline/Services/PathProvider.cs b/Timeline/Services/PathProvider.cs index 15e66972..1f6b629a 100644 --- a/Timeline/Services/PathProvider.cs +++ b/Timeline/Services/PathProvider.cs @@ -1,9 +1,5 @@ using Microsoft.Extensions.Configuration; -using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Threading.Tasks; namespace Timeline.Services { diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 38bb3164..f5220446 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -108,7 +108,7 @@ namespace Timeline services.TryAddSingleton(); - services.AddDbContext((services, options )=> + services.AddDbContext((services, options) => { var pathProvider = services.GetRequiredService(); options.UseSqlite($"Data Source={pathProvider.GetDatabaseFilePath()}"); -- cgit v1.2.3