Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/kennyy/bigger event cache #399

Closed

Conversation

tabascq
Copy link
Contributor

@tabascq tabascq commented Apr 20, 2019

No description provided.

Caches lookups from the Events table, since we make a lot of calls to this. This eliminates 5 server roundtrips per page load. The cache expires after 1 minute and is shared among all users and threads.
Caching the results of is player/author/admin, as well as the lookup of PuzzleUsers. Expiry is left at 1 minute so we can make changes to the event and have them take effect rapidly.

These changes, plus the event cache itself, knocked 12-15 SQL requests off of the Puzzles page.
@tabascq tabascq mentioned this pull request Apr 20, 2019
@@ -11,6 +13,11 @@ namespace ServerCore.Helpers
/// </summary>
public static class EventHelper
{
private static ConcurrentDictionary<int, Event> eventCacheByInt = new ConcurrentDictionary<int, Event>();
private static ConcurrentDictionary<string, Event> eventCacheByString = new ConcurrentDictionary<string, Event>();
private static DateTime expiryTime = DateTime.MinValue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want volatile here.

private static ConcurrentDictionary<int, Event> eventCacheByInt = new ConcurrentDictionary<int, Event>();
private static ConcurrentDictionary<string, Event> eventCacheByString = new ConcurrentDictionary<string, Event>();
private static DateTime expiryTime = DateTime.MinValue;
private static TimeSpan expiryWindow = TimeSpan.FromMinutes(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this should be readonly

}
else
{
if (eventCacheByString.TryGetValue(eventId, out result) || (Int32.TryParse(eventId, out int eventIdInt) && eventCacheByInt.TryGetValue(eventIdInt, out result)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm understanding this correctly, you're caching the Event object, not the full results. Are all the puzzles cached indirectly by being instantiated as we enumerate them off of the event object? The caching I was thinking of doing (and I can still work on it if you'd like) is to cache the computed standings.

return result;
}
}

// first, lookup by UrlString - this is the friendly name
result = await puzzleServerContext.Events.Where(e => e.UrlString == eventId).FirstOrDefaultAsync();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could combine these two queries into one:

Int32.TryParse(eventId, out int eventIdAsInt);  // if 0 is a valid eventId, then need an if here
result = await puzzleServerContext.Events.Where(
    e => e.UrlString == eventId || e.ID == eventIdAsInt)
    .FirstOrDefaultAsync();

@tabascq
Copy link
Contributor Author

tabascq commented Jun 20, 2019

Abandoning; if we cache we should do it right.

@tabascq tabascq closed this Jun 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants