Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions IntegrationTests/DistributedLockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 12 additions & 8 deletions IntegrationTests/MultiSourceApiRequestCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,18 +28,18 @@ public MultiSourceApiRequestCacheTests(WebApplicationFactory<Program> factory)
memoryDbContext.SaveChanges();
var users = new List<User>()
{
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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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<User>();
Assert.True(userByCache.Time > 0);

user.Name = "joe";

var updateAfter = await _httpClient.PutAsJsonAsync(baseUrl, user);
Expand Down Expand Up @@ -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);
}
Expand Down
14 changes: 7 additions & 7 deletions IntegrationTests/UserApiRequestCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public UserApiRequestCacheTests(WebApplicationFactory<Program> factory)
memoryDbContext.SaveChanges();
var users = new List<User>()
{
new()
new(DateTimeOffset.UtcNow)
{
Id = "1",
Name = "anson1"
},
new()
new(DateTimeOffset.UtcNow)
{
Id = "2",
Name = "anson2"
},
new()
new(DateTimeOffset.UtcNow)
{
Id = "3",
Name = "anson3"
Expand Down Expand Up @@ -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",
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
13 changes: 13 additions & 0 deletions TestApi/Entity/User.cs
Original file line number Diff line number Diff line change
@@ -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(){}

Check warning on line 9 in TestApi/Entity/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 9 in TestApi/Entity/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

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<long>? ThirdPartyIds { get; set; }
}
2 changes: 1 addition & 1 deletion TestApi/Service/MultiSourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public virtual IEnumerable<User> List(string page)
return dbContext.Set<User>().ToList();
}

[MultiSourceCacheable("MultiSource-single", "{id}", Target.Redis, 5)]
[MultiSourceCacheable("MultiSource-single", "{id}", Target.Redis, 1000)]
public async Task<User?> SingleOrDefault(string id)
{
return await dbContext.Set<User>().SingleOrDefaultAsync(x => x.Id == id);
Expand Down
Loading