Skip to content

Commit

Permalink
enhancement: Cache DB calls for card template
Browse files Browse the repository at this point in the history
Signed-off-by: Rommel Terrence Juanillo <terrence@newlogic.com>
  • Loading branch information
renceInbox committed Jun 5, 2023
1 parent c7acd69 commit 73c00f5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
1 change: 1 addition & 0 deletions .envs/.local/.django
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ USE_DOCKER=yes
IPYTHONDIR=/app/.ipython
CELERY_BROKER_URL=redis://redis:6379/0
CELERY_RESULT_BACKEND=redis://redis:6379/0
REDIS_URL=redis://redis:6379/1
9 changes: 9 additions & 0 deletions card_generator/api/v1/cards/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from django.conf import settings
from django.core.cache import cache
from drf_spectacular.utils import OpenApiExample, extend_schema, extend_schema_view
from rest_framework.authentication import TokenAuthentication
from rest_framework.decorators import action
Expand Down Expand Up @@ -34,6 +35,14 @@ class CardViewSet(ModelViewSet):
authentication_classes = (TokenAuthentication,)
http_method_names = ("get", "post", "put", "delete")

def get_object(self):
card_data = cache.get("card_data")
if not card_data:
card_data = super().get_object()
cache.set("card_data", card_data, 600)

return card_data

@action(
methods=["post"],
detail=True,
Expand Down
26 changes: 24 additions & 2 deletions config/settings/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Base settings to build other settings files upon.
"""

from pathlib import Path

import environ
Expand All @@ -10,8 +11,7 @@
APPS_DIR = ROOT_DIR / "card_generator"
env = environ.Env()

READ_DOT_ENV_FILE = env.bool("DJANGO_READ_DOT_ENV_FILE", default=False)
if READ_DOT_ENV_FILE:
if READ_DOT_ENV_FILE := env.bool("DJANGO_READ_DOT_ENV_FILE", default=False):
# OS environment variables take precedence over variables from .env
env.read_env(str(ROOT_DIR / ".env"))

Expand Down Expand Up @@ -190,6 +190,28 @@
}
]

# CACHES
# ------------------------------------------------------------------------------
CACHE_TTL = env.int("CACHE_TTL", default=600)
DEFAULT_TIMEOUT = (
CACHE_TTL # Backwards compatibility, django_redis use this instead of CACHE_TTL
)
REDIS_URL = env.str("REDIS_URL", default=None)
if REDIS_URL:
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": REDIS_URL,
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
# Mimicing memcache behavior.
# https://github.com/jazzband/django-redis#memcached-exceptions-behavior
"IGNORE_EXCEPTIONS": True,
},
}
}


# https://docs.djangoproject.com/en/dev/ref/settings/#form-renderer
FORM_RENDERER = "django.forms.renderers.TemplatesSetting"

Expand Down
18 changes: 1 addition & 17 deletions config/settings/production.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# sourcery skip: use-named-expression
import logging

import sentry_sdk
Expand All @@ -21,23 +22,6 @@
DATABASES["default"]["ATOMIC_REQUESTS"] = True # noqa F405
DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60) # noqa F405

# CACHES
# ------------------------------------------------------------------------------
REDIS_URL = env.str("REDIS_URL", default=None)
if REDIS_URL:
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": REDIS_URL,
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
# Mimicing memcache behavior.
# https://github.com/jazzband/django-redis#memcached-exceptions-behavior
"IGNORE_EXCEPTIONS": True,
},
}
}

# SECURITY
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-proxy-ssl-header
Expand Down

0 comments on commit 73c00f5

Please sign in to comment.