librelist archives

« back to archive

How use cache the jsonify(data) by the redis backend.

How use cache the jsonify(data) by the redis backend.

From:
ydf
Date:
2014-11-07 @ 09:58
the return is  response object , not string, how can I  cache it.

my english is poor, thanks.
@app.route('/api/business/<part>/<int:part_id>')
@cached()
def business_type(part, part_id):
    """part : businessbrand, industry"""
    data = {}
    if part == u'businessbrand':
           ............
        return jsonify(data)
    if part == u'industry':
           ....................
        return jsonify(data)‍



now the cache is :
from werkzeug.contrib.cache import SimpleCache

cache = SimpleCache()
def cached(timeout=3600, key='view/%s'):
    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            cache_key = key % request.path
            rv = cache.get(cache_key)
            if rv is not None:
                return rv
            rv = f(*args, **kwargs)
            cache.set(cache_key, rv, timeout)
            return rv
        return decorated_function
    return decorator‍

Re: [flask] How use cache the jsonify(data) by the redis backend.

From:
digwtx
Date:
2014-11-07 @ 15:48
maybe you can see http://pythonhosted.org/Flask-Cache/ to get something help

At 2014-11-07 17:58:29, "ydf" <85897930@qq.com> wrote:
 the return is  response object , not string, how can I  cache it.

my english is poor, thanks.
@app.route('/api/business/<part>/<int:part_id>')
@cached()
def business_type(part, part_id):
    """part : businessbrand, industry"""
    data = {}
    if part == u'businessbrand':
           ............
        return jsonify(data)
    if part == u'industry':
           ....................
        return jsonify(data)‍



now the cache is :
from werkzeug.contrib.cache import SimpleCache

cache = SimpleCache ()
def cached(timeout=3600, key='view/%s'):
    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            cache_key = key % request.path
            rv = cache.get(cache_key)
            if rv is not None:
                return rv
            rv = f(*args, **kwargs)
            cache.set(cache_key, rv, timeout)
            return rv
        return decorated_function
    retur n decorator‍

回复:[flask] How use cache the jsonify(data) by the redis backend.

From:
ydf
Date:
2014-11-08 @ 06:42
thanks,  I will test pickle to load the python object.‍

------------------ 原始邮件 ------------------
发件人: "digwtx";<digwtx@163.com>;
发送时间: 2014年11月7日(星期五) 晚上11:48
收件人: "flask"<flask@librelist.com>; 

主题: Re:[flask] How use  cache  the  jsonify(data)  by  the redis backend.



maybe you can see http://pythonhosted.org/Flask-Cache/ to get something help

At 2014-11-07 17:58:29, "ydf" <85897930@qq.com> wrote:
  the return is  response object , not string, how can I  cache it.

my english is poor, thanks.
@app.route('/api/business/<part>/<int:part_id>')
@cached()
def business_type(part, part_id):
    """part : businessbrand, industry"""
    data = {}
    if part == u'businessbrand':
           ............
        return jsonify(data)
    if part == u'industry':
           ....................
        return jsonify(data)‍



now the cache is :
from werkzeug.contrib.cache import SimpleCache

cache = SimpleCache  ()
def cached(timeout=3600, key='view/%s'):
    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            cache_key = key % request.path
            rv = cache.get(cache_key)
            if rv is not None:
                return rv
            rv = f(*args, **kwargs)
            cache.set(cache_key, rv, timeout)
            return rv
        return decorated_function
    retur  n decorator‍

回复:[flask] How use cache the jsonify(data) by the redis backend.

From:
ydf
Date:
2014-11-11 @ 06:58
‍class RedisCache(object):

    """docstring for RedisCache"""

    def get(self, key):
        "返回key对应的value"
        value = redis_db.get(key)
        if value is None:
            return None
        return cPickle.loads(value)

    def set(self, key, value, timeout=None):
        "设定缓存值"
        rv = cPickle.dumps(value)
        redis_db.set(key, rv, ex=timeout)‍

------------------ 原始邮件 ------------------
发件人: "digwtx";<digwtx@163.com>;
发送时间: 2014年11月7日(星期五) 晚上11:48
收件人: "flask"<flask@librelist.com>; 

主题: Re:[flask] How use  cache  the  jsonify(data)  by  the redis backend.



maybe you can see http://pythonhosted.org/Flask-Cache/ to get something help

At 2014-11-07 17:58:29, "ydf" <85897930@qq.com> wrote:
  the return is  response object , not string, how can I  cache it.

my english is poor, thanks.
@app.route('/api/business/<part>/<int:part_id>')
@cached()
def business_type(part, part_id):
    """part : businessbrand, industry"""
    data = {}
    if part == u'businessbrand':
           ............
        return jsonify(data)
    if part == u'industry':
           ....................
        return jsonify(data)‍



now the cache is :
from werkzeug.contrib.cache import SimpleCache

cache = SimpleCache  ()
def cached(timeout=3600, key='view/%s'):
    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            cache_key = key % request.path
            rv = cache.get(cache_key)
            if rv is not None:
                return rv
            rv = f(*args, **kwargs)
            cache.set(cache_key, rv, timeout)
            return rv
        return decorated_function
    retur  n decorator‍