Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some caching using Redis #33

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion util.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
import datetime
import os
import random
import re
from functools import wraps
import time
from functools import wraps, update_wrapper

from flask import abort
from flask_login import current_user, login_required
from passlib.hash import bcrypt
from redis import from_url

redis = from_url(os.getenv("REDIS_URL"))


def cache(expire_in=120, uid=None):
def decorator(f):
def cache_function(*args, **kwargs):
arg_collection = list(args) + sorted(kwargs.items())
key = 'cache/%s/%s' % (f.__name__, arg_collection)
if uid is not None:
key = '%s/%s' % (key, uid())
with redis.pipeline() as p:
p.get(key)
result = p.execute()[0]
if result is None:
result = f(*args, **kwargs)
p.set(key, result)
p.expireat(key, int(time.time() + expire_in))
p.execute()
return result

return update_wrapper(cache_function, f)

return decorator


def uncache(function_name=None, args=None, uid=None):
if not function_name: return
key = 'cache/%s/%s' % (function_name, args)
if uid is not None:
key = '%s/%s' % (key, uid)
with redis.pipeline() as p:
p.delete(key)
p.execute()


def isoformat(seconds):
Expand Down
5 changes: 4 additions & 1 deletion views/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import config
from forms import EventForm
from models import db, Event
from util import admin_required, isoformat
from util import admin_required, isoformat, cache, uncache

blueprint = Blueprint('events', __name__, template_folder='templates')

Expand All @@ -21,6 +21,7 @@ def events_create():
event_create_form.populate_obj(new_event)
db.session.add(new_event)
db.session.commit()
uncache(function_name=events_owned.__name__, args=[], uid=current_user.id)
return redirect(url_for('.events_owned'))
return render_template('events/create.html', event_create_form=event_create_form)

Expand Down Expand Up @@ -139,6 +140,7 @@ def events_unapproved(page_number=1):

@blueprint.route('/owned')
@blueprint.route('/owned/page/<int:page_number>')
@cache(uid=lambda: current_user.id)
@login_required
def events_owned(page_number=1):
if page_number <= 0:
Expand Down Expand Up @@ -199,5 +201,6 @@ def events_remove(event_id):
if current_user != event.owner:
abort(403)
event.removed = True
uncache(function_name=events_owned.__name__, args=[], uid=current_user.id)
db.session.commit()
return redirect(url_for('.events_owned'))