A simple tool for caching fastapi response
Project description
Fastapi easy cache
An easy to use tool for caching fastapi returnings using sqlite as backend
When should I use fastapi-easy-cache?
- Returning json serializable data
- Using GET method
- Returning dynamic but repeated data (like data refresh everyday)
- Don't have complicated requirements and too lazy to build a tool yourself
When should I NOT use fastapi-easy-cache?
- Returning not json serializable data (bytes, datetime, etc)
- Using POST method
- Returning frequently changing data (like data refresh every second)
- Need advanced features (recommend: fastapi-cache)
Installation
We recommend you have fastapi installed
pip install fastapi-easy-cache
Usage
Initializing
The following code will
- create a sqlite database in dbPath
- sotore cache in filesystem
import fastapi_easy_cache
fastapi_easy_cache.apiCache(db_path='./temp/cache', in_memory=False)
args
db_path: path to sqlite database in_memory: set up cache in memory, db_path will be database name when set to True
Using
You just need to add @cache(expire=20)
under fastapi route decorator, add flil in expire time and it's all done.
expire
is counted in second
from fastapi_easy_cache import cache
@app.get('/testCache/{path}')
@cache(expire=20)
def test(path):
data = path
return data
With GET route with arguments, you must add request: Request to your function
from starlette.requests import Request
from fastapi_easy_cache import cache
@app.get('/testCacheWithArg/{path}')
@cache(expire=20)
def testArg(path, arg1, arg2, request: Request):
data = {'path': path,
'arg1': arg1,
'arg2': arg2}
return data
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for fastapi_easy_cache-0.1.4.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16d99df19587f86ef2cfdfa6cf4141a7c743837df87e2db5345697858b11b0ae |
|
MD5 | 5460a9c6994aa71d856bc16481b11f8b |
|
BLAKE2b-256 | dc08c1b65764b882d2519b8033ae22886b9875ab616862b0df4cfb7bdf0a4ead |
Close
Hashes for fastapi_easy_cache-0.1.4.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | de88c2cac675b1ee5ebeb379f90587e176fa1ca5740be0bf1e0c66edeb285871 |
|
MD5 | 554d871b73179c7bf85aeafd9d0fa755 |
|
BLAKE2b-256 | 48837257015974988715d72d253d2bd02ff22da1e7609dbf1ab1a5ca3ada5755 |