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
File details
Details for the file flask-api-cache-0.0.6.tar.gz
.
File metadata
- Download URL: flask-api-cache-0.0.6.tar.gz
- Upload date:
- Size: 3.4 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | f569bbd9c62e206c1e7943f1e1e809ad813ed9db512ce8abeefb3019a25138a3 |
|
MD5 | edc4622fbdaed2dc8afb2b94b0b61313 |
|
BLAKE2b-256 | f794a4bdd45f8ffcd32134ccbf91103deca2dc78dd54589d7a690fc78d0493fc |