mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-03 16:45:18 +00:00
Cache clearing on object deletion or update
This commit is contained in:
parent
aa7ec7f1f7
commit
437254d0de
33
club/apps.py
Normal file
33
club/apps.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
# -*- coding:utf-8 -*
|
||||||
|
#
|
||||||
|
# Copyright 2023
|
||||||
|
# - Maréchal <thgirod@hotmail.com>
|
||||||
|
#
|
||||||
|
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
||||||
|
# http://ae.utbm.fr.
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
# the terms of the GNU General Public License a published by the Free Software
|
||||||
|
# Foundation; either version 3 of the License, or (at your option) any later
|
||||||
|
# version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
# details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with
|
||||||
|
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
||||||
|
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
#
|
||||||
|
#
|
||||||
|
|
||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class ClubConfig(AppConfig):
|
||||||
|
name = "club"
|
||||||
|
verbose_name = "Management of the clubs and of the memberships in these clubs"
|
||||||
|
|
||||||
|
def ready(self):
|
||||||
|
import club.signals
|
52
club/signals.py
Normal file
52
club/signals.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
# -*- coding:utf-8 -*
|
||||||
|
#
|
||||||
|
# Copyright 2023
|
||||||
|
# - Maréchal <thgirod@hotmail.com>
|
||||||
|
#
|
||||||
|
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
||||||
|
# http://ae.utbm.fr.
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
# the terms of the GNU General Public License a published by the Free Software
|
||||||
|
# Foundation; either version 3 of the License, or (at your option) any later
|
||||||
|
# version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
# details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with
|
||||||
|
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
||||||
|
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
#
|
||||||
|
#
|
||||||
|
|
||||||
|
from django.core.cache import cache
|
||||||
|
from django.db.models.signals import pre_delete, post_init, pre_save
|
||||||
|
from django.dispatch import receiver
|
||||||
|
|
||||||
|
from club.models import Club, Membership
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(pre_delete, sender=Club, dispatch_uid="clear_cached_club")
|
||||||
|
def clear_cached_club(sender, instance: Club, **_kwargs):
|
||||||
|
"""
|
||||||
|
When a club is deleted, clear the cache of the memberships
|
||||||
|
associated with this club
|
||||||
|
"""
|
||||||
|
for membership in instance.members.ongoing().select_related("user"):
|
||||||
|
cache.delete(f"membership_{instance.id}_{membership.user.id}")
|
||||||
|
cache.delete(f"sith_club_{instance.unix_name}")
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(
|
||||||
|
[pre_save, pre_delete],
|
||||||
|
sender=Membership,
|
||||||
|
dispatch_uid="clear_cached_membership",
|
||||||
|
)
|
||||||
|
def clear_cached_membership(sender, instance: Membership, **_kwargs):
|
||||||
|
"""
|
||||||
|
When the membership of a user is deleted or edited, clear the associated cache
|
||||||
|
"""
|
||||||
|
cache.delete(f"membership_{instance.id}_{instance.user.id}")
|
@ -34,6 +34,7 @@ class SithConfig(AppConfig):
|
|||||||
|
|
||||||
def ready(self):
|
def ready(self):
|
||||||
from forum.models import Forum
|
from forum.models import Forum
|
||||||
|
import core.signals
|
||||||
|
|
||||||
def clear_cached_memberships(**kwargs):
|
def clear_cached_memberships(**kwargs):
|
||||||
Forum._club_memberships = {}
|
Forum._club_memberships = {}
|
||||||
@ -44,4 +45,3 @@ class SithConfig(AppConfig):
|
|||||||
weak=False,
|
weak=False,
|
||||||
dispatch_uid="clear_cached_memberships",
|
dispatch_uid="clear_cached_memberships",
|
||||||
)
|
)
|
||||||
# TODO: there may be a need to add more cache clearing
|
|
||||||
|
54
core/signals.py
Normal file
54
core/signals.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
# -*- coding:utf-8 -*
|
||||||
|
#
|
||||||
|
# Copyright 2023
|
||||||
|
# - Maréchal <thgirod@hotmail.com>
|
||||||
|
#
|
||||||
|
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
||||||
|
# http://ae.utbm.fr.
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
# the terms of the GNU General Public License a published by the Free Software
|
||||||
|
# Foundation; either version 3 of the License, or (at your option) any later
|
||||||
|
# version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
# details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with
|
||||||
|
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
||||||
|
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
#
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
from django.core.cache import cache
|
||||||
|
from django.db.models.signals import pre_delete, pre_save
|
||||||
|
from django.dispatch import receiver
|
||||||
|
|
||||||
|
from club.models import Club, Membership
|
||||||
|
from core.models import Group
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(pre_delete, sender=Club, dispatch_uid="clear_cached_memberships")
|
||||||
|
def clear_cached_memberships(sender, instance: Club, **_kwargs):
|
||||||
|
"""
|
||||||
|
When a club is deleted, clear the cache of the memberships
|
||||||
|
associated with this club
|
||||||
|
"""
|
||||||
|
for membership in instance.members.ongoing():
|
||||||
|
cache.delete(f"membership_{membership.club.id}_{membership.user.id}")
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(
|
||||||
|
pre_delete,
|
||||||
|
sender=Group,
|
||||||
|
dispatch_uid="clear_cached_user_groups",
|
||||||
|
)
|
||||||
|
def clear_cached_user_groups(sender, instance: Group, **_kwargs):
|
||||||
|
"""
|
||||||
|
When the membership of a user is deleted or edited, clear the associated cache
|
||||||
|
"""
|
||||||
|
cache.delete(f"sith_group_{instance.id}")
|
||||||
|
cache.delete(f"sith_group_{instance.name.replace(' ', '_')}")
|
Loading…
x
Reference in New Issue
Block a user