Skip to content

Commit

Permalink
Change points where invalidate /api/config api cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
wlorenzetti committed Oct 25, 2023
1 parent 46ffd0e commit 172e0ce
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 10 deletions.
8 changes: 7 additions & 1 deletion g3w-admin/caching/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from caching.models import G3WCachingLayer
from caching.utils import get_config

import logging

@receiver(load_layer_actions)
def caching_layer_action(sender, **kwargs):
"""
Expand Down Expand Up @@ -133,4 +135,8 @@ def invalid_prj_cache(**kwargs):
"""Invalid the possible qdjango project cache"""

layer = Layer.objects.get(pk=kwargs["instance"].layer_id)
layer.project.invalidate_cache()
layer.project.invalidate_cache()
logging.getLogger("g3wadmin.debug").debug(
f"Qdjango project /api/config invalidate on update/delete of a caching layer state: "
f"{layer.project}"
)
35 changes: 35 additions & 0 deletions g3w-admin/core/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from core.signals import execute_search_on_models
from django.dispatch import receiver
from django.contrib.postgres.search import SearchQuery, SearchVector
from django.db.models.signals import post_save, pre_delete, pre_save
from .models import GroupProjectPanoramic, Group, MacroGroup
from .searches import GroupSearch, MacroGroupSearch

import logging

def check_overviewmap_project(sender, **kwargs):

Expand Down Expand Up @@ -33,3 +35,36 @@ def execute_search(sender, request, search_text, **kwargs):
MacroGroupSearch(search_text, user=request.user),
]


@receiver(post_save, sender=GroupProjectPanoramic)
@receiver(pre_delete, sender=GroupProjectPanoramic)
def invalid_prj_cache(**kwargs):
"""Invalid the possible qdjango project cache"""

from qdjango.models import Project
project = Project.objects.get(pk=kwargs['instance'].project_id)
project.invalidate_cache()
logging.getLogger("g3wadmin.debug").debug(
f"Qdjango project /api/config invalidate on create/delete of a GroupProjectPanoramic: "
f"{project}"
)

@receiver(pre_save, sender=GroupProjectPanoramic)
def invalid_prj_cache_on_update(**kwargs):
""" Invalidate /api/config cache on update of GroupProjectPanoramic instance """

# Get current value if pk is set: so only on update
if not kwargs['instance'].pk:
return

from qdjango.models import Project
gpp = kwargs['sender'].objects.get(pk=kwargs['instance'].pk)
project = Project.objects.get(pk=gpp.project_id)
project.invalidate_cache()
logging.getLogger("g3wadmin.debug").debug(
f"Qdjango project /api/config invalidate on update of a GroupProjectPanoramic: "
f"{project}"
)



4 changes: 4 additions & 0 deletions g3w-admin/editing/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,8 @@ def invalid_prj_cache(**kwargs):

layer = Layer.objects.get(pk=kwargs["instance"].layer_id)
layer.project.invalidate_cache()
logging.getLogger("g3wadmin.debug").debug(
f"Qdjango project /api/config invalidate on update/delete of a editing layer state: "
f"{layer.project}"
)

9 changes: 5 additions & 4 deletions g3w-admin/qdjango/models/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,10 +531,11 @@ def invalidate_cache(self, user=None):
# Invalidate every cache for every user
users = User.objects.all()
for user in users:
logger.debug(
f"[CACHING /api/config]: Ivalidate key {pre_key}_{str(user.pk)}"
)
cache.delete(f"{pre_key}_{str(user.pk)}")
d = cache.delete(f"{pre_key}_{str(user.pk)}")
if d:
logger.debug(
f"[CACHING /api/config]: Ivalidate key {pre_key}_{str(user.pk)}"
)
else:

# Invalidate only for user
Expand Down
27 changes: 23 additions & 4 deletions g3w-admin/qdjango/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def delete_project_file(sender, **kwargs):
logger.error(e)

instance.invalidate_cache()
logging.getLogger("g3wadmin.debug").debug(
f"Qdjango project /api/config invalidate cache after delete it: {instance}"
)

# delete ProjectMapUrlAlias related instance
ProjectMapUrlAlias.objects.filter(
Expand All @@ -59,7 +62,11 @@ def delete_cache(sender, **kwargs):
Perform deleting of key caches for getprojectsettings response.
"""

kwargs["instance"].invalidate_cache()
instance = kwargs["instance"]
instance.invalidate_cache()
logging.getLogger("g3wadmin.debug").debug(
f"Qdjango project /api/config invalidate cache after save it: {instance}"
)


@receiver(post_delete, sender=Layer)
Expand Down Expand Up @@ -101,6 +108,13 @@ def remove_parent_project_from_cache(sender, **kwargs):
"QGIS Server parent project touched to invalidate cache: %s" % path
)

# Invalidate /api/config
l.project.invalidate_cache()
logging.getLogger("g3wadmin.debug").debug(
f"Parent qdjango project /api/config invalidate cache: {l.project}"
)



@receiver(post_save, sender=Layer)
def update_widget(sender, **kwargs):
Expand All @@ -122,9 +136,6 @@ def update_widget(sender, **kwargs):
widget.datasource = layer.datasource
widget.save()

# invalidate project cache
layer.project.invalidate_cache()


@receiver(user_logged_out)
def delete_session_token_filter(sender, **kwargs):
Expand Down Expand Up @@ -251,12 +262,20 @@ def invalid_prj_cache(**kwargs):
"""Invalid the possible qdjango project cache"""

kwargs['instance'].constraint.layer.project.invalidate_cache()
logging.getLogger("g3wadmin.debug").debug(
f"Parent qdjango project /api/config invalidate on create/update/delete of a layer constraint: "
f"{kwargs['instance'].constraint.layer.project}"
)

@receiver(post_save, sender=ColumnAcl)
@receiver(pre_delete, sender=ColumnAcl)
def invalid_prj_cache(**kwargs):
"""Invalid the possible qdjango project cache"""

kwargs['instance'].layer.project.invalidate_cache()
logging.getLogger("g3wadmin.debug").debug(
f"Parent qdjango project /api/config invalidate on create/update/delete of a layer columnacl: "
f"{kwargs['instance'].layer.project}"
)


29 changes: 29 additions & 0 deletions g3w-admin/qdjango/views/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,19 @@ def get_context_data(self, **kwargs):
def get_success_url(self):
return None

def form_valid(self, form):

ret = super().form_valid(form)

# Invalidate project cache
self.project.invalidate_cache()
logging.getLogger("g3wadmin.debug").debug(
f"Qdjango project /api/config invalidate cache after update layer widget: {self.project}"
)

return ret



class QdjangoLayerWidgetsView(G3WGroupViewMixin, QdjangoProjectViewMixin, QdjangoLayerViewMixin, ListView):
"""
Expand Down Expand Up @@ -725,6 +738,18 @@ class QdjangoLayerWidgetDeleteView(G3WAjaxDeleteViewMixin, SingleObjectMixin, Vi
def dispatch(self, *args, **kwargs):
return super(QdjangoLayerWidgetDeleteView, self).dispatch(*args, **kwargs)

def post(self, request, *args, **kwargs):

# Invalidate /api/config project cache
for l in self.get_object().layers.all():
l.project.invalidate_cache()
logging.getLogger("g3wadmin.debug").debug(
f"Qdjango project /api/config invalidate cache after delate layer widget: {l.project}"
)

res = super().post(request, *args, **kwargs)
return res


class QdjangoLinkWidget2LayerView(G3WRequestViewMixin, G3WGroupViewMixin, QdjangoProjectViewMixin, QdjangoLayerViewMixin, View):
"""
Expand Down Expand Up @@ -854,6 +879,10 @@ def form_valid(self, form):
# invalidate project cache
if len(to_remove) > 0 or len(to_add) > 0:
self.layer.project.invalidate_cache()
logging.getLogger("g3wadmin.debug").debug(
f"Qdjango project /api/config invalidate cache after update of layer filter-by-user: "
f"{self.layer.project.project}"
)

return super().form_valid(form)

Expand Down
6 changes: 5 additions & 1 deletion g3w-admin/qplotly/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,8 @@ def invalid_prj_cache(**kwargs):
"""Invalid the possible qdjango project cache"""

for layer in kwargs['instance'].layers.all():
layer.project.invalidate_cache()
layer.project.invalidate_cache()
logging.getLogger("g3wadmin.debug").debug(
f"Qdjango project /api/config invalidate cache after create/update/delete of layer qplotly widget: "
f"{layer.project}"
)

0 comments on commit 172e0ce

Please sign in to comment.