Skip to content

Commit

Permalink
Merge pull request raksha-life#397 from ashwinrajeev/master
Browse files Browse the repository at this point in the history
add caching for mapdata
  • Loading branch information
vigneshhari authored Aug 17, 2018
2 parents 1627fcf + 8c9399e commit 3fe7513
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ SECRET_KEY=1
ALLOWED_HOSTS=localhost
SENTRY_DSN=""
B_DATABASE_URL="postgres://rescueuser:password@localhost/rescuekerala"
REDIS_URL=redis://localhost:6379
CACHE_TIMEOUT=60
DEBUG=true
15 changes: 14 additions & 1 deletion floodrelief/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def get_list(text):

env = environ.Env(
# set casting, default value
DEBUG=(bool, False)
DEBUG=(bool, False),
CACHE_TIMEOUT=(int, 60)
)
# reading .env file
root = environ.Path(__file__) - 2
Expand Down Expand Up @@ -108,6 +109,18 @@ def get_list(text):
DATABASES = {}
DATABASES['default'] = dj_database_url.parse(env('B_DATABASE_URL'), conn_max_age=600)

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": env('REDIS_URL'),
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient"
},
"KEY_PREFIX": "keralarescue"
}
}
CACHE_TIMEOUT = env('CACHE_TIMEOUT')

# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

Expand Down
12 changes: 9 additions & 3 deletions mainapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from django.contrib.auth import logout
from django.shortcuts import redirect
from django.db.models import Count
from django.core.cache import cache
from django.conf import settings

class CreateRequest(CreateView):
model = Request
Expand Down Expand Up @@ -168,11 +170,15 @@ class Maintenance(TemplateView):


def mapdata(request):
if("district" in request.GET.keys()):
data = Request.objects.exclude(latlng__exact="").filter(district = request.GET.get("district")).values()
district = request.GET.get("district", "all")
data = cache.get("mapdata:" + district)
if data:
return JsonResponse(list(data) , safe=False)
if district != "all":
data = Request.objects.exclude(latlng__exact="").filter(district=district).values()
else:
data = Request.objects.exclude(latlng__exact="").values()

cache.set("mapdata:" + district, data, settings.CACHE_TIMEOUT)
return JsonResponse(list(data) , safe=False)

def mapview(request):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ psycopg2==2.7.5
pytz==2018.5
raven==6.9.0
whitenoise==4.0
django-redis==4.9.0

0 comments on commit 3fe7513

Please sign in to comment.