cache decor
This commit is contained in:
parent
d5272789f4
commit
86e0c06805
|
@ -162,7 +162,7 @@ def create_app(subdomain=None):
|
||||||
return redirect(redirect_url())
|
return redirect(redirect_url())
|
||||||
else:
|
else:
|
||||||
s = Site()
|
s = Site()
|
||||||
s.create(name=wiki_name, repo=wiki_name)
|
s.create(name=wiki_name, repo=wiki_name, founder=CurrentUser.get('id'))
|
||||||
instances.pop(wiki_name, None)
|
instances.pop(wiki_name, None)
|
||||||
return redirect('http://%s.%s' % (wiki_name, config.hostname))
|
return redirect('http://%s.%s' % (wiki_name, config.hostname))
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,13 +1,28 @@
|
||||||
|
import json
|
||||||
|
|
||||||
import rethinkdb as rdb
|
import rethinkdb as rdb
|
||||||
import bcrypt
|
import bcrypt
|
||||||
from flask import session, flash
|
from flask import session, flash
|
||||||
|
|
||||||
from flask.ext.login import login_user, logout_user
|
from flask.ext.login import login_user, logout_user
|
||||||
|
|
||||||
from rethinkORM import RethinkModel
|
from rethinkORM import RethinkModel
|
||||||
|
|
||||||
from util import gravatar_url
|
from util import gravatar_url
|
||||||
from services import db
|
from services import db, cache
|
||||||
|
|
||||||
|
|
||||||
def to_dict(cur, first):
|
def to_json(res, first=False):
|
||||||
|
"""
|
||||||
|
Jsonify query result.
|
||||||
|
"""
|
||||||
|
res = to_dict(res, first)
|
||||||
|
return json.dumps(res, separators=(',',':'))
|
||||||
|
|
||||||
|
|
||||||
|
def to_dict(cur, first=False):
|
||||||
|
if not cur:
|
||||||
|
return None
|
||||||
ret = []
|
ret = []
|
||||||
for row in cur:
|
for row in cur:
|
||||||
ret.append(row)
|
ret.append(row)
|
||||||
|
@ -16,6 +31,33 @@ def to_dict(cur, first):
|
||||||
else:
|
else:
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
def cache_it(fn):
|
||||||
|
def wrap(*args, **kw):
|
||||||
|
key = "%s:%s" % (args[0].table, args[1])
|
||||||
|
data = cache.get(key)
|
||||||
|
# Assume strings are JSON encoded
|
||||||
|
try:
|
||||||
|
data = json.loads(data)
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if data is not None:
|
||||||
|
return data
|
||||||
|
else:
|
||||||
|
data = fn(*args)
|
||||||
|
ret = data
|
||||||
|
if data is None:
|
||||||
|
data = ''
|
||||||
|
if not isinstance(data, basestring):
|
||||||
|
try:
|
||||||
|
data = json.dumps(data, separators=(',', ':'))
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
cache.set(key, data)
|
||||||
|
return ret
|
||||||
|
return wrap
|
||||||
|
|
||||||
class BaseModel(RethinkModel):
|
class BaseModel(RethinkModel):
|
||||||
|
|
||||||
|
@ -30,9 +72,14 @@ class BaseModel(RethinkModel):
|
||||||
def create(cls, **kwargs):
|
def create(cls, **kwargs):
|
||||||
return super(BaseModel, cls).create(**kwargs)
|
return super(BaseModel, cls).create(**kwargs)
|
||||||
|
|
||||||
|
@cache_it
|
||||||
|
def get_by_id(self, id):
|
||||||
|
return to_dict(rdb.table(self.table).get(id).run(self._conn))
|
||||||
|
|
||||||
def get_all(self, arg, index):
|
def get_all(self, arg, index):
|
||||||
return rdb.table(self.table).get_all(arg, index=index).run(self._conn)
|
return rdb.table(self.table).get_all(arg, index=index).run(self._conn)
|
||||||
|
|
||||||
|
#@cache_it
|
||||||
def get_one(self, arg, index):
|
def get_one(self, arg, index):
|
||||||
return rdb.table(self.table).get_all(arg, index=index).limit(1).run(self._conn)
|
return rdb.table(self.table).get_all(arg, index=index).limit(1).run(self._conn)
|
||||||
|
|
||||||
|
@ -62,6 +109,13 @@ class CurrentUser():
|
||||||
def is_authenticated(self):
|
def is_authenticated(self):
|
||||||
return True if self.id else False
|
return True if self.id else False
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get(key):
|
||||||
|
try:
|
||||||
|
return session['user'][key]
|
||||||
|
except KeyError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
class User(BaseModel):
|
class User(BaseModel):
|
||||||
table = 'users'
|
table = 'users'
|
||||||
|
|
Loading…
Reference in a new issue