Django Cache (Redis) refresh when DB update or POST success

84 Views Asked by At

Is there a more efficient approach, in addition to relying solely on signals, to promptly update the Django cache in Redis whenever a modification occurs in the database?

I have explored the use of signals, but I discovered that they clear the entire cache, which is not a practical solution. I am in search of an alternative method that enables more precise cache updates based on specific changes in the database.

Here's an example code for reference. Whenever an instance is updated or created, I would like to refresh the Django cache in Redis:

class Employee(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    code = models.CharField(unique=True, default='0', max_length=100)
    created_date = models.DateField(null=True, blank=True)

Redis cache example:

1688976797.822442 [0 127.0.0.1:51844] "SET" "api_:1:views.decorators.cache.cache_header..00091ef3e65048c2d6d75837833ba349.en-us.UTC" "\x80\x05\x95\x12\x00\x00\x00\x00\x00\x00\x00]\x94\x8c\x0bHTTP_ACCEPT\x94a." "EX" "7200"
0

There are 0 best solutions below