the only part of this where something interesting has to happen is the "all()". The rest of it seems like you'd stick with a straight Query.
So here's one way pattern:
q = query(User).filter(User.id = user_id).limit(whatever)
results = from_redis(q, Query.all)
and here's a way we can do that:
def from_redis(query, meth):
for server in ['r1', 'r2']:
try:
return meth(q.options(FromCache(server)))
except Exception, e:
continue
else:
raise e
but that might seem a little lame. Since you're using the caching recipe and subclassing Query
, you could add your redis feature to it as well, sort of like:
class RedisQuery(CachingQuery):
redis_servers = False
@_generative(self):
def using_redis(self):
self.redis_servers = True
def __iter__(self):
if self.redis_servers:
for server in ['r1', 'r2']:
try:
return super(RedisQuery, self).options(FromCache(server)).__iter__()
except Exception, e:
continue
else:
raise e
else:
return super(RedisQuery, self).__iter__()
that one you call like this:
q = query(User).filter(User.id = user_id).limit(whatever)
results = q.using_redis().all()
just some ideas.