11using System . Text . Json ;
22using GenHTTP . Api . Content ;
3- using GenHTTP . Api . Content . Caching ;
43using GenHTTP . Api . Protocol ;
54using genhttp . Infrastructure ;
6- using GenHTTP . Modules . Caching ;
75using GenHTTP . Modules . IO ;
86using GenHTTP . Modules . Reflection ;
97using GenHTTP . Modules . Webservices ;
10- using StringContent = GenHTTP . Modules . IO . Strings . StringContent ;
8+ using Microsoft . Extensions . Caching . Memory ;
119
1210namespace genhttp . Tests ;
1311
1412public 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
0 commit comments