Поделиться через


Using Profiles in a Web Farm Scenario

In a Web farm scenario, each server provides its own local profile cache. The local caches that store the UserObject profile will become unsynchronized when the profile is changed on one of the servers. For the outdated servers to recognize that the user's profile has changed, a "last updated" time stamp must be set in the user's profile (in the Profiles store). In addition, if cookies are enabled on the user's browser, the time stamp is set in a cookie when the profile is created or updated by calling the CreateProfile or UpdateProfile method.

When a user visits a server in the Web farm, the time stamp in the profile in each local cache is checked against the time stamp in the cookie. If the time stamps are not equal, the profile is retrieved from the Profiles store, bypassing the local cache. If cookies are not enabled, the cache is always bypassed, because there is no reliable way to track the last update.

The Profiles System caches the lookup of address profiles. However, organization profile lookups bypass the cache on an as-needed basis, depending on the probability of the data in the profile changing.

The EnsureUpToDateUserProfile function in the following example demonstrates how you can make sure that a user's profile is up to date in a Web farm scenario.

private void EnsureUpToDateUserProfile(Profile prof)
{
    string cookieTimeStamp = "";
    string cacheTimeStamp = "";

    if (Request.Browser.Capabilities["Cookies"].ToString() == "true")
    {
        // Cookies are supported.

        if (Request.Cookies["UserProfileTimeStamp"] != null)
        {
            cookieTimeStamp = Request.Cookies["UserProfileTimeStamp"].Value.ToString();
        }

        if (prof.Properties["ProfileSystem.date_last_changed"] != null)
        {
            cacheTimeStamp = prof.Properties["ProfileSystem.date_last_changed"].Value.ToString();
        }

        if (cacheTimeStamp != cookieTimeStamp)
        {
            // Time stamps are not the same.

            if (prof == null)
            {
                // There is no loaded profile.
                // Delete the cookie for a deleted profile. 
                Response.Cookies["UserProfileTimeStamp"].Expires = System.DateTime.Now.AddDays(-1);
            }
            else
            {
                // There is a loaded profile.
                // Force an update of the profile from the Profiles System.
                prof.Refresh();

                // Update the cookie.    
                Response.Cookies["UserProfileTimeStamp"].Value = prof.Properties["ProfileSystem.date_last_changed"].Value.ToString();
            }
        }
    }
    else
    {
        // Cookies are not supported.
        // Force an update of the profile from the Profiles System.
        prof.Refresh();
    }
}

See Also

Other Resources

Developing with the Profiles System

How to Refresh a Profile Instance