Skip to content

purge sitetree cache on change #2764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ default: help
.state/db-migrated:
# Call migrate target
make migrate
docker compose run --rm web ./manage.py createcachetable

# Mark the state so we don't rebuild this needlessly.
mkdir -p .state && touch .state/db-migrated

.state/db-initialized: .state/docker-build-web .state/db-migrated
# Ensure cache table
docker compose run --rm web ./manage.py createcachetable
# Load all fixtures
docker compose run --rm web ./manage.py loaddata fixtures/*.json

Expand Down
8 changes: 8 additions & 0 deletions pydotorg/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.apps import AppConfig


class PyDotOrgConfig(AppConfig):
name = "pydotorg"

def ready(self):
import pydotorg.signals # noqa: F401
1 change: 1 addition & 0 deletions pydotorg/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
'django_countries',
'sorl.thumbnail',

'pydotorg',
'banners',
'blogs',
'boxes',
Expand Down
4 changes: 2 additions & 2 deletions pydotorg/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@

CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'pythondotorg-local-cache',
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'django_cache_table',
}
}

Expand Down
14 changes: 14 additions & 0 deletions pydotorg/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.db.models.signals import m2m_changed, post_delete, post_save
from django.dispatch import receiver

from sitetree.models import Tree, TreeItem
from sitetree.sitetreeapp import get_sitetree

@receiver(post_save, sender=Tree)
@receiver(post_save, sender=TreeItem)
@receiver(post_delete, sender=TreeItem)
@receiver(m2m_changed, sender=TreeItem.access_permissions)
def purge_sitetree_cache(sender, instance, **kwargs):
cache_ = get_sitetree().cache
cache_.empty()
cache_.reset()
8 changes: 7 additions & 1 deletion sponsors/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django import forms
from django.conf import settings
from django.core.mail import EmailMessage
from django.test import TestCase
from django.test import TestCase, override_settings
from django.utils import timezone

from ..models import (
Expand Down Expand Up @@ -327,6 +327,12 @@ def test_singleton_object_cannot_be_deleted(self):

self.assertIn("Singleton object cannot be delete. Try updating it instead.", str(context.exception))

@override_settings(CACHES={
"default": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
"LOCATION": "pythondotorg-local-cache",
}
})
def test_current_year_is_cached(self):
# cleans cached from previous test runs
cache.clear()
Expand Down