Skip to content

Commit de61fc4

Browse files
renzonrenzon
authored andcommitted
Implemented Cache with Redis
close #2024
1 parent 2be0f2c commit de61fc4

File tree

7 files changed

+74
-17
lines changed

7 files changed

+74
-17
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ inflection = "*"
3434
django-two-factor-auth = "*"
3535
django-recaptcha = "*"
3636
django-pagarme = "*"
37+
django-redis = "*"
3738

3839
[dev-packages]
3940
faker = "*"

Pipfile.lock

Lines changed: 21 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ def turn_active_campaign_on(settings):
204204
settings.ACTIVE_CAMPAIGN_TURNED_ON = True
205205

206206

207+
@pytest.fixture(autouse=True)
208+
def turn_cache_off(settings):
209+
"""
210+
This way test don't depend on local .env configuration
211+
"""
212+
settings.CACHE_TURNED_ON = False
213+
214+
207215
@pytest.fixture(autouse=True)
208216
def turn_ssl_rediret_off_for_tests(settings):
209217
"""

contrib/env-sample

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ DISCOURSE_API_USER=
3535
ACTIVE_CAMPAIGN_URL=
3636
ACTIVE_CAMPAIGN_KEY=
3737
# Make it False on dev to avoiding sending data to ActiveCampaign
38-
ACTIVE_CAMPAIGN_TURNED_ON=true
38+
ACTIVE_CAMPAIGN_TURNED_ON=false
3939

4040
# Google Tag Manager Config
4141
GOOGLE_TAG_MANAGER_ID=GTM-ABC1234
@@ -49,4 +49,7 @@ REDIS_URL=redis://localhost:6379/1
4949

5050
RECAPTCHA_PUBLIC_KEY=
5151
RECAPTCHA_PRIVATE_KEY=
52-
52+
53+
54+
# Make it False to disable cache. Default is True
55+
CACHE_TURNED_ON=false

pythonpro/cohorts/facade.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from functools import partial
2+
3+
from django.conf import settings
4+
from django.core.cache import cache
15
from django.db.models import Prefetch as _Prefetch
26
from django.urls import reverse
37

@@ -15,7 +19,8 @@
1519

1620

1721
def get_all_cohorts_desc():
18-
return tuple(_Cohort.objects.order_by('-start'))
22+
lazy_all_cohorts = partial(tuple, _Cohort.objects.order_by('-start'))
23+
return cache.get_or_set('ALL_COHORTS', lazy_all_cohorts, settings.CACHE_TTL)
1924

2025

2126
def find_cohort(slug):

pythonpro/modules/facade.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from functools import partial
2+
3+
from django.conf import settings
4+
from django.core.cache import cache
15
from django.db.models import Prefetch
26

37
from pythonpro.modules.models import (
@@ -14,7 +18,8 @@ def get_all_modules():
1418
Search all modules on database sorted by order
1519
:return: tuple of Module
1620
"""
17-
return tuple(_Module.objects.order_by('order'))
21+
lazy_all_modules = partial(tuple, _Module.objects.order_by('order'))
22+
return cache.get_or_set('ALL_MODULES', lazy_all_modules, settings.CACHE_TTL)
1823

1924

2025
def get_module_with_contents(slug):

pythonpro/settings.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,32 @@
159159
'default': config('DATABASE_URL', default=default_db_url, cast=dburl),
160160
}
161161

162+
# Cache configuration
163+
REDIS_URL = config('REDIS_URL')
164+
CACHE_REDIS_URL = f'{REDIS_URL}/1'
165+
166+
CACHE_TTL = 60 * 15
167+
168+
CACHE_TURNED_ON = config('CACHE_TURNED_ON', default=True, cast=bool)
169+
170+
if CACHE_TURNED_ON:
171+
CACHES = {
172+
"default": {
173+
"BACKEND": "django_redis.cache.RedisCache",
174+
"LOCATION": CACHE_REDIS_URL,
175+
"OPTIONS": {
176+
"CLIENT_CLASS": "django_redis.client.DefaultClient"
177+
},
178+
"KEY_PREFIX": "example"
179+
}
180+
}
181+
else:
182+
CACHES = {
183+
'default': {
184+
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
185+
}
186+
}
187+
162188
# Password validation
163189
# https://docs.djangoproject.com/en/dev/ref/settings/#auth-password-validators
164190

@@ -274,6 +300,6 @@
274300

275301
BROKER_URL = config('CLOUDAMQP_URL')
276302

277-
CELERY_RESULT_BACKEND = config('REDIS_URL')
303+
CELERY_RESULT_BACKEND = f'{REDIS_URL}/0'
278304
CELERY_TASK_SERIALIZER = 'json'
279305
CELERY_TIMEZONE = TIME_ZONE

0 commit comments

Comments
 (0)