-
Notifications
You must be signed in to change notification settings - Fork 0
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 level of caching for speed-up #78
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from functools import wraps | ||
|
||
from flask import request | ||
from werkzeug.contrib.cache import SimpleCache | ||
|
||
|
||
cache = SimpleCache() | ||
EXPIRY_SECONDS = 5 * 60 | ||
|
||
|
||
def cached(timeout=EXPIRY_SECONDS, key='view/%s'): | ||
""" | ||
A decorator for caching views. | ||
|
||
Source: http://flask.pocoo.org/docs/patterns/viewdecorators/ | ||
|
||
With a little additional work it could be made into a generic caching | ||
decorator for other functions that return pickleable data. | ||
""" | ||
def decorator(f): | ||
@wraps(f) | ||
def decorated_function(*args, **kwargs): | ||
cache_key = key % request.path | ||
rv = cache.get(cache_key) | ||
if rv is not None: | ||
return rv | ||
rv = f(*args, **kwargs) | ||
cache.set(cache_key, rv, timeout=timeout) | ||
return rv | ||
return decorated_function | ||
return decorator | ||
|
||
|
||
def activate_requests_caching(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we want both Doesn't the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They serve different purposes. One caches what we serve to the user, the other caches what we get from other tools. User -(cached)- Ployst -(requests_caching)- 3rd party tools Eventually we may want to review how we fetch and cache 3rd party data so that we can have some of it pre-fetched in the background, not impacting response time to the user. That lives in a longer-term future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I understood the difference in when they were caching - but in the use case of I'm a little uneasy with the way the Happy to merge if you add a config param to decide whether to call this on line 70 of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On retrospect, this looked like a quick win but it is a shoddy solution. I am concerned about the load time for the page and I want my team to use ployst and I didn't want page load time to be a pain point. But I am not sure what is the best place to implement caching for provider calls. Either we do it by 1) wrapping it around calls to providers (preferrable, but at that level we may not be aware of what variables make output change - such as provider configuration) or 2) we leave that responsibility to providers' implementations (that will be more aware of what caching makes sense for the service they call, or which are the variables). Leaning towards 1), but changes in provider configuration may require busting the cache for that provider. |
||
""" | ||
Call once to activate caching for all HTTP GET issued by 'requests' | ||
""" | ||
import requests_cache | ||
requests_cache.install_cache(expire_after=EXPIRY_SECONDS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this also wants to take into account user. Is there something in the request that is unique for the user?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will have to get session ID from somewhere?