Skip to content

Commit bd97038

Browse files
authored
Merge pull request #630 from Kaliumhexacyanoferrat/genhttp-compliant-crud
[C#] GenHTTP: Compliant implementation of CRUD (with 200ms TTL)
2 parents aaf8fde + 19b4e0b commit bd97038

4 files changed

Lines changed: 47 additions & 36 deletions

File tree

frameworks/genhttp/Tests/Crud.cs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
using System.Text.Json;
22
using GenHTTP.Api.Content;
3-
using GenHTTP.Api.Content.Caching;
43
using GenHTTP.Api.Protocol;
54
using genhttp.Infrastructure;
6-
using GenHTTP.Modules.Caching;
75
using GenHTTP.Modules.IO;
86
using GenHTTP.Modules.Reflection;
97
using GenHTTP.Modules.Webservices;
10-
using StringContent = GenHTTP.Modules.IO.Strings.StringContent;
8+
using Microsoft.Extensions.Caching.Memory;
119

1210
namespace genhttp.Tests;
1311

1412
public class Crud
1513
{
1614
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web);
1715

18-
private static readonly ICache<string> ItemCache = Cache.Memory<string>().Build();
16+
private static readonly IMemoryCache ItemCache = new MemoryCache(new MemoryCacheOptions());
17+
18+
private static readonly MemoryCacheEntryOptions ItemCacheOptions = new() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMilliseconds(200) };
1919

2020
[ResourceMethod]
2121
public async Task<CrudListResponse> List(string category = "electronics", int page = 1, int limit = 10)
@@ -42,28 +42,34 @@ public async Task<CrudListResponse> List(string category = "electronics", int pa
4242
{
4343
items.Add(new ProcessedItem
4444
{
45-
Id = reader.GetInt32(0),
46-
Name = reader.GetString(1),
45+
Id = reader.GetInt32(0),
46+
Name = reader.GetString(1),
4747
Category = reader.GetString(2),
48-
Price = reader.GetInt32(3),
48+
Price = reader.GetInt32(3),
4949
Quantity = reader.GetInt32(4),
50-
Active = reader.GetBoolean(5),
51-
Tags = JsonSerializer.Deserialize<List<string>>(reader.GetString(6)),
52-
Rating = new RatingInfo { Score = (int)reader.GetDouble(7), Count = reader.GetInt32(8) }
50+
Active = reader.GetBoolean(5),
51+
Tags = JsonSerializer.Deserialize<List<string>>(reader.GetString(6)),
52+
Rating = new RatingInfo
53+
{
54+
Score = (int)reader.GetDouble(7),
55+
Count = reader.GetInt32(8)
56+
}
5357
});
5458
}
5559

56-
return new CrudListResponse { Items = items, Total = items.Count, Page = page, Limit = limit };
60+
return new CrudListResponse
61+
{
62+
Items = items,
63+
Total = items.Count,
64+
Page = page,
65+
Limit = limit
66+
};
5767
}
5868

5969
[ResourceMethod(":id")]
6070
public async ValueTask<IResponse> Get(int id, IRequest request)
6171
{
62-
var cacheKey = id.ToString();
63-
64-
var cached = await ItemCache.GetEntryAsync(cacheKey, string.Empty);
65-
66-
if (cached != null)
72+
if (ItemCache.TryGetValue(id, out string cached))
6773
{
6874
return request.Respond()
6975
.Content(cached)
@@ -81,8 +87,8 @@ public async ValueTask<IResponse> Get(int id, IRequest request)
8187

8288
var json = JsonSerializer.Serialize(item, JsonOptions);
8389

84-
await ItemCache.StoreAsync(cacheKey, string.Empty, json);
85-
90+
ItemCache.Set(id, json, ItemCacheOptions);
91+
8692
return request.Respond()
8793
.Content(json)
8894
.Type(ContentType.ApplicationJson)
@@ -130,7 +136,7 @@ public async Task<CrudItem> Update(int id, CrudItem item)
130136
throw new ProviderException(ResponseStatus.NotFound, $"Item with ID {id} does not exist");
131137
}
132138

133-
await ItemCache.StoreAsync(id.ToString(), string.Empty, null);
139+
ItemCache.Remove(id);
134140

135141
return item;
136142
}
@@ -149,14 +155,18 @@ public async Task<CrudItem> Update(int id, CrudItem item)
149155

150156
return new ProcessedItem()
151157
{
152-
Id = reader.GetInt32(0),
153-
Name = reader.GetString(1),
158+
Id = reader.GetInt32(0),
159+
Name = reader.GetString(1),
154160
Category = reader.GetString(2),
155-
Price = reader.GetInt32(3),
161+
Price = reader.GetInt32(3),
156162
Quantity = reader.GetInt32(4),
157-
Active = reader.GetBoolean(5),
158-
Tags = JsonSerializer.Deserialize<List<string>>(reader.GetString(6)),
159-
Rating = new RatingInfo { Score = (int)reader.GetDouble(7), Count = reader.GetInt32(8) }
163+
Active = reader.GetBoolean(5),
164+
Tags = JsonSerializer.Deserialize<List<string>>(reader.GetString(6)),
165+
Rating = new RatingInfo
166+
{
167+
Score = (int)reader.GetDouble(7),
168+
Count = reader.GetInt32(8)
169+
}
160170
};
161171
}
162172

frameworks/genhttp/genhttp.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
<PackageReference Include="GenHTTP.Modules.Layouting" Version="10.5.0" />
1414
<PackageReference Include="GenHTTP.Modules.Webservices" Version="10.5.0" />
1515
<PackageReference Include="GenHTTP.Modules.Websockets" Version="10.5.0" />
16-
<PackageReference Include="GenHTTP.Modules.Caching" Version="10.5.0" />
16+
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="10.0.7" />
17+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="10.0.7" />
1718
<PackageReference Include="Npgsql" Version="10.0.2" />
1819
</ItemGroup>
1920
</Project>

site/data/crud-4096.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@
4141
{
4242
"framework": "genhttp",
4343
"language": "C#",
44-
"rps": 503585,
45-
"avg_latency": "7.78ms",
46-
"p99_latency": "31.70ms",
47-
"cpu": "3839.5%",
48-
"memory": "1.5GiB",
44+
"rps": 484308,
45+
"avg_latency": "8.01ms",
46+
"p99_latency": "28.30ms",
47+
"cpu": "4815.4%",
48+
"memory": "1.1GiB",
4949
"connections": 4096,
5050
"threads": 64,
5151
"duration": "5s",
5252
"pipeline": 1,
53-
"bandwidth": "199.83MB/s",
54-
"input_bw": "43.22MB/s",
55-
"reconnects": 35864,
56-
"status_2xx": 7553783,
53+
"bandwidth": "184.45MB/s",
54+
"input_bw": "41.57MB/s",
55+
"reconnects": 34389,
56+
"status_2xx": 7264631,
5757
"status_3xx": 0,
5858
"status_4xx": 0,
5959
"status_5xx": 0

site/data/current.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"docker": "29.3.0",
1111
"docker_runtime": "runc",
1212
"governor": "performance",
13-
"commit": "30f0c0b5",
13+
"commit": "b506385d",
1414
"tcp": {
1515
"lo_mtu": "1500",
1616
"congestion": "cubic",

0 commit comments

Comments
 (0)