Skip to content

Commit

Permalink
Cache filters using custom cache key. Fixes hasgeek#313
Browse files Browse the repository at this point in the history
  • Loading branch information
jace committed Mar 31, 2016
1 parent 413457c commit db7f93f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
4 changes: 0 additions & 4 deletions hasjob/models/jobpost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 15 additions & 8 deletions hasjob/views/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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')
Expand All @@ -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()]
Expand 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()]
Expand 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
4 changes: 2 additions & 2 deletions hasjob/views/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit db7f93f

Please sign in to comment.