feat(django-redis): docker-compose sample for Django and Redis integration

Signed-off-by: cankush625 <cankush625@gmail.com>
This commit is contained in:
cankush625
2022-03-20 16:24:57 +05:30
parent 04f8c9ca12
commit 067f88f326
20 changed files with 482 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class DatabaseConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'database'

View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@@ -0,0 +1,8 @@
from django.urls import path
from database.views import visit_counter
urlpatterns = [
path('hello-redis/', visit_counter),
]

View File

@@ -0,0 +1,17 @@
from django.conf import settings
from django.http import HttpResponse
from redis import Redis
def get_database_connection(host: str, port: str) -> Redis:
"""Returns Redis client"""
return Redis(host=host, port=int(port))
def visit_counter(request) -> HttpResponse:
"""Connect to redis and increase the visits count for each visit"""
redis = get_database_connection(settings.HOST, settings.PORT)
redis.incr('views')
count = str(redis.get('views'), 'utf-8')
return HttpResponse("This page has been visited {0} times".format(count))