Project Description

Tags are a way to categorize cache records.
When you save a cache, you can set a list of tags to apply for this record.
Then you will be able to invalidate all cache records tagged with a given tag (or tags).

from django.db import transaction
from django_cache_dependencies import cache
from django_cache_dependencies import cache_transaction
with cache.transaction, transaction.commit_on_success():
# ... some code
# Changes a some data
cache.invalidate_tags('tag1', 'tag2', 'tag3')
# ... some long code
# Another concurrent process/thread can obtain old data at this time,
# after changes but before commit, and create cache with old data,
# if isolation level is not "Read uncommitted".
# Otherwise, if isolation level is "Read uncommitted", and transaction will rollback,
# the concurrent and current process/thread can creates cache with dirty data.

Transaction handler as decorator:

from django.db import transaction
from django_cache_dependencies import cache
from django_cache_dependencies.decorators import cache_transaction
@cache.transaction
@transaction.commit_on_success():
def some_view(request):
# ... some code
cache.invalidate_tags('tag1', 'tag2', 'tag3')
# ... some long code
# Another concurrent process/thread can obtain old data at this time,
# after changes but before commit, and create cache with old data,
# if isolation level is not "Read uncommitted".
# Otherwise, if isolation level is "Read uncommitted", and transaction will rollback,
# the concurrent and current process/thread can creates cache with dirty data.
#
# We can even invalidate cache before data changes,
# by signals django.db.models.signals.pre_save()
# or django.db.models.signals.pre_delete(), and don't worry.