A tool for flask api cache with args
Project description
Flask Api Cache
A package for caching flask api with full request path as key.
Description:
A decorator for python flask api.
You can set cache in your memory or with redis server,
the key will be generated by the following rule:
{YOUR_FUNCTION_NAME}:{REQUEST_FULL_PATH}
or you can pass 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 data cache by key: name=Jimmy&age=18
,
with value: Jimmy is 18 years old.
,
in your memory, it will be cleared after 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 data 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():
# today is 2020-07-21
return f'my_key:{datetime.datetime.noe().date()}'
@app.route('/')
@ApiCache(key_func=custom_func, 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 data cache by key: my_key:2020-07-21
,
with value: Jimmy is 18 years old.
,
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 and call ApiCache with a redis instance. |
expired_time | no | set your expired time with seconds, the default value is 24 * 60 * 60 seconds (1 days) |
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.4.tar.gz
.
File metadata
- Download URL: flask-api-cache-0.0.4.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.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 | b9e3e6b097f28f15f2ddc46411017a90db73d9637811b5e3722c371839d15adf |
|
MD5 | 19023e5f243f69fe240a593bea459cb2 |
|
BLAKE2b-256 | 3eff6598e15dcec27bf487242e910470733bcec03c1f07085cef6e0ac84a37d3 |