Skip to content
Merged
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
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ inflection = "*"
django-two-factor-auth = "*"
django-recaptcha = "*"
django-pagarme = "*"
django-redis = "*"

[dev-packages]
faker = "*"
Expand Down
33 changes: 21 additions & 12 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ def turn_active_campaign_on(settings):
settings.ACTIVE_CAMPAIGN_TURNED_ON = True


@pytest.fixture(autouse=True)
def turn_cache_off(settings):
"""
This way test don't depend on local .env configuration
"""
settings.CACHE_TURNED_ON = False


@pytest.fixture(autouse=True)
def turn_ssl_rediret_off_for_tests(settings):
"""
Expand Down
7 changes: 5 additions & 2 deletions contrib/env-sample
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ DISCOURSE_API_USER=
ACTIVE_CAMPAIGN_URL=
ACTIVE_CAMPAIGN_KEY=
# Make it False on dev to avoiding sending data to ActiveCampaign
ACTIVE_CAMPAIGN_TURNED_ON=true
ACTIVE_CAMPAIGN_TURNED_ON=false

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

RECAPTCHA_PUBLIC_KEY=
RECAPTCHA_PRIVATE_KEY=



# Make it False to disable cache. Default is True
CACHE_TURNED_ON=false
7 changes: 6 additions & 1 deletion pythonpro/cohorts/facade.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from functools import partial

from django.conf import settings
from django.core.cache import cache
from django.db.models import Prefetch as _Prefetch
from django.urls import reverse

Expand All @@ -15,7 +19,8 @@


def get_all_cohorts_desc():
return tuple(_Cohort.objects.order_by('-start'))
lazy_all_cohorts = partial(tuple, _Cohort.objects.order_by('-start'))
return cache.get_or_set('ALL_COHORTS', lazy_all_cohorts, settings.CACHE_TTL)


def find_cohort(slug):
Expand Down
7 changes: 6 additions & 1 deletion pythonpro/modules/facade.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from functools import partial

from django.conf import settings
from django.core.cache import cache
from django.db.models import Prefetch

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


def get_module_with_contents(slug):
Expand Down
28 changes: 27 additions & 1 deletion pythonpro/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,32 @@
'default': config('DATABASE_URL', default=default_db_url, cast=dburl),
}

# Cache configuration
REDIS_URL = config('REDIS_URL')
CACHE_REDIS_URL = f'{REDIS_URL}/1'

CACHE_TTL = 60 * 15

CACHE_TURNED_ON = config('CACHE_TURNED_ON', default=True, cast=bool)

if CACHE_TURNED_ON:
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": CACHE_REDIS_URL,
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient"
},
"KEY_PREFIX": "example"
}
}
else:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}

# Password validation
# https://docs.djangoproject.com/en/dev/ref/settings/#auth-password-validators

Expand Down Expand Up @@ -274,6 +300,6 @@

BROKER_URL = config('CLOUDAMQP_URL')

CELERY_RESULT_BACKEND = config('REDIS_URL')
CELERY_RESULT_BACKEND = f'{REDIS_URL}/0'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE