Flask-SQLAlchemy-Cache

CachingQuery implementation to Flask using Flask-SQLAlchemy and Flask-Cache


License
MIT
Install
pip install Flask-SQLAlchemy-Cache==0.1.5

Documentation

Flask-SQLAlchemy-Cache

A CachingQuery implementation to Flask using Flask-SQLAlchemy and Flask-Cache.

To start using caching queries, you just have to replace Flask-SQLAlchemy Model.query_class.

from flask.ext.sqlalchemy import SQLAlchemy. Model
from flask.ext.sqlalchemy_cache import CachingQuery

# for Flask-SQLAlchemy < 3.0, set `Model.query_class` and `session_options['query_cls']`
Model.query_class = CachingQuery
db = SQLAlchemy(session_options={'query_cls': CachingQuery})

# for Flask-SQLAlchemy >= 3.0, `query_class` is a built-in argument.
db = SQLAlchemy(app, query_class=CachingQuery)

After that, you can just make queries to a model YourModel:

from flask.ext.sqlalchemy_cache import FromCache

# cache is a Flask-Cache instance
YourModel.query.options(FromCache(cache)).first()

You also have RelationshipCache to enable lazy loading relationships from cache.

from sqlalchemy.orm import lazyload
from flask.ext.sqlalchemy_cache import RelationshipCache

rc = RelationshipCache(YourModel.some_relationship, cache)
obj = YourModel.query.options(lazyload(YourModel.some_relationship), rc).first()

# make the query and cache the results for future queries
print obj.some_relationship

Take a look at Dogpile Caching example to more details about how CachingQuery works. Most changes to their were made just to integrate it with Flask, Flask-SQLAlchemy and Flask-Cache instead of Dogpile.