feat: add endpoint for default caching via redis

Signed-off-by: Ameya Shenoy <shenoy.ameya@gmail.com>
This commit is contained in:
Ameya Shenoy 2021-02-11 11:18:58 +05:30
parent 63b2a94440
commit 9629b7b7d2
Signed by: codingcoffee
GPG key ID: F7D58AAC5DACF8D3
5 changed files with 33 additions and 8 deletions

View file

@ -1,6 +1,7 @@
from django.urls import path from django.urls import path
from app.views import BhavCopyEquityView from app.views import BhavCopyEquityView, BhavCopyEquityDefaultRedisView
urlpatterns = [ urlpatterns = [
path('bhavcopyequity/', BhavCopyEquityView.as_view(), name='bhavcopyequity_view'), path('bhavcopyequity/', BhavCopyEquityView.as_view(), name='bhavcopyequity_view'),
path('bhavcopyequitydefaultredis/', BhavCopyEquityDefaultRedisView.as_view(), name='bhavcopyequity_view'),
] ]

View file

@ -1,9 +1,16 @@
# third-party imports
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import generics from rest_framework import generics
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
# app imports
from app.models import BhavCopyEquity from app.models import BhavCopyEquity
from app.serializers import BhavCopyEquitySerializer from app.serializers import BhavCopyEquitySerializer
# Create your views here. # Create your views here.
class BhavCopyEquityView(generics.RetrieveAPIView): class BhavCopyEquityView(generics.RetrieveAPIView):
queryset = BhavCopyEquity.objects.all() queryset = BhavCopyEquity.objects.all()
@ -11,5 +18,18 @@ class BhavCopyEquityView(generics.RetrieveAPIView):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
queryset = self.get_queryset() queryset = self.get_queryset()
serializer = BhavCopyEquitySerializer(queryset, many=True) serializer = BhavCopyEquitySerializer(queryset, many=True)
print("without redis")
return Response(serializer.data) return Response(serializer.data)
class BhavCopyEquityDefaultRedisView(generics.RetrieveAPIView):
queryset = BhavCopyEquity.objects.all()
@method_decorator(cache_page(60*60*2))
def get(self, request, *args, **kwargs):
queryset = self.get_queryset()
serializer = BhavCopyEquitySerializer(queryset, many=True)
print("without redis")
return Response(serializer.data)

View file

@ -97,8 +97,8 @@ DATABASES = {
CACHES = { CACHES = {
'default': { 'default': {
'BACKEND': 'redis_cache.cache.RedisCache', 'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis:6379/1', 'LOCATION': 'redis://redis:6379/1',
'OPTIONS': { 'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient', 'CLIENT_CLASS': 'django_redis.client.DefaultClient',
}, },
@ -152,6 +152,9 @@ RQ_QUEUES = {
}, },
} }
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"
CACHE_TTL = 60 * 5
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/ # https://docs.djangoproject.com/en/3.1/howto/static-files/

View file

@ -10,7 +10,7 @@ export default new VueRouter({
base: process.env.BASE_URL, base: process.env.BASE_URL,
routes: [ routes: [
{ {
path: '/', path: '/bhavcopy',
name: 'bhavcopy', name: 'bhavcopy',
component: BhavCopy, component: BhavCopy,
} }

View file

@ -25,7 +25,7 @@
</v-card-title> </v-card-title>
<v-data-table <v-data-table
ref="bhavCopyTable" ref="bhavCopyTable"
:items="APIData" :items="apiData"
:headers="headers" :headers="headers"
:items-per-page="10" :items-per-page="10"
:footer-props="{ :footer-props="{
@ -75,7 +75,8 @@
data() { data() {
return { return {
sc_name: '', sc_name: '',
APIData: [], apiData: [],
apiEndpointSelected: 'bhavcopyequity',
headersData: [ headersData: [
{text: 'Stock Code', value: 'sc_code'}, {text: 'Stock Code', value: 'sc_code'},
{ {
@ -101,10 +102,10 @@
} }
}, },
created() { created() {
getAPI.get('/bhavcopyequity/',) getAPI.get(`/${this.apiEndpointSelected}/`,)
.then(response => { .then(response => {
console.log('BhavCopyEquity API has recieved data') console.log('BhavCopyEquity API has recieved data')
this.APIData = response.data this.apiData = response.data
}) })
.catch(err => { .catch(err => {
console.log(err) console.log(err)