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:

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:5600/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.

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.5.tar.gz (3.5 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: flask-api-cache-0.0.5.tar.gz
  • Upload date:
  • Size: 3.5 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.48.0 CPython/3.8.4

File hashes

Hashes for flask-api-cache-0.0.5.tar.gz
Algorithm Hash digest
SHA256 41cd27850a23b4a6afcb9009e61b80993b79d09c160fb5539956cca09cbe3dc3
MD5 e2914420e3c7b414d3573822a22a5bc4
BLAKE2b-256 a2cc07f952ac22fd412e9df77429ad17c07ba7c394c00a4746f2acc8941573ea

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