OrchardCMS / Orchard

Orchard is a free, open source, community-focused Content Management System built on the ASP.NET MVC platform.
https://orchardproject.net
BSD 3-Clause "New" or "Revised" License
2.37k stars 1.12k forks source link

GetUser is called several times per pageload #8709

Closed MatteoPiovanelli-Laser closed 10 months ago

MatteoPiovanelli-Laser commented 1 year ago

https://github.com/OrchardCMS/Orchard/blob/bdba35c704bd07b431fae901f0376856c9a743c2/src/Orchard.Web/Modules/Orchard.Users/Services/MembershipService.cs#L134

This method is called a few times per pageload, because it's called by the provider for WorkContext.CurrentUser (when a User is authenticated).

We could cache the query inside this (per request, and my guts say only if a user is found) to save a few more queries being sent to the database.

sebastienros commented 1 year ago

A local cache should work fine. (in the scoped service)

MatteoPiovanelli-Laser commented 1 year ago

I did more tests on this today with fresher eyes.

Looks like there is already a memorization happening per-workcontext, and the additional invocations I'm seeing are caused by filters and such having to generate a new WorkContext (e.g. OutputCacheFilter has to do this). When a new WorkContext is instanced, its underlying providers (specifically the one for CurrentUser) are also reinstanced (that can make sense to me), so a local cache (on a simple level, a dictionary) gets reset.

That being the case I think I'd close this and leave things as they are.