November Happy Hour will be moved to Thursday December 5th.

CustomerContact not updated in cache

Vote:
 

Hi, 

When I'm registering new users (as CustomerContacts), I see that they are registered with the correct information (firstname, lastname, phone, email, etc) in the Commerce Manager. But if I view the user on the applications MyPage, the only information registred is the email address. If I do one of the following things and access MyPage again, the correct information is displayed: 

- Restart application
- Click on the Contact in the Commerce Manager
- Log out and log in over again

It seems like the cache is not updated when I register the customer information, and the empty customerContact object (containing only email address) is returned from cache. Shouldn't customerContact.SaveChanges() automatically update the cache?

In order to verify that caching is the issue, I call CustomersCache.Clear(); directly after registering the user. Doing this displays the correct probile information on the applications MyPage since it has to retrieve the CustomerContact directly from the database. But I can't clear the entire CustomersCache everytime someone registers, that's going to affect the performance of the whole application. 

Does anyone know what might be causing this issue?

Thanks in advance!

Karoline

#90974
Sep 23, 2014 12:33
Vote:
 

Hi,

If I understand your question correctly (you're using code to create CustomerContact whenever an user register on front-end site) , I believe when creating a CustomerContact, you can call this to clear the cache for that contact specifically:

CustomersCache.RemoveFromCache(CustomerContact.ClassName, string.Empty, "GetContactById", contactId.ToString())

before getting back the customer contact

change "GetContactById" by the method you're using.

Regards.

/Q

#90981
Sep 23, 2014 13:05
Vote:
 

Hi, 

I've tried to replace my call to CustomersCache.Clear() with both of these, but it does not seem to clear the correct cache as I still receive the basic CustomerContact object without any profile information:

CustomersCache.RemoveFromCache(CustomerContact.ClassName, string.Empty, "GetContactById", contact.PrimaryKeyId.ToString())
CustomersCache.RemoveFromCache("Contact", null, null, contact.PrimaryKeyId.ToString());

Can you confirm that the contactId passed into the method should be contact.PrimaryKeyId.Value?

Karoline

#90982
Sep 23, 2014 13:12
Vote:
 

If you're using GetContactById then, yes - it should be a guid.

Note

"change "GetContactById" by the method you're using." 

If this does not work then I think I would need to take a look at the code you're using to create contact then get it back.

Regards.

/Q

#90983
Sep 23, 2014 13:21
Vote:
 

I'm using CustomerContext.Current.CurrentContact, which as far as I can see is using the "GetContactById" method. 

I've created some example code where this problem occurs: 

     RegistrationForm registrationForm = new RegistrationForm()
            {
                Address = "Testveien 1",
                City = "Oslo",
                Email = "test@test.no",
                Alias = "Alias",
                EmailContactConsented = true,
                Password = "test123!",
                FirstName = "Testnavn",
                LastName = "TestSlutt",
                MobilePhone = "99999999",
                PostCode = "0555"
            };

            MembershipCreateStatus status;
            MembershipUser user = Membership.CreateUser(registrationForm.Email, registrationForm.Password, registrationForm.Email, null, null, true, Guid.NewGuid(), out status);
            if (user == null)
                return false;

            CustomerContact customerContact = CustomerContact.CreateInstance(user);
            customerContact = customerContact.SaveChanges();

            SecurityContext.Current.AssignUserToGlobalRole(user, AppRoles.EveryoneRole);
            SecurityContext.Current.AssignUserToGlobalRole(user, AppRoles.RegisteredRole);

            customerContact.FullName = string.Join(" ", registrationForm.FirstName, registrationForm.LastName);
            customerContact.FirstName = registrationForm.FirstName;
            customerContact.LastName = registrationForm.LastName;
            customerContact.Email = registrationForm.Email;
            customerContact[Constants.Metadata.Customer.MobilePhone] = registrationForm.MobilePhone;
            customerContact[Constants.Metadata.Customer.EmailContactConsented] = registrationForm.EmailContactConsented;
            customerContact[Constants.Metadata.Customer.Alias] = registrationForm.Alias;
            customerContact.RegistrationSource = "Test";
            customerContact.CustomerGroup = "Customer";
            customerContact = customerContact.SaveChanges();

            bool valid = Membership.ValidateUser(registrationForm.Email, registrationForm.Password);
            if (valid)
                membershipService.CreateAuthenticationCookie(user.UserName, false);

            // If I uncomment the following line, it works...
            //CustomersCache.Clear(); 

As mentioned above, calling CustomerContext.Current.CurrentContact after running this code gives me a CustomerContact object containing only the email and firstname (which is set to the email address). 

Karoline

#90985
Sep 23, 2014 13:46
Vote:
 

Because you're using CurrentContact, it'll be a bit different. CurrentContact is cached by the MembershipUser.ProviderUserKey  instead of contactId :)

/Q

#91009
Sep 24, 2014 6:10
Vote:
 

You are right, I had to use the MembershipUser.ProviderUserKey: 

MapUserKey mapUserKey = new MapUserKey();
string uniqueSuffix = mapUserKey.ToTypedString(user.ProviderUserKey);

CustomersCache.RemoveFromCache("Contact", string.Empty, "GetContactById", uniqueSuffix);

Thank you so much for you help! 

#91014
Sep 24, 2014 10:34
Vote:
 

Karolines solution didn't do it for me but this code did:

var key = CustomersCache.CreateCacheKey("Contact", string.Empty, "GetContactById", "String:" + username);
HttpRuntime.Cache.Remove(key);

#115173
Edited, Jan 12, 2015 14:03
Vote:
 

sorry about the spam but i something clearly went wrong with the site when i posted my last reply...

#115247
Jan 12, 2015 14:31
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.