Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mrkiven/ecache

:clap::clap: Integrate cache(redis) [flask etc.] with SQLAlchemy.
https://github.com/mrkiven/ecache

cache flask python sqlalchemy

Last synced: 23 days ago
JSON representation

:clap::clap: Integrate cache(redis) [flask etc.] with SQLAlchemy.

Awesome Lists containing this project

README

        

Ecache for sqlalchemy
=====================

Run test
--------

.. code:: bash

make unittest

Installation / Rquirements
--------------------------

.. code::

pip install ecache

Usage
-----

With Flask Integrate
~~~~~~~~~~~~~~~~~~~~

.. code:: python

from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy

from ecache.ext.flask_cache import CacheableMixin, query_callable, regions

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
app.debug = True
db = SQLAlchemy(app)

class User(db.Model, CacheableMixin):
"""Default backend is redis and expiration time is 1 hour, default
region name is `default`, you can override this:

cache_regions = your_regions
cache_label = your_label
"""

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)

@app.route('/users')
def all_users():
"""Result will try to get from cache first. load from db if cache miss.
"""
users = [user.to_dict() for user in User.cache.filter()]
return jsonify(users=users)

@app.route('/users/