Skip to content

Commit

Permalink
chore: implement caching on endpoints
Browse files Browse the repository at this point in the history
remove comment
  • Loading branch information
dmdhrumilmistry committed Feb 3, 2023
1 parent c9a9137 commit af9ac7a
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions route_planner/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.shortcuts import HttpResponse
# from django.views.decorators.cache import cache_page
# from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.utils.decorators import method_decorator
from folium import Map, Marker, CircleMarker
from itertools import combinations
from rest_framework.decorators import api_view
Expand Down Expand Up @@ -35,15 +35,13 @@

# Endpoint
# /api/route-mapper/map


class MapViewSet(ReadOnlyModelViewSet):
queryset = GarbageBinLocation.objects.all()
serializer_class = GarbageBinLocationSerializer
permission_classes = [IsAuthenticatedOrReadOnly]

# cache requested url for each user for 2 hours
# @method_decorator(cache_page(60*60*2))
@method_decorator(cache_page(60*60*2))
def create(self, request):
'''
HTTP POST request
Expand Down Expand Up @@ -71,7 +69,7 @@ def create(self, request):
return HttpResponse(map._repr_html_())

# cache requested url for each user for 1 hour
# @method_decorator(cache_page(60*60*1))
@method_decorator(cache_page(60*60*1))
def list(self, _: Request):
'''
HTTP GET request returns optimal route direction map visiting
Expand All @@ -86,7 +84,6 @@ def list(self, _: Request):
locations = list(filter(lambda loc: loc.garbage_weight > 0, locations))
logger.debug(f'Filtered Locations: {locations}')

# TODO: consider locations and apply djikstra's algorithm between locations
center_loc = locations[0]
center_loc = (center_loc.latitude, center_loc.longitude)
# BUG: If you're getting some sort of key error then increase distance value
Expand Down Expand Up @@ -126,7 +123,7 @@ def list(self, _: Request):

# /api/route-mapper/update-routes
@api_view(['GET'])
# @cache_page(60*60*1)
@cache_page(60*60*1)
def update_routes_data(request):
'''
Apply Djikstra's algorithm and store result in global variable which will be
Expand Down

0 comments on commit af9ac7a

Please sign in to comment.