Cache RecommendationManager to improve the avg response time

This commit is contained in:
Mauro Doglio 2017-09-28 12:56:05 +01:00 коммит произвёл Mauro Doglio
Родитель 6f9361b4d1
Коммит 5d198f3e43
1 изменённых файлов: 10 добавлений и 3 удалений

Просмотреть файл

@ -1,3 +1,4 @@
from django.core.cache import cache
from django.conf import settings
from django.http import JsonResponse
@ -8,8 +9,14 @@ from taar.hbase_client import HBaseClient
def recommendations(request, client_id):
"""Return a list of recommendations provided a telemetry client_id."""
hbase_client = HBaseClient(settings.HBASE_HOST)
profile_fetcher = ProfileFetcher(hbase_client)
recommendation_manager = RecommendationManager(profile_fetcher)
recommendation_manager = cache.get("recommendation_manager")
if recommendation_manager is None:
hbase_client = HBaseClient(settings.HBASE_HOST)
profile_fetcher = ProfileFetcher(hbase_client)
recommendation_manager = RecommendationManager(profile_fetcher)
# Cache the recommendation manager for 1h
cache.set("recommendation_manager",
recommendation_manager,
3600)
recommendations = recommendation_manager.recommend(client_id, settings.TAAR_MAX_RESULTS)
return JsonResponse({"results": recommendations})