Browse Source

memcached

pull/1/head
wes 8 years ago
parent
commit
6a044996db
  1. 2
      src/posts.py
  2. 2
      src/projects.py
  3. 21
      src/website.py

2
src/posts.py

@ -17,8 +17,6 @@ class Posts:
self.client.credentials = (user, password) self.client.credentials = (user, password)
# FIXME check for pooling / concurrency issues
self.db = self.client["blog"] self.db = self.client["blog"]
def savepost(self, title="", content="", author="", category="programming", _id=False): def savepost(self, title="", content="", author="", category="programming", _id=False):

2
src/projects.py

@ -3,4 +3,4 @@ import requests
from json import loads from json import loads
def getProjects(): def getProjects():
return loads(requests.get("https://api.github.com/users/nisstyre56/repos?sort=updated&direction=desc&affiliation=owner").content) return requests.get("https://api.github.com/users/nisstyre56/repos?sort=updated&direction=desc&affiliation=owner").content

21
src/website.py

@ -27,14 +27,15 @@ def cacheit(key, thunk):
""" """
Tries to find a cached version of ``key'' Tries to find a cached version of ``key''
If there is no cached version then it will If there is no cached version then it will
evaluate thunk (which must be a generator) evaluate thunk and cache that
and cache that, then return the result
""" """
cached = cache.get(quote(key)) cached = cache.get(quote(key))
if cached is None: if cached is None:
result = list(thunk()) print("cache miss for %s" % key)
result = thunk()
cache.set(quote(key), result) cache.set(quote(key), result)
return result return result
print("cache hit for %s" % key)
return cached return cached
def get_posts(): def get_posts():
@ -43,6 +44,12 @@ def get_posts():
posts = g._posts = Posts(app.config["COUCHDB_USER"], app.config["COUCHDB_PASSWORD"]) posts = g._posts = Posts(app.config["COUCHDB_USER"], app.config["COUCHDB_PASSWORD"])
return posts return posts
def get_initial():
initial_post = getattr(g, "initial_post", None)
if initial_post is None:
initial_post = g._initial_post = posts.getinitial()
return initial_post
def NeverWhere(configfile=None): def NeverWhere(configfile=None):
app = Flask(__name__) app = Flask(__name__)
app.config["TEMPLATES_AUTO_RELOAD"] = True app.config["TEMPLATES_AUTO_RELOAD"] = True
@ -73,7 +80,7 @@ def NeverWhere(configfile=None):
@app.route("/blog/ghprojects", methods=("GET",)) @app.route("/blog/ghprojects", methods=("GET",))
def projects(): def projects():
return jsonify(cacheit("projects", getProjects)) return jsonify(loads(cacheit("projects", getProjects)))
@app.route("/blog/stuff", methods=("GET",)) @app.route("/blog/stuff", methods=("GET",))
def stuff(): def stuff():
@ -82,12 +89,11 @@ def NeverWhere(configfile=None):
# blog post routes # blog post routes
@app.route("/blog/posts/", methods=("GET",)) @app.route("/blog/posts/", methods=("GET",))
def renderInitial(): def renderInitial():
post_content = posts.getinitial() return render_template("index.html", quote=quote, postcontent=dict(initial_post))
return render_template("index.html", quote=quote, postcontent=dict(post_content))
@app.route("/blog/posts/<_id>", methods=("GET",)) @app.route("/blog/posts/<_id>", methods=("GET",))
def renderPost(_id): def renderPost(_id):
post_content = posts.getpost(_id, json=False) post_content = loads(cacheit(_id, lambda: dumps(posts.getpost(_id, json=False))))
return render_template("index.html", quote=quote, postcontent=dict(post_content)) return render_template("index.html", quote=quote, postcontent=dict(post_content))
@app.route("/blog/", methods=("GET", "POST")) @app.route("/blog/", methods=("GET", "POST"))
@ -173,6 +179,7 @@ def teardown_couchdb(exception):
del posts.db del posts.db
posts = LocalProxy(get_posts) posts = LocalProxy(get_posts)
initial_post = LocalProxy(get_initial)
login_manager.init_app(app) login_manager.init_app(app)

Loading…
Cancel
Save