Skip to main content

flask-api-cache

A package for caching flask api with custom key.

Description:

A decorator for python flask api.

You can set cache in your memory or with redis instance,
the key will be generated by the following rule:
{REQUEST_FULL_PATH}:{PARAMETER_STRING}
or you can use your custom key function by key_func args,
the value will be your function return value.

How To Use:

Import

from flask_api_cache import ApiCache

Parameters

name required description
redis no if you want to caching data in redis, you can call ApiCache with a redis instance.
expired_time no set your expired time with seconds, the default value is 24 * 60 * 60 seconds (1 day)
key_func no the function which you want to make custom key

Cache Without Redis

@app.route('/example_1/<string:name>')
@ApiCache(expired_time=10)
def example_1(name):
    """
    caching data in memory with default key.
        - http://0.0.0.0:5000/example_1/jimmy
    :param name:
    :return:
    """
    return jsonify(f'Hi {name}, it is {datetime.now()}')

If you request for http://0.0.0.0:5000/example_1/jimmy,
it will set a 10 seconds cache by key: example_1:/example_1/Jimmy?,
with value: Hi jimmy, it is 2020-10-23 16:06:27.996358., in your memory, it will be cleared after api service restart.

Cache With Redis

@app.route('/example_3/<string:items>')
@ApiCache(redis=redis_instance, expired_time=10)
def example_3(items):
    """
    caching data in redis instance with default key.
        - http://0.0.0.0:5000/example_3/coffee
    :param items:
    :return:
    """
    return jsonify(f'You bought {items} at {datetime.now()}')

If you request for http://0.0.0.0:5000/example_3/coffee,
it will set a 10 seconds cache by key: example_3:/example_3/coffee?,
with value: You bought coffee at 2020-10-23 16:06:59.904216, in your redis instance.

Cache With Custom Function

def custom_func_2(**kwargs):
    items = kwargs.get('items')
    price = kwargs.get('price')
    keys = f'{items}:{price}'
    return keys

@app.route('/example_4/<string:items>/<int:price>')
@ApiCache(redis=redis_instance, key_func=custom_func_2, expired_time=10)
def example_4(items, price):
    """
    caching data in redis instance with custom function.
        - http://0.0.0.0:5000/example_4/coffee/20
    :param items:
    :param price:
    :return:
    """
    return jsonify(f'You bought {items} at {datetime.now()}, it cost ${price}')

If you request for http://0.0.0.0:5000/example_4/coffee/20 ,
it will set a 10 seconds cache by key: coffee:20,
with value: You bought coffee at 2020-10-23 16:07:59.818357, it cost $20, in your memory, it will be cleared after service restart.

Sample Code

# -*- coding: utf-8 -*-
"""
      ┏┓       ┏┓
    ┏━┛┻━━━━━━━┛┻━┓
    ┃      ☃      ┃
    ┃  ┳┛     ┗┳  ┃
    ┃      ┻      ┃
    ┗━┓         ┏━┛
      ┗┳        ┗━┓
       ┃          ┣┓
       ┃          ┏┛
       ┗┓┓┏━━━━┳┓┏┛
        ┃┫┫    ┃┫┫
        ┗┻┛    ┗┻┛
    God Bless,Never Bug
"""
import redis
from flask import Flask, request, jsonify
from flask_api_cache import ApiCache
from datetime import datetime

app = Flask(__name__)

redis_instance = redis.StrictRedis(host='redis', port=6379)


def custom_func_1(**kwargs):
    name = kwargs.get('name')
    age = kwargs.get('age')
    sex = kwargs.get('sex')
    keys = f'{name}:{age}:{sex}'
    return keys


def custom_func_2(**kwargs):
    items = kwargs.get('items')
    price = kwargs.get('price')
    keys = f'{items}:{price}'
    return keys


"""GET"""


@app.route('/example_1/<string:name>', methods=['GET'])
@ApiCache(expired_time=10)
def example_1(name):
    """
    caching data in memory with default key.
        - http://0.0.0.0:5000/example_1/jimmy
    :param name:
    :return:
    """
    return jsonify(f'Hi {name}, it is {datetime.now()}')


@app.route('/example_2/<string:name>/<int:age>', methods=['GET'])
@ApiCache(expired_time=10, key_func=custom_func_1)
def example_2(name, age):
    """
    caching data in memory with custom function.
        - http://0.0.0.0:5000/example_2/jimmy/18?sex=boy
    :param name:
    :param age:
    :return:
    """
    sex = request.args.get('sex', 'boy', str)
    return jsonify(f'{name} is a {age} years old {sex}. {datetime.now()}')


@app.route('/example_3/<string:items>', methods=['GET'])
@ApiCache(redis=redis_instance, expired_time=10)
def example_3(items):
    """
    caching data in redis instance with default key.
        - http://0.0.0.0:5000/example_3/coffee
    :param items:
    :return:
    """
    return jsonify(f'You bought {items} at {datetime.now()}')


@app.route('/example_4/<string:items>/<int:price>', methods=['GET'])
@ApiCache(redis=redis_instance, key_func=custom_func_2, expired_time=10)
def example_4(items, price):
    """
    caching data in redis instance with custom function.
        - http://0.0.0.0:5000/example_4/coffee/20
    :param items:
    :param price:
    :return:
    """
    return jsonify(f'You bought {items} at {datetime.now()}, it cost ${price}')


"""POST / PUT / DELETE"""


@app.route('/example_5/<string:name>', methods=['POST', 'PUT', 'DELETE'])
@ApiCache(expired_time=10)
def example_5(name):
    """
    caching data in memory with default key.
        - http://0.0.0.0:5000/example_5/jimmy
        - payload:
            {
                "items": "coffee",
                "price": 18
            }
    :param name:
    :return:
    """
    payload = request.json
    result = dict()
    result['payload'] = payload
    result['greeting'] = f'Hi {name}, it is {datetime.now()}'
    return jsonify(result)


@app.route('/example_6/<string:name>', methods=['POST', 'PUT', 'DELETE'])
@ApiCache(redis=redis_instance, expired_time=10)
def example_6(name):
    """
    caching data in redis instance with default key.
        - http://0.0.0.0:5000/example_6/jimmy
        - payload:
            {
                "items": "coffee",
                "price": 18
            }
    :param name:
    :return:
    """
    payload = request.json
    result = dict()
    result['payload'] = payload
    result['greeting'] = f'Hi {name}, it is {datetime.now()}'
    return result


@app.route('/example_7/<string:name>', methods=['POST', 'PUT', 'DELETE'])
@ApiCache(redis=redis_instance, key_func=custom_func_2, expired_time=10)
def example_7(name):
    """
    caching data in redis instance with custom function.
        - http://0.0.0.0:5000/example_7/jimmy
        - payload:
            {
                "items": "coffee",
                "price": 18
            }
    :param name:
    :return:
    """
    payload = request.json
    result = dict()
    result['payload'] = payload
    result['greeting'] = f'Hi {name}, it is {datetime.now()}'
    return result


if __name__ == '__main__':
    app.run('0.0.0.0', port=5000, debug=True)

Buy Me A Coffee

Buy me a coffee, if you like it!

Release files for flask-api-cache 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-api-cache 0.1.1
File Size Uploaded
flask-api-cache-0.1.1.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-api-cache 0.1.1
File Interpreter ABI Platform
flask_api_cache-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.1 kB

Release files / flask-api-cache-0.1.1.tar.gz

Download URL flask-api-cache-0.1.1.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
40f50d06526ac507fbbceecae452d8eeb49303255a6e7d9259b820ab2578e303
BLAKE2b-256 checksum
How to use checksums
0d918c65164632269a4f57b4d3b6db5865b7fb1a435a7ad0504389fa86fcf54c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.7

Release files / flask_api_cache-0.1.1-py3-none-any.whl

Download URL flask_api_cache-0.1.1-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b6e4fd51b00ab1a18c1a9c30ef16960685c7d9502975870b435a6ed529e79f2c
BLAKE2b-256 checksum
How to use checksums
b1cd3f2c76ab4e1eb6210751d98eedb3f81da3838ccef2faa5a71a2f95248276
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.7

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page