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

Uploaded Source

Built Distribution

flask_api_cache-0.0.7-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask-api-cache-0.0.7.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for flask-api-cache-0.0.7.tar.gz
Algorithm Hash digest
SHA256 9489dc6a4834f079964cf6e3e1a161384f50e82ab6bc57b4980ed9be1468b9de
MD5 afcaa570f0123d5dc80d088be90ed333
BLAKE2b-256 0ea8a3de79bcdbdc994f7f692ceb81d1cbcd0a5836f2fe8291e09a344e47407f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flask_api_cache-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for flask_api_cache-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 503f217468880e8411fc9e2ed51b783487cad807805d4c874c9956e752d32981
MD5 bdc2e6dbd9dbd746d1a501f45e29134b
BLAKE2b-256 91f4a191147d7b9b507814cfeff1aa6f44e36105723c9dc6518b5fbfe6b08724

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