Use Django cache

For caching employees
This commit is contained in:
Reynir Björnsson 2020-02-13 12:00:08 +01:00
parent 62be989948
commit e33f615205
1 changed files with 6 additions and 5 deletions

View File

@ -5,6 +5,7 @@ from django.utils.dateparse import parse_datetime
from django.utils import timezone
from django.urls import reverse
from django.conf import settings
from django.core.cache import cache
import pytz
import datetime
@ -17,19 +18,19 @@ local_timezone = pytz.timezone('Europe/Copenhagen')
planday = Planday(settings.PLANDAY_APPID, settings.PLANDAY_REFRESH_TOKEN)
planday.refresh_access_token()
employee_cache = {}
def today():
return datetime.date.today().isoformat()
def get_employee(employee_id):
# FIXME: Error handling
if employee_id in employee_cache:
return employee_cache[employee_id]
# FIXME: Use an employee cache, not the default cache
employee = cache.get(employee_id)
if employee:
return employee
employee = planday.get_employee(employee_id)
if employee == None:
return
employee_cache[employee_id] = employee
cache.set(employee_id, employee, timeout=60*60)
return employee
def index(request):