skip to Main Content

I have created two templates I have the extended template De user (AbsctractUser) and the template (Score) with the field total_score.

From what I could understand about the django cache with redis is that as long as the line is not modified, redis keeps the information in cache.

The total_score field will be updated regularly, like 100 times every 10 minutes if it is added in the AbstractUser template, this one will force redis to regularly update the user’s cache at each page change (view), whereas if I create a second Score template with a OnetoOne relationship, this one will be updated only on the 2 pages concerned or the view loads the Score model.

Or I can put directly the field total_score in my abstract user and update it regularly. But for me I lose the efficiency of redis for the Abstract User model.
I’d like to know if my reasoning is correct…

What I could read is that a OnetoOne is useful to extend an already existing table, but for the creation of a new project it is necessary to avoid the OnetoOne, and to add directly the field in the model.
so I’m in a fog. Thank you in advance.

Example :

better for redis cache (score_user) update frequent updating of the row score_user :

class User(AbstractUser):

    name = CharField(_("Last name of User"), blank=True, max_length=255)
    forename = CharField(_("First name of User"), blank=True, max_length=255)

class Score(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)
    score_user = models.PositiveIntegerField(default=0)

VS

class User(AbstractUser):

    name = CharField(_("Last name of User"), blank=True, max_length=255)
    forename = CharField(_("First name of User"), blank=True, max_length=255)
    score_user = models.PositiveIntegerField(default=0)

2

Answers


  1. Both Ways can work, a few ways to have can be
    1. pass custom key for cache which does not include update_at params you can pass a hash of name and forename

    1. you can invalidate key in post save hook only if these fields update, else ignore
    Login or Signup to reply.
  2. Separate your Score field from user because normalize your database schema.
    Its depend upon on a situation where you want to see both details user and score are must be shown on the same page and you need some performance then go with abstract user. Make 1 query and you get everything.

    You already mentioned that using abstract user degrade the performance then go with onetoone relationship.

    And you can also make separate hash or string of score in redis to updating the score field. Don’t update user session hash.

    But denormalization increases the performance.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search