From 2c408389389a701f9e2e15ae1c23f65108d4a942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 10 Apr 2017 18:58:52 +0200 Subject: [PATCH 1/5] Add real cache support - Fix cache per process issue with a real cache system - Configuration seems too easy... but it seems to work --- provisioning/bootstrap.sh | 2 +- requirements.txt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/provisioning/bootstrap.sh b/provisioning/bootstrap.sh index 269e4f25..e94d3ac4 100644 --- a/provisioning/bootstrap.sh +++ b/provisioning/bootstrap.sh @@ -9,7 +9,7 @@ DBPASSWD="4KZt3nGPLVeWSvtBZPSM3fSzXpzEU4" # Installation de paquets utiles apt-get update && apt-get install -y python3-pip python3-dev python3-venv \ - libmysqlclient-dev libjpeg-dev git redis-server + libmysqlclient-dev libjpeg-dev git redis-server memcached pip install -U pip # Configuration et installation de mysql. Le mot de passe root est le même que diff --git a/requirements.txt b/requirements.txt index ce081588..202f5dda 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,3 +21,4 @@ git+https://git.eleves.ens.fr/cof-geek/django_custommail.git#egg=django_customma ldap3 git+https://github.com/Aureplop/channels.git#egg=channels python-dateutil +python-memcached From ab31c20649601514a24411328021daf6a636a46b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 10 Apr 2017 19:07:19 +0200 Subject: [PATCH 2/5] missing CACHES value... --- cof/settings_dev.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cof/settings_dev.py b/cof/settings_dev.py index 18aadaad..3bc4b807 100644 --- a/cof/settings_dev.py +++ b/cof/settings_dev.py @@ -115,6 +115,15 @@ USE_L10N = True USE_TZ = True +# Cache system +CACHES = { + 'default': { + 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', + 'LOCATION': '127.0.0.1:11211', + } +} + + # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.8/howto/static-files/ From 36771c2c4f6a3c31c0d71e26ce5f2adda8445017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 10 Apr 2017 21:36:00 +0200 Subject: [PATCH 3/5] Use redis for cache. - Cache use db #1 of redis. - Channel layer (of channels) use db #0 of redis. - `settings` try getting redis connection variables from environment. - Drop memcached system --- cof/settings_dev.py | 39 ++++++++++++++++++++++++++++----------- provisioning/bootstrap.sh | 2 +- requirements.txt | 2 +- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/cof/settings_dev.py b/cof/settings_dev.py index 3bc4b807..97b4bf7c 100644 --- a/cof/settings_dev.py +++ b/cof/settings_dev.py @@ -115,15 +115,6 @@ USE_L10N = True USE_TZ = True -# Cache system -CACHES = { - 'default': { - 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', - 'LOCATION': '127.0.0.1:11211', - } -} - - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.8/howto/static-files/ @@ -176,13 +167,39 @@ RECAPTCHA_PUBLIC_KEY = "DUMMY" RECAPTCHA_PRIVATE_KEY = "DUMMY" RECAPTCHA_USE_SSL = True -# Channels settings + +# Redis settings (for cache and channel layers) + +REDIS_HOST = os.environ.get("REDIS_HOST", "localhost") +REDIS_USER = os.environ.get("REDIS_USER", "") +REDIS_PASS = os.environ.get("REDIS_PASS", "") +REDIS_PORT = os.environ.get("REDIS_PORT", "6379") + +REDIS_AUTH = REDIS_USER+":"+REDIS_PASS+"@" if REDIS_USER or REDIS_PASS else '' +REDIS_BASE_URL = "redis://" + REDIS_AUTH + REDIS_HOST + ":" + REDIS_PORT + + +# Cache settings (use db #1 of redis) + +CACHE_REDIS_URL = REDIS_BASE_URL + "/1" + +CACHES = { + 'default': { + 'BACKEND': 'redis_cache.RedisCache', + 'LOCATION': CACHE_REDIS_URL, + } +} + + +# Channels settings (use db #0 of redis) + +CHANNEL_REDIS_URL = REDIS_BASE_URL + "/0" CHANNEL_LAYERS = { "default": { "BACKEND": "asgi_redis.RedisChannelLayer", "CONFIG": { - "hosts": [(os.environ.get("REDIS_HOST", "localhost"), 6379)], + "hosts": [CHANNEL_REDIS_URL], }, "ROUTING": "cof.routing.channel_routing", } diff --git a/provisioning/bootstrap.sh b/provisioning/bootstrap.sh index e94d3ac4..269e4f25 100644 --- a/provisioning/bootstrap.sh +++ b/provisioning/bootstrap.sh @@ -9,7 +9,7 @@ DBPASSWD="4KZt3nGPLVeWSvtBZPSM3fSzXpzEU4" # Installation de paquets utiles apt-get update && apt-get install -y python3-pip python3-dev python3-venv \ - libmysqlclient-dev libjpeg-dev git redis-server memcached + libmysqlclient-dev libjpeg-dev git redis-server pip install -U pip # Configuration et installation de mysql. Le mot de passe root est le même que diff --git a/requirements.txt b/requirements.txt index 202f5dda..2012351a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ django-autoslug==1.9.3 django-cas-ng==3.5.5 django-grappelli==2.8.1 django-recaptcha==1.0.5 +django-redis-cache==1.7.1 mysqlclient==1.3.7 Pillow==3.3.0 six==1.10.0 @@ -21,4 +22,3 @@ git+https://git.eleves.ens.fr/cof-geek/django_custommail.git#egg=django_customma ldap3 git+https://github.com/Aureplop/channels.git#egg=channels python-dateutil -python-memcached From dbf5844f6a996399b2227f57a136a39897002673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Sat, 15 Apr 2017 14:41:55 +0200 Subject: [PATCH 4/5] Clean settings redis --- cof/settings/common.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/cof/settings/common.py b/cof/settings/common.py index 98f4f713..a3b7527f 100644 --- a/cof/settings/common.py +++ b/cof/settings/common.py @@ -19,6 +19,26 @@ except ImportError: except KeyError: raise RuntimeError("Secrets missing") + +# Redis settings (for cache and channel layers) +try: + from .secret import REDIS_USER, REDIS_PASS +except ImportError: + REDIS_USER = os.environ.get("REDIS_USER", "") + REDIS_PASS = os.environ.get("REDIS_PASS", "") + +if REDIS_USER or REDIS_PASS: + REDIS_AUTH = REDIS_USER+":"+REDIS_PASS+"@" +else: + REDIS_AUTH = '' + +REDIS_HOST = os.environ.get("REDIS_HOST", "localhost") +REDIS_PORT = os.environ.get("REDIS_PORT", "6379") + +REDIS_BASE_URL = "redis://" + REDIS_AUTH + REDIS_HOST + ":" + REDIS_PORT +# To select a specific redis database, do: REDIS_BASE_URL + '/' + + # Other secrets try: from .secret import ( @@ -153,18 +173,6 @@ AUTHENTICATION_BACKENDS = ( RECAPTCHA_USE_SSL = True - -# Redis settings (for cache and channel layers) - -REDIS_HOST = os.environ.get("REDIS_HOST", "localhost") -REDIS_USER = os.environ.get("REDIS_USER", "") -REDIS_PASS = os.environ.get("REDIS_PASS", "") -REDIS_PORT = os.environ.get("REDIS_PORT", "6379") - -REDIS_AUTH = REDIS_USER+":"+REDIS_PASS+"@" if REDIS_USER or REDIS_PASS else '' -REDIS_BASE_URL = "redis://" + REDIS_AUTH + REDIS_HOST + ":" + REDIS_PORT - - # Cache settings (use db #1 of redis) CACHE_REDIS_URL = REDIS_BASE_URL + "/1" From e7266e7a9d834b2372c38c5339ad6412beae39a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Wed, 26 Apr 2017 11:28:18 +0200 Subject: [PATCH 5/5] use new settings for redis --- cof/settings/common.py | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/cof/settings/common.py b/cof/settings/common.py index 14f672e0..372d7d81 100644 --- a/cof/settings/common.py +++ b/cof/settings/common.py @@ -20,25 +20,6 @@ except KeyError: raise RuntimeError("Secrets missing") -# Redis settings (for cache and channel layers) -try: - from .secret import REDIS_USER, REDIS_PASS -except ImportError: - REDIS_USER = os.environ.get("REDIS_USER", "") - REDIS_PASS = os.environ.get("REDIS_PASS", "") - -if REDIS_USER or REDIS_PASS: - REDIS_AUTH = REDIS_USER+":"+REDIS_PASS+"@" -else: - REDIS_AUTH = '' - -REDIS_HOST = os.environ.get("REDIS_HOST", "localhost") -REDIS_PORT = os.environ.get("REDIS_PORT", "6379") - -REDIS_BASE_URL = "redis://" + REDIS_AUTH + REDIS_HOST + ":" + REDIS_PORT -# To select a specific redis database, do: REDIS_BASE_URL + '/' - - # Other secrets try: from .secret import ( @@ -174,19 +155,19 @@ AUTHENTICATION_BACKENDS = ( RECAPTCHA_USE_SSL = True -# Cache settings (use db #1 of redis) - -CACHE_REDIS_URL = REDIS_BASE_URL + "/1" +# Cache settings CACHES = { 'default': { 'BACKEND': 'redis_cache.RedisCache', - 'LOCATION': CACHE_REDIS_URL, + 'LOCATION': 'redis://:{passwd}@{host}:{port}/db' + .format(passwd=REDIS_PASSWD, host=REDIS_HOST, + port=REDIS_PORT, db=REDIS_DB), } } -# Channels settings (use db #0 of redis) +# Channels settings CHANNEL_LAYERS = { "default": {