feat(django-redis): docker-compose sample for Django and Redis integration
Signed-off-by: cankush625 <cankush625@gmail.com>
This commit is contained in:
0
django-redis/django_redis/database/__init__.py
Normal file
0
django-redis/django_redis/database/__init__.py
Normal file
3
django-redis/django_redis/database/admin.py
Normal file
3
django-redis/django_redis/database/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
6
django-redis/django_redis/database/apps.py
Normal file
6
django-redis/django_redis/database/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class DatabaseConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'database'
|
3
django-redis/django_redis/database/models.py
Normal file
3
django-redis/django_redis/database/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
8
django-redis/django_redis/database/urls.py
Normal file
8
django-redis/django_redis/database/urls.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from django.urls import path
|
||||
|
||||
from database.views import visit_counter
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path('hello-redis/', visit_counter),
|
||||
]
|
17
django-redis/django_redis/database/views.py
Normal file
17
django-redis/django_redis/database/views.py
Normal 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))
|
Reference in New Issue
Block a user