From f1d9016bc55c3dc894ed20606f1347a3b6af91b6 Mon Sep 17 00:00:00 2001 From: "https://gitlab.sjfood.us/Joe.G" Date: Sat, 26 Apr 2025 11:48:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=BA=E5=8C=96=E6=B5=8B=E8=AF=95=E6=83=85?= =?UTF-8?q?=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IntegrationTests/DistributedLockTests.cs | 4 ++-- .../MultiSourceApiRequestCacheTests.cs | 20 +++++++++++-------- IntegrationTests/UserApiRequestCacheTests.cs | 14 ++++++------- TestApi/Entity/User.cs | 13 ++++++++++++ TestApi/Service/MultiSourceService.cs | 2 +- 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/IntegrationTests/DistributedLockTests.cs b/IntegrationTests/DistributedLockTests.cs index 13927b2..3d1c552 100644 --- a/IntegrationTests/DistributedLockTests.cs +++ b/IntegrationTests/DistributedLockTests.cs @@ -50,7 +50,7 @@ async Task SortedSet(int index, int delayMs) var stopwatch = new Stopwatch(); stopwatch.Start(); var responseMessage = await _httpClient.PostAsJsonAsync($"{baseUrl}?delayMs={delayMs}", - new User { Id = index.ToString(), Name = index.ToString() }); + new User(DateTimeOffset.UtcNow) { Id = index.ToString(), Name = index.ToString() }); stopwatch.Stop(); var message = await responseMessage.Content.ReadAsStringAsync(); @@ -86,7 +86,7 @@ async Task SortedSet(int index, int delayMs) var stopwatch = new Stopwatch(); stopwatch.Start(); var response = await _httpClient.PostAsJsonAsync($"{baseUrl}/add-with-cache?delayMs={delayMs}", - new User { Id = index.ToString(), Name = index.ToString() }); + new User(DateTimeOffset.UtcNow) { Id = index.ToString(), Name = index.ToString() }); stopwatch.Stop(); var message = await response.Content.ReadAsStringAsync(); diff --git a/IntegrationTests/MultiSourceApiRequestCacheTests.cs b/IntegrationTests/MultiSourceApiRequestCacheTests.cs index d2b7b1b..45db8fa 100644 --- a/IntegrationTests/MultiSourceApiRequestCacheTests.cs +++ b/IntegrationTests/MultiSourceApiRequestCacheTests.cs @@ -6,8 +6,6 @@ using System.Net.Http; using System.Net.Http.Json; using System.Threading.Tasks; -using FastCache.Core.Driver; -using FastCache.Redis.Driver; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using TestApi.DB; @@ -30,18 +28,18 @@ public MultiSourceApiRequestCacheTests(WebApplicationFactory factory) memoryDbContext.SaveChanges(); var users = new List() { - new() + new(DateTimeOffset.UtcNow) { Id = "1", Name = "anson1", Age = 10 }, - new() + new(DateTimeOffset.UtcNow) { Id = "2", Name = "anson2" }, - new() + new(DateTimeOffset.UtcNow) { Id = "3", Name = "anson3" @@ -96,7 +94,7 @@ public async void CacheCanEvict(string baseUrl) var result1 = await resp1.Content.ReadAsStringAsync(); - var resultForPost = await _httpClient.PutAsJsonAsync($"{baseUrl}?id=3", new User() + var resultForPost = await _httpClient.PutAsJsonAsync($"{baseUrl}?id=3", new User(DateTimeOffset.UtcNow) { Id = "3", Name = "anson33" @@ -118,7 +116,7 @@ public async void CacheCanEvict(string baseUrl) [InlineData("/MultiSourceInMemory")] public async void CacheAndEvictOther(string baseUrl) { - await _httpClient.PostAsJsonAsync($"{baseUrl}", new User() + await _httpClient.PostAsJsonAsync($"{baseUrl}", new User(DateTimeOffset.UtcNow) { Id = "5", Name = "anson5" @@ -163,6 +161,12 @@ public async Task TestUpdated(string baseUrl) Assert.NotNull(user); + var responseMessage2 = await _httpClient.GetAsync($"{baseUrl}?id=1"); + Assert.Equal(responseMessage2.StatusCode, HttpStatusCode.OK); + + var userByCache = await responseMessage2.Content.ReadFromJsonAsync(); + Assert.True(userByCache.Time > 0); + user.Name = "joe"; var updateAfter = await _httpClient.PutAsJsonAsync(baseUrl, user); @@ -238,7 +242,7 @@ public async void TestMultiSourceCacheableMultipleParam(string baseUrl) { var responseMessage = await _httpClient.GetAsync($"{baseUrl}/get/two?id=123&name=anson1"); Assert.Equal(responseMessage.StatusCode, HttpStatusCode.NoContent); - + var responseMessage2 = await _httpClient.GetAsync($"{baseUrl}/get/two?id=123&name=anson1"); Assert.Equal(responseMessage2.StatusCode, HttpStatusCode.NoContent); } diff --git a/IntegrationTests/UserApiRequestCacheTests.cs b/IntegrationTests/UserApiRequestCacheTests.cs index 54154d3..9111d32 100644 --- a/IntegrationTests/UserApiRequestCacheTests.cs +++ b/IntegrationTests/UserApiRequestCacheTests.cs @@ -26,17 +26,17 @@ public UserApiRequestCacheTests(WebApplicationFactory factory) memoryDbContext.SaveChanges(); var users = new List() { - new() + new(DateTimeOffset.UtcNow) { Id = "1", Name = "anson1" }, - new() + new(DateTimeOffset.UtcNow) { Id = "2", Name = "anson2" }, - new() + new(DateTimeOffset.UtcNow) { Id = "3", Name = "anson3" @@ -83,7 +83,7 @@ public async void CacheCanEvict() var result1 = await resp1.Content.ReadAsStringAsync(); - var resultForPost = await _httpClient.PutAsJsonAsync("/user?id=3", new User() + var resultForPost = await _httpClient.PutAsJsonAsync("/user?id=3", new User(DateTimeOffset.UtcNow) { Id = "3", Name = "anson33", @@ -104,7 +104,7 @@ public async void CacheCanEvict() [Fact] public async void CacheAndEvictOther() { - await _httpClient.PostAsJsonAsync("/user", new User() + await _httpClient.PostAsJsonAsync("/user", new User(DateTimeOffset.UtcNow) { Id = "5", Name = "anson5" @@ -141,7 +141,7 @@ public async void CacheAndEvictOther() [Fact] public async void CacheBySameNameFun() { - await _httpClient.PostAsJsonAsync("/user/indirect-impl", new User() + await _httpClient.PostAsJsonAsync("/user/indirect-impl", new User(DateTimeOffset.UtcNow) { Id = "5", Name = "anson5" @@ -167,7 +167,7 @@ await _httpClient.GetAsync( [Fact] public async void CacheWhileIndirectImpl() { - await _httpClient.PostAsJsonAsync("/user/indirect-impl", new User() + await _httpClient.PostAsJsonAsync("/user/indirect-impl", new User(DateTimeOffset.UtcNow) { Id = "5", Name = "anson5" diff --git a/TestApi/Entity/User.cs b/TestApi/Entity/User.cs index 20e8ca8..1ae2b3d 100644 --- a/TestApi/Entity/User.cs +++ b/TestApi/Entity/User.cs @@ -1,13 +1,26 @@ using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json.Serialization; +using Newtonsoft.Json; namespace TestApi.Entity; public record User : IEntity { + public User(){} + + public User(DateTimeOffset time) + { + Time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + } + public string Id { get; set; } public string Name { get; set; } public int Age { get; set; } + + [JsonProperty("Time")] + [JsonInclude] + public long Time { get; private set; } [NotMapped]public List? ThirdPartyIds { get; set; } } \ No newline at end of file diff --git a/TestApi/Service/MultiSourceService.cs b/TestApi/Service/MultiSourceService.cs index 682c1c9..63e96cc 100644 --- a/TestApi/Service/MultiSourceService.cs +++ b/TestApi/Service/MultiSourceService.cs @@ -53,7 +53,7 @@ public virtual IEnumerable List(string page) return dbContext.Set().ToList(); } - [MultiSourceCacheable("MultiSource-single", "{id}", Target.Redis, 5)] + [MultiSourceCacheable("MultiSource-single", "{id}", Target.Redis, 1000)] public async Task SingleOrDefault(string id) { return await dbContext.Set().SingleOrDefaultAsync(x => x.Id == id);