Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vouchers losing Django's prefetch_cache on basket loading from cookie #4008

Open
amatmv opened this issue Nov 21, 2022 · 0 comments
Open

Vouchers losing Django's prefetch_cache on basket loading from cookie #4008

amatmv opened this issue Nov 21, 2022 · 0 comments

Comments

@amatmv
Copy link

amatmv commented Nov 21, 2022

Issue Summary

In our implementation of oscar ecommerce we have found that, in some cases under heavy load, many baskets are not able to access its vouchers and apply offers.

Steps to Reproduce

We have extended the BasketMiddleware and overridden the method apply_offers_to_basket to add logic to this process.
So our method looks like this:

def apply_offers_to_basket(self, request, basket):
    # Clean up not applicable vouchers before using applicator
    if basket and basket.id:
        for voucher in basket.vouchers.all():
            can_be_applied, __ = voucher.can_be_applied(user=basket.owner)
            if not can_be_applied:
                basket.vouchers.remove(voucher)

We are facing many exceptions in the basket.voucher.all() part, where Django loses the _prefetched_objects_cache and it cannot access its Vouchers.

Possible solution

We believe that we could fix this issue by prefetching the vouchers in the get_basket method in BasketMiddleware.

num_baskets_merged = 0
manager = Basket.open
cookie_key = self.get_cookie_key(request)
cookie_basket = self.get_cookie_basket(cookie_key, request, manager)

So the fix could look like

manager = Basket.open.prefetch_related('vouchers')

Technical details

  • Python version: 3.9.
  • Django version: 3.2
  • Oscar version: 2.2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant