'Cache when user open multiple tabs Redis + Django

I have a database that is updated once a month, so I use redis to save cache

Everything works fine for me, I add user name after a key like this:

nhan_vien=CustomUser.objects.get(id=request.user.id).username
contract_number = cache.get("contract_number" + nhan_vien)

my settings:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        'TIMEOUT': 60 * 60 * 8,
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
        
    }
}

A user cannot access cache from another users.

If a user open 2 tabs, Tab1: he/she has contract_number from cache.get("contract_number" + nhan_vien). He open new tab2, then he/she has new contract_number from cache.get("contract_number" + nhan_vien) My issue when he/she save data in tab1, my website gets value from new contract_number (in tab2) to save data while the content belongs to contract_number in tab1. If I set Timeout, after this time no record contract is appear and then they cannot save data.

I have 3 questions:

  1. How can I handle this issue by redis + django. How can I dont allow user opens a new tab while they access my website?
  2. If I cannot solve this by only redis + django, Is there any other solutions?
  3. I used to read in Stackoverflow https://stackoverflow.com/questions/7763115/django-passing-data-between-views how can pass data between views. Solution 6 is about REST API's, can I use REST API's to handle my issue? Sorry if this is not a good question because I dont know about REST API.

Thanks for reading and answers my question.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source