2

When developing your project in Django with READ-COMITTED level, I think cache.delete can lead to race condition and django's signals won't help much.

T1                          T2
                            cache.delete
cache.get (not found)
read from database
cache.set (old value again)
                            commit
                            cache.get (old value)

How can I ensure that cache invalidation is only done at the moment of transaction commit?

hurturk
  • 5,214
  • 24
  • 41

1 Answers1

1

You should only delete from cache when the transaction has been committed. To ensure your transaction has been committed (for example, in case you're using django.middleware.transaction.TransactionMiddleware to commit upon every request), you can force a commit using commit_on_success:

from django.db import transaction
with transaction.commit_on_success():
    # ... do db stuff ...

cache.delete('key')
augustomen
  • 8,977
  • 3
  • 43
  • 63
  • Well, that sounds like commit-continue, which wouldn't let me to rollback in future part of the runtime. It seems, I either need something like version control or cache management middleware. – hurturk Sep 11 '13 at 15:29
  • It would appear you need to define what to do with the cached value in case you do a rollback. – augustomen Sep 12 '13 at 12:26