Skip to content

Commit

Permalink
Add user pk to cache key.
Browse files Browse the repository at this point in the history
  • Loading branch information
wlorenzetti committed Oct 10, 2023
1 parent 6c38ba1 commit a7bb2ce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion g3w-admin/core/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def cache_page(timeout, key_args, key_prefix="", cache_alias=None):
def _decorator(view_func):
@wraps(view_func)
def _wrapped_view(request, *args, **kwargs):
key = f"{key_prefix}{'_'.join([str(kwargs[k]) for k in key_args])}"
key = f"{key_prefix}{'_'.join([str(kwargs[k]) for k in key_args] + [str(request.user.pk)])}"
response = cache.get(key)
if not response:
response = view_func(request, *args, **kwargs)
Expand Down
15 changes: 12 additions & 3 deletions g3w-admin/qdjango/models/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,21 @@ def __getattr__(self, attr):

return super(Project, self).__getattribute__(attr)

def invalidate_cache(self):
def invalidate_cache(self, user=None):
"""Method to invalidate(delete) API REST /api/config"""

# invalidate project cache
key = f"{settings.QDJANGO_PRJ_CACHE_KEY}{self.group.pk}_{'qdjango'}_{self.pk}"
cache.delete(key)
pre_key = f"{settings.QDJANGO_PRJ_CACHE_KEY}{self.group.pk}_{'qdjango'}_{self.pk}"
if user == None:

# Invalidate every cache for every user
users = User.objects.all()
for user in users:
cache.delete(f"{pre_key}_{str(user.pk)}")
else:

# Invalidate only for user
cache.delete(f"{pre_key}_{str(user.pk)}")


post_delete.connect(check_overviewmap_project, sender=Project)
Expand Down

0 comments on commit a7bb2ce

Please sign in to comment.