caching: Add configuration class for post-migration cache flushing.

- To avoid redefining migrate manage command is added new application
  configuration class which emit post_migration signal. This signal
  require models module inside application and defined AppConfig
  Instance as signal sender.  Documentation here:
  https://docs.djangoproject.com/en/1.8/ref/signals/#post-migrate.
- Add AppConf subclass to __init__ zerver app file to make apllication
  load it by default.

Fixes #1084.
This commit is contained in:
K.Kanakhin
2016-10-17 22:11:16 +06:00
committed by Tim Abbott
parent c6f7b786a6
commit 78b1b80987
4 changed files with 28 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
# Load AppConfig app subclass by default on django applications initialization
default_app_config = 'zerver.apps.ZerverConfig'

23
zerver/apps.py Normal file
View File

@@ -0,0 +1,23 @@
from __future__ import print_function
from django.apps import AppConfig
from django.db.models.signals import post_migrate
from django.core.cache import cache
from django.conf import settings
from typing import Any, Dict
import logging
def flush_cache(sender, **kwargs):
# type: (AppConfig, **Any) -> None
logging.info("Clearing memcached cache after migrations")
cache.clear()
class ZerverConfig(AppConfig):
name = "zerver" # type: str
def ready(self):
# type: () -> None
if settings.POST_MIGRATION_CACHE_FLUSHING:
post_migrate.connect(flush_cache, sender=self)

View File

@@ -36,3 +36,5 @@ SAVE_FRONTEND_STACKTRACES = True
EVENT_LOGS_ENABLED = True
SYSTEM_ONLY_REALMS = set() # type: Set[str]
USING_PGROONGA = True
# Flush cache after migration.
POST_MIGRATION_CACHE_FLUSHING = True # type: bool

View File

@@ -178,6 +178,7 @@ DEFAULT_SETTINGS = {'TWITTER_CONSUMER_KEY': '',
'SYSTEM_ONLY_REALMS': {"zulip.com"},
'FIRST_TIME_TOS_TEMPLATE': None,
'USING_PGROONGA': False,
'POST_MIGRATION_CACHE_FLUSHING': False,
}
for setting_name, setting_val in six.iteritems(DEFAULT_SETTINGS):