Try our conversational search powered by Generative AI!

Change CustomerId on cart - Cache not updated

Vote:
 

Hi

I am currently working on a new feature on our B2B shop, that will allow some users to claim ownership of carts created by other users within the same company/organization.
Example on use case: Person A creates a new cart, and saves it as a draft. Person B then takes ownership of that cart, in order to add more products and complete the order.

Our current implementation requires that the CustomerId on the cart matches the ID of the current user.

I have already made the logic, that succesfully updates the CustomerId along with some properties in the "Data" field. I have verified that everything looks corrent in the database after the cart is saved with the new CustomerId.
That means that everything is working for Person B in the above example.

The cart is updated using this EPiServer method.

orderRepository.Save(cart);

My problem is that the cart is still returned when we load carts belonging to Person A, for a periode of time after the update.
I have come to the conclusion, that this is due to the cache inside the OrderRepository.

Carts are loaded using this EpiServer method.

orderRepository.Load<ICart>(userGuid, cartName);

For a period of time, the updated cart is still returned when carts are loaded for Person A.
In that result, the cart still contains the CustomerId of Person A.

Can any of you think of a way to solve this problem?
Is it somehow possible to refresh the cache of Person A, when Person B takes ownership of one of his/her carts?

#209294
Nov 13, 2019 10:59
Vote:
 

Hi Morten

As you found out, the orders are cached by the Customer ID until it expires or it is invalidated.

You can do either of these two options:

  1. Save the updated cart for Person B. Then invalidate the cart for Person A, by removing the cache key from the cache (you will have to build the same cache key and then "remove" it from ISynchronizedObjectInstanceCache).
  2. Save the updated cart for Person B (set the ID to 0 to force inserting a new cart in the database). Then delete the cart for Person A (by calling IOrderRepository.Delete with the previous ID).

Option 1 corresponds to taking ownership of the cart, as you put it.

Option 2 is more like copying the cart and deleting the original.

But the end result would be the same.

#209298
Edited, Nov 13, 2019 11:59
Vote:
 

Hi Stefan

Thank you for the answer
Do you know the format of that cache key, or where to find it?

I actually had something like Option 2 in mind as an alternative solution.

#209299
Nov 13, 2019 12:48
Vote:
 

Generally you can use the Episerver DeveloperTools add-on to inspect local cache items and then derive patterns of cache keys.

I found this example for you (replace the parts with your own values):

EP:DOR:1311c287-0175-4ba2-979c-d7f07aa1ed14:Default:ICart

  • The GUID matches my test Customer ID
  • "Default" matches the name of my regular cart instance (this can also be wishlist or a custom cart name in your solution)
  • "ICart" seems to refer to the order type

Be aware that this is a derived cache key format. Although I doubt it will suddenly change, there is no guarantee that it won't.

#209313
Nov 13, 2019 14:02
Vote:
 

Changing CustomerId is quite unusual thing to do, but it's argubly a bug... I will see if we can do anything about it.

#209322
Nov 13, 2019 14:37
Vote:
 

Stefan, you are my hero of the week!

That cache invalidation was the final piece to my puzzle. Now everything works as intended.

#209396
Nov 14, 2019 10:41
Stefan Holm Olsen - Nov 14, 2019 11:09
You are welcome, Morten. I'm happy it helped you out.
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.