From db7f93f225e293024c263f83e6291533aa8223e5 Mon Sep 17 00:00:00 2001 From: Kiran Jonnalagadda Date: Thu, 31 Mar 2016 11:06:47 +0530 Subject: [PATCH] Cache filters using custom cache key. Fixes #313 --- hasjob/models/jobpost.py | 4 ---- hasjob/views/helper.py | 23 +++++++++++++++-------- hasjob/views/index.py | 4 ++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/hasjob/models/jobpost.py b/hasjob/models/jobpost.py index 02e3e91ee..309b5e9c0 100644 --- a/hasjob/models/jobpost.py +++ b/hasjob/models/jobpost.py @@ -546,10 +546,6 @@ def reports(self): counts[flag.reportcode] = counts.setdefault(flag.reportcode, 0) + 1 return [{'count': i[2], 'title': i[1]} for i in sorted([(k.seq, k.title, v) for k, v in counts.items()])] - @classmethod - def is_paginated(cls, request): - return 'startdate' in request.values - def viewstats_helper(jobpost_id, interval, limit, daybatch=False): post = JobPost.query.get(jobpost_id) diff --git a/hasjob/views/helper.py b/hasjob/views/helper.py index 141a270f0..2841ad04a 100644 --- a/hasjob/views/helper.py +++ b/hasjob/views/helper.py @@ -3,11 +3,12 @@ from os import path from datetime import datetime, timedelta from urlparse import urljoin +from urllib import quote, quote_plus +import hashlib import uuid import bleach import requests from pytz import utc, timezone -from urllib import quote, quote_plus from sqlalchemy import or_ from sqlalchemy.exc import IntegrityError from geoip2.errors import AddressNotFoundError @@ -39,6 +40,10 @@ def sniffle(): } +def index_is_paginated(): + return 'startdate' in request.values + + @form_validation_success.connect def event_form_validation_success(form): g.event_data['form_validation'] = 'ok' @@ -711,7 +716,6 @@ def filter_basequery(basequery, filters, exclude_list=[]): return basequery -@cache.memoize(timeout=3600) def filter_locations(filters): now = datetime.utcnow() basequery = db.session.query(JobLocation.geonameid, db.func.count(JobLocation.geonameid).label('count') @@ -728,7 +732,6 @@ def filter_locations(filters): for geonameid in geonameids] -@cache.memoize(timeout=3600) def filter_types(basequery, board, filters): basequery = filter_basequery(basequery, filters, exclude_list=['types']) filtered_typeids = [post.type_id for post in basequery.options(db.load_only('type_id')).distinct('type_id').all()] @@ -744,7 +747,6 @@ def format_job_type(job_type): for job_type in JobType.query.filter_by(private=False, public=True).order_by('seq')] -@cache.memoize(timeout=3600) def filter_categories(basequery, board, filters): basequery = filter_basequery(basequery, filters, exclude_list=['categories']) filtered_categoryids = [post.category_id for post in basequery.options(db.load_only('category_id')).distinct('category_id').all()] @@ -763,10 +765,15 @@ def format_job_category(job_category): @app.context_processor def inject_filter_options(): # Don't compute filter choices for paginated results - if JobPost.is_paginated(request): + if index_is_paginated(): return dict() basequery = getposts(showall=True, order=False, limit=False) filters = g.get('event_data', {}).get('filters', {}) - return dict(job_location_filters=filter_locations(filters), - job_type_filters=filter_types(basequery, board=g.board, filters=filters), - job_category_filters=filter_categories(basequery, board=g.board, filters=filters)) + cache_key = 'jobfilters/' + hashlib.sha1(repr(filters)).hexdigest() + result = cache.get(cache_key) + if not result: + result = dict(job_location_filters=filter_locations(filters), + job_type_filters=filter_types(basequery, board=g.board, filters=filters), + job_category_filters=filter_categories(basequery, board=g.board, filters=filters)) + cache.set(cache_key, result, timeout=3600) + return result diff --git a/hasjob/views/index.py b/hasjob/views/index.py index 07c77ceb3..ca96627a5 100644 --- a/hasjob/views/index.py +++ b/hasjob/views/index.py @@ -13,7 +13,7 @@ from ..models import (db, JobCategory, JobPost, JobType, POSTSTATUS, newlimit, agelimit, JobLocation, Board, Domain, Location, Tag, JobPostTag, Campaign, CAMPAIGN_POSITION, CURRENCY, JobApplication, starred_job_table) from ..views.helper import (getposts, getallposts, gettags, location_geodata, cache_viewcounts, session_jobpost_ab, - bgroup, make_pay_graph) + bgroup, make_pay_graph, index_is_paginated) from ..uploads import uploaded_logos from ..utils import string_to_number @@ -365,7 +365,7 @@ def index(basequery=None, md5sum=None, tag=None, domain=None, location=None, tit header_campaign=header_campaign, loadmore=loadmore, search_domains=search_domains, query_params=query_params, is_siteadmin=is_siteadmin, - pay_graph_data=pay_graph_data, paginated=JobPost.is_paginated(request), template_vars=template_vars) + pay_graph_data=pay_graph_data, paginated=index_is_paginated(), template_vars=template_vars) @csrf.exempt