问题
a) When current user accesses Profile object for the first time, does Asp.Net retrieve a complete profile for that user or are profile properties retrieved one at the time as they are called?
b) In any case, is profile data for current user retrieved from DB each time it is called or is it retrieved just once and then saved for the duration of current request?
thanx
回答1:
You don't specify if this is a Website Project or a Web Application Project; when it comes to profiles there is a big difference as they are not implemented out of the box for the Web Application Project template:
http://codersbarn.com/post/2008/07/10/ASPNET-PayPal-Subscriptions-IPN.aspx
http://leedumond.com/blog/asp-net-profiles-in-web-application-projects/
Have you actually implemented it yet or are you just in the planning stage? If the latter, then the above links should provide some valuable info. As regards the caching issue, I would go with Dave's advice.
回答2:
If you want to save a database call, you can use a utility method to cache the profile or you can implement your own custom MembershipProvider which handles the caching.
The utility method is probably the simplest solution in this case unless you have other functionality you want to implement which would be served by implementing a custom MembershipProvider.
You can refer to this link for more details: How can I access UserId in ASP.NET Membership without using Membership.GetUser()?
Here's an example of a utility method to do caching. You can pass a slidingMinutesToExpire to make it expire from the cache after some duration of time to avoid consuming excess server memory if you have many users.
public static void AddToCache(string key, Object value, int slidingMinutesToExpire)
{
if (slidingMinutesToExpire == 0)
{
HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.NotRemovable, null);
}
else
{
HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(slidingMinutesToExpire), System.Web.Caching.CacheItemPriority.NotRemovable, null);
}
}
来源:https://stackoverflow.com/questions/1744256/is-profile-data-for-current-user-retrieved-just-once