11using Microsoft . EntityFrameworkCore ;
2+ using Microsoft . Extensions . Caching . Memory ;
3+ using Microsoft . Extensions . Logging ;
24using PocketDDD . Server . DB ;
3- using PocketDDD . Server . Model . DTOs ;
4- using System ;
5- using System . Collections . Generic ;
6- using System . Linq ;
7- using System . Text ;
8- using System . Threading . Tasks ;
95using PocketDDD . Shared . API . RequestDTOs ;
106using PocketDDD . Shared . API . ResponseDTOs ;
117
128namespace PocketDDD . Server . Services ;
13- public class EventDataService
9+
10+ public class EventDataService ( PocketDDDContext dbContext , IMemoryCache memoryCache , ILogger < EventDataService > logger )
1411{
15- private readonly PocketDDDContext dbContext ;
12+ private const string FetchLatestEventDataCacheKey = nameof ( FetchLatestEventData ) ;
13+ private const string FetchCurrentEventDetailIdCacheKey = nameof ( FetchCurrentEventDetailId ) ;
1614
17- public EventDataService ( PocketDDDContext dbContext )
15+ public async Task < int > FetchCurrentEventDetailId ( )
1816 {
19- this . dbContext = dbContext ;
17+ if ( memoryCache . TryGetValue < int ? > ( FetchCurrentEventDetailIdCacheKey , out var cachedEventDetailId ) )
18+ return cachedEventDetailId ! . Value ;
19+
20+ cachedEventDetailId = await dbContext . EventDetail . MaxAsync ( e => e . Id ) ;
21+ memoryCache . Set ( FetchCurrentEventDetailIdCacheKey , cachedEventDetailId , TimeSpan . FromMinutes ( 10 ) ) ;
22+
23+ return cachedEventDetailId . Value ;
2024 }
2125
22- public async Task < EventDataResponseDTO ? > FetchLatestEventData ( EventDataUpdateRequestDTO requestDTO )
26+ public async Task < EventDataResponseDTO ? > FetchLatestEventData ( EventDataUpdateRequestDTO requestDto )
2327 {
24- var eventDetails = await dbContext . EventDetail
25- . Include ( x => x . TimeSlots )
26- . Include ( x => x . Tracks )
27- . Include ( x => x . Sessions )
28- . SingleAsync ( x => x . Id == 1 ) ;
29-
30- if ( requestDTO . Version == eventDetails ! . Version )
28+ if ( memoryCache . TryGetValue < EventDataResponseDTO ? > ( FetchLatestEventDataCacheKey , out var latestEventData ) )
29+ {
30+ logger . LogDebug ( "Retrieved latest event data from the cache {eventData}" , latestEventData ) ;
31+ }
32+ else
33+ {
34+ logger . LogDebug ( "No event data in the cache, retrieving from the db" ) ;
35+ latestEventData = await dbContext . EventDetail
36+ . AsNoTracking ( )
37+ . AsSingleQuery ( )
38+ . OrderBy ( e => e . Id )
39+ . Select ( eventDetails => new EventDataResponseDTO
40+ {
41+ Id = eventDetails . Id ,
42+ Version = eventDetails . Version ,
43+ TimeSlots = eventDetails . TimeSlots . Select ( ts => new TimeSlotDTO
44+ {
45+ Id = ts . Id ,
46+ Info = ts . Info ,
47+ From = ts . From ,
48+ To = ts . To
49+ } ) . ToList ( ) ,
50+ Tracks = eventDetails . Tracks . Select ( t => new TrackDTO
51+ {
52+ Id = t . Id ,
53+ Name = t . Name ,
54+ RoomName = t . RoomName
55+ } ) . ToList ( ) ,
56+ Sessions = eventDetails . Sessions . Select ( s => new SessionDTO
57+ {
58+ Id = s . Id ,
59+ Title = s . Title ,
60+ ShortDescription = s . ShortDescription ,
61+ FullDescription = s . FullDescription ,
62+ Speaker = s . Speaker ,
63+ TimeSlotId = s . TimeSlot . Id ,
64+ TrackId = s . Track . Id
65+ } ) . ToList ( )
66+ } )
67+ . LastAsync ( ) ;
68+
69+ memoryCache . Set ( FetchLatestEventDataCacheKey , latestEventData , TimeSpan . FromMinutes ( 0.5 ) ) ;
70+ logger . LogDebug ( "Updated the latest event data in the cache {eventData}" , latestEventData ) ;
71+ }
72+
73+ if ( requestDto . Version >= latestEventData ! . Version )
3174 return null ;
3275
33- var dtoResponse = new EventDataResponseDTO
34- {
35- Version = eventDetails . Version ,
36- TimeSlots = eventDetails . TimeSlots . Select ( ts => new TimeSlotDTO
37- {
38- Id = ts . Id ,
39- Info = ts . Info ,
40- From = ts . From ,
41- To = ts . To
42- } ) . ToList ( ) ,
43- Tracks = eventDetails . Tracks . Select ( t => new TrackDTO
44- {
45- Id = t . Id ,
46- Name = t . Name ,
47- RoomName = t . RoomName
48- } ) . ToList ( ) ,
49- Sessions = eventDetails . Sessions . Select ( s => new SessionDTO
50- {
51- Id = s . Id ,
52- Title = s . Title ,
53- ShortDescription = s . ShortDescription ,
54- FullDescription = s . FullDescription ,
55- Speaker = s . Speaker ,
56- TimeSlotId = s . TimeSlot . Id ,
57- TrackId = s . Track . Id
58- } ) . ToList ( )
59- } ;
60-
61- return dtoResponse ;
76+ return latestEventData ;
6277 }
63- }
78+ }
0 commit comments