Skip to content
Open
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
2 changes: 2 additions & 0 deletions CACHING.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ SqlSugar automatically generates cache keys based on:
- Query parameters
- Table names

For hot hackathon endpoints, explicitly defined cache keys are used via `.WithCache("...")` (for example `hackathon:details:{id}` and `hackathon:public-list`) to make cache intent easier to understand while preserving table-based invalidation. Public details keys also normalize the route token (`Guid` as `"D"` format, short code lowercased) so equivalent requests map to the same key.

**User-Specific Data**: Queries with `WHERE userId = {userId}` get unique cache keys per user, providing natural isolation.

**Navigation Properties**: Queries using `.Includes()` for navigation properties should **NOT** use `.WithCache()` as SqlSugar's caching doesn't properly serialize/deserialize navigation properties. Load related entities separately or use explicit joins instead.
Expand Down
5 changes: 3 additions & 2 deletions HackOMania.Api/Endpoints/Organizers/Hackathon/Get/Endpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public override void Configure()

public override async Task HandleAsync(Request req, CancellationToken ct)
{
var hackathonCacheKey = $"hackathon:details:{req.HackathonId}";
var hackathon = await sql.Queryable<HackathonEntity>()
.Where(h => h.Id == req.HackathonId)
.WithCache()
.WithCache(hackathonCacheKey)
.FirstAsync(ct);

if (hackathon is null)
Expand All @@ -35,7 +36,7 @@ public override async Task HandleAsync(Request req, CancellationToken ct)

var emailTemplates = await sql.Queryable<HackathonNotificationTemplate>()
.Where(t => t.HackathonId == hackathon.Id)
.WithCache()
.WithCache($"hackathon:details:{hackathon.Id}:notification-templates")
.ToListAsync(ct);
var emailTemplateMap = emailTemplates
.GroupBy(t => t.EventKey, StringComparer.OrdinalIgnoreCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public override async Task HandleAsync(CancellationToken ct)
);
}

var hackathons = await query.WithCache().ToListAsync(ct);
var hackathons = await query.WithCache($"hackathon:organizer-list:{userId.Value}").ToListAsync(ct);
var hackathonIds = hackathons.Select(h => h.Id).ToList();

var templates = await sql.Queryable<HackathonNotificationTemplate>()
.Where(t => hackathonIds.Contains(t.HackathonId))
.WithCache()
.WithCache($"hackathon:organizer-list:{userId.Value}:notification-templates")
.ToListAsync(ct);
var templatesByHackathon = templates
.GroupBy(t => t.HackathonId)
Expand Down
22 changes: 19 additions & 3 deletions HackOMania.Api/Endpoints/Participants/Hackathon/Get/Endpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,28 @@ public override void Configure()

public override async Task HandleAsync(Request req, CancellationToken ct)
{
var requestedToken = (req.HackathonIdOrShortCode ?? string.Empty).Trim();
if (string.IsNullOrWhiteSpace(requestedToken))
{
await Send.NotFoundAsync(ct);
return;
}
var isGuidToken = Guid.TryParse(requestedToken, out var parsedHackathonId);
var normalizedShortCode = requestedToken.ToLower();
var cacheKeySegment = isGuidToken
? parsedHackathonId.ToString("D")
: normalizedShortCode;
var hackathonCacheKey = $"hackathon:public-details:{cacheKeySegment}";
var hackathon = await sql.Queryable<Entities.Hackathon>()
.Where(h =>
h.Id.ToString() == req.HackathonIdOrShortCode
|| h.ShortCode == req.HackathonIdOrShortCode
(isGuidToken && h.Id == parsedHackathonId)
|| (
!isGuidToken
&& h.ShortCode != null
&& SqlFunc.ToLower(h.ShortCode) == normalizedShortCode
)
)
.WithCache()
.WithCache(hackathonCacheKey)
.FirstAsync(ct);

if (hackathon is null || !hackathon.IsPublished)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override async Task HandleAsync(CancellationToken ct)
{
var hackathons = await sql.Queryable<Entities.Hackathon>()
.Where(h => h.IsPublished)
.WithCache()
.WithCache("hackathon:public-list")
.ToListAsync(ct);

await Send.OkAsync(
Expand Down