1

Our websites sometimes has around 600 authenticated users trying to register for an event in a timeframe of 5 min. We have a VPS with 1 CPU and 1GB ram. On these moments the site slows down and gives a 502 error.

For that reason I'm using per-site cache with FileBasedCache. This works excellent and stress tests work fine.

But, when people login, they're redirect to their profile. This is the code:

class UserRedirectView(LoginRequiredMixin, RedirectView):

    permanent = False

    def get_redirect_url(self):
        return reverse("users:detail", kwargs={"membership_number": self.request.user.membership_number})

the user is redirect to an url with their membership_number

class UserDetailView(LoginRequiredMixin, DetailView):

    model = User
    slug_field = "membership_number"
    slug_url_kwarg = "membership_number"

Some users are reporting that they are redirected to someone else his/her profile after logging in.

How does this work? How to prevent user specific parts of the website to be cached? e.g. users also see a list of events that are specific to the groups they are in. In other words, every user should see a different list.

Any ideas? Best practices?

A. Nurb
  • 496
  • 1
  • 3
  • 17
  • Have you tried to use template cache in django ? – Thierno Amadou Sow Nov 05 '21 at 08:29
  • are you using @cache_page? – Phteven Nov 05 '21 at 08:50
  • @Phteven I'm not using cache-page but per-site cache trough middleware. – A. Nurb Nov 05 '21 at 08:57
  • @amadousow I would rather not use template cache, because I have loads of templates and I only would like to excluded the pages that use URL parameters. – A. Nurb Nov 05 '21 at 08:57
  • Use the cache alongwith the `vary_on_cookie` decorator. See answer: https://stackoverflow.com/a/35682133/1925257. – xyres Nov 05 '21 at 09:09
  • Are you sure "membership_number" is unique? If so, in detail view, you use User model, is it the built-in Auth User model? as this model has no membership _number field by default..if you can share your models and url so we can understand your case properly – Husam Alhwadi Nov 05 '21 at 09:47

1 Answers1

0

you should be able to vary cache on cookie so that logged in users (assuming cookie based auth) get another cache key.

from django.views.decorators.vary import vary_on_cookie
@vary_on_cookie
def my_view(request):
    pass

https://docs.djangoproject.com/en/dev/topics/cache/#controlling-cache-using-other-headers and https://docs.djangoproject.com/en/dev/topics/cache/#using-vary-headers

Phteven
  • 400
  • 2
  • 7