Try our conversational search powered by Generative AI!

CustomerContact not updated in cache



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!


Sep 23, 2014 12:33


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.



Sep 23, 2014 13:05


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?


Sep 23, 2014 13:12

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


"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.



Sep 23, 2014 13:21

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 = "",
                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...

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). 


Sep 23, 2014 13:46

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


Sep 24, 2014 6:10

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! 

Sep 24, 2014 10:34

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

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

Edited, Jan 12, 2015 14:03

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

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.