ASP.NET Profile Data Cached?
Does the ASP.NET SQL Profile Provider use caching? I mean if I pull data from the profile for a user will it hit the DB for each request?
http://msdn.microsoft.com/en-us/library/aa478953.aspx describes process as
When called by SqlProfileProvider.GetPropertyValues, aspnet_Profile_GetProperties performs the following actions:
- Queries the aspnet_Applications table to convert the application name input to it into an application ID.
- Queries the aspnet_Users table to convert the user name input to it into a user ID.
- Queries the aspnet_Profile table for the PropertyNames, PropertyValuesString, and PropertyValuesBinary fields for the specified user.
- Updates the user's last activity date in the aspnet_Users table with the current date and time.
When your application runs, ASP.NET creates a ProfileCommon class, which is a dynamically generated class that inherits the ProfileBase class. The dynamic ProfileCommon class includes properties created from the profile property definitions you specify in your application configuration. An instance of this dynamic ProfileCommon class is then set as the value of the Profile property of the current HttpContext and is available to pages in your application.
It appears that the Profile object is attached to the current HttpContext of the request. So, unless you create a new HttpContext, the profile data is loaded and stored here on first request.
EDIT - Allen brought up some good points for some clarification here -> the profile data is queried on each HttpRequest (e.g. when a new HttpContext is created). But within each request, calls to the profile provider for different properties do not keep hitting the DB, only on the first call.
Yes and no
The provider uses "caching" once per request that accesses the profile data. If you don't access profile data during any given request, it isn't queried. However, if you access profile data several times during one request (and don't update it) then its only queried once.
The important takeaway is this: if you access profile data on every request, then asp.net is querying the database once per request for your profile data!.
To specifically answer your question: "If I pull data from the profile for a user will it hit the DB for each [http] request?" Yes, it will hit the database each time.
You could probably build your own provider that does some form of session based caching so you're not always calling the database to lookup your users profile data. Here is a great article on the topic: Optimizing ASP.NET Profiles Performance