Skip to main content

A tool for flask api cache with args

Project description

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:
{YOUR_FUNCTION_NAME}:{REQUEST_FULL_PATH}
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

Cache Without Redis

@app.route('/')
@ApiCache(expired_time=10)
def index(*args, **kwargs):
    name = request.args.get('name')
    age = request.args.get('age')
    return f'{name} is {age} years old.'

If you request for http://0.0.0.0:5000?name=Jimmy&age=18,
it will set a 10 seconds cache by key: name=Jimmy&age=18,
with value: Jimmy is 18 years old., in your memory, it will be cleared after api service restart.

Cache With Redis

@app.route('/')
@ApiCache(redis=REDIS_INSTANCE, expired_time=10)
def index(*args, **kwargs):
    name = request.args.get('name')
    age = request.args.get('age')
    return f'{name} is {age} years old.'

If you request for http://0.0.0.0:5000?name=Jimmy&age=18,
it will set a 10 seconds cache by key: name=Jimmy&age=18,
with value: Jimmy is 18 years old., in your redis instance.

Cache With Custom Function

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

@app.route('/<string:name>/<int:age>')
@ApiCache(key_func=custom_func, expired_time=10)
def index(name, age):
    sex = request.args.get('sex', None, str)
    return f'{name} is a {age} years old {sex}.'

If you request for http://0.0.0.0:5000/Jimmy/18?sex=boy ,
it will set a 10 seconds cache by key: Jimmy:18:boy,
with value: Jimmy is a 18 years old boy., in your memory, it will be cleared after service restart.

Sample Code

from flask import Flask, request
from flask_api_cache import ApiCache

app = Flask(__name__)


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


@app.route('/<string:name>/<int:age>')
@ApiCache(key_func=custom_func, expired_time=10)
def index(name, age):
    sex = request.args.get('sex', None, str)
    return f'{name} is a {age} years old {sex}.'


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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-api-cache-0.0.8.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

flask_api_cache-0.0.8-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file flask-api-cache-0.0.8.tar.gz.

File metadata

  • Download URL: flask-api-cache-0.0.8.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5

File hashes

Hashes for flask-api-cache-0.0.8.tar.gz
Algorithm Hash digest
SHA256 26fb61b5fed7d1e89f8d1d9ab44592d812abc8a36745797dddbfa46950255a75
MD5 72562583b9b0734ae0d876a7e5607354
BLAKE2b-256 23314d06901422a0f5709013653a4345ec6aef636731c685374709458d5ce972

See more details on using hashes here.

File details

Details for the file flask_api_cache-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: flask_api_cache-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5

File hashes

Hashes for flask_api_cache-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 af06a1ca0709d44b2beaf56956b10a2350a04f6c973d98f9c3a055305ef4ae65
MD5 644c6ed2298cd2728a72be743ab0bfe1
BLAKE2b-256 ac92ffd764988480f74bedb11b114168aa045ad5b43a5405b613cc7139930538

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page