Skip to content

Instantly share code, notes, and snippets.

@mmautner
Last active October 22, 2019 18:09
Show Gist options
  • Save mmautner/cd60fdd45934e5aa494d to your computer and use it in GitHub Desktop.
Save mmautner/cd60fdd45934e5aa494d to your computer and use it in GitHub Desktop.

Revisions

  1. mmautner revised this gist May 29, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions test.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    import requests
    # first time is uncached:
    print requests.get('http://localhost:5000/api/person').json()
    # second time is cached:
    print requests.get('http://localhost:5000/api/person').json()
  2. mmautner revised this gist May 29, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions app.py
    Original file line number Diff line number Diff line change
    @@ -23,21 +23,21 @@ class Person(db.Model):
    def get_cache_key():
    return hashlib.md5(request.path + request.query_string).hexdigest()

    def some_preprocessor(**kwargs):
    def cache_preprocessor(**kwargs):
    key = get_cache_key()
    if cache.get(key):
    print 'returning cached result'
    raise ProcessingException(description=cache.get(key), code=200)

    def some_postprocessor(result, **kwargs):
    def cache_postprocessor(result, **kwargs):
    cache.set(get_cache_key(), json.dumps(result))
    print 'result cached.'


    manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)
    manager.create_api(Person,
    methods=['GET'],
    preprocessors={'GET_MANY': [some_preprocessor]},
    postprocessors={'GET_MANY': [some_postprocessor]})
    preprocessors={'GET_MANY': [cache_preprocessor]},
    postprocessors={'GET_MANY': [cache_postprocessor]})

    app.run()
  3. mmautner created this gist May 29, 2014.
    43 changes: 43 additions & 0 deletions app.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    import json
    import hashlib
    import flask
    import flask.ext.sqlalchemy
    import flask.ext.restless
    from flask.ext.restless import ProcessingException

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

    # using memcached:
    from werkzeug.contrib.cache import MemcachedCache
    cache = MemcachedCache(['127.0.0.1:11211'])

    class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)

    db.create_all()
    from flask import request

    def get_cache_key():
    return hashlib.md5(request.path + request.query_string).hexdigest()

    def some_preprocessor(**kwargs):
    key = get_cache_key()
    if cache.get(key):
    print 'returning cached result'
    raise ProcessingException(description=cache.get(key), code=200)

    def some_postprocessor(result, **kwargs):
    cache.set(get_cache_key(), json.dumps(result))
    print 'result cached.'


    manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)
    manager.create_api(Person,
    methods=['GET'],
    preprocessors={'GET_MANY': [some_preprocessor]},
    postprocessors={'GET_MANY': [some_postprocessor]})

    app.run()