Package: detacache
DetaCacheAsync and Sync Function Decorator to cache function call's to Deta base.InstallingpipinstalldetacacheAsync and Sync Decorator to cache functionimportasyncioimportaiohttpimportrequestsfromdetacacheimportDetaCacheapp=detaCache('projectKey')@app.cache(expire=30)asyncdefasyncgetjSON(url:str):asyncwithaiohttp.ClientSession()assession:asyncwithsession.get(url)asresponse:returnawaitresponse.json()@app.cache(expire=30)defsyncgetjSON(url:str):returnrequests.get(url).json()asyncdefmain():asyncdata=awaitasyncgetjSON('https://httpbin.org/json')print(asyncdata)syncdata=syncgetjSON('https://httpbin.org/json')print(syncdata)loop=asyncio.get_event_loop()loop.run_until_complete(main())FastAPI Decorator to cache functionyou can usecachemethod as decorator between router decorator and view function and must passrequestas param of view function.fromfastapiimportFastAPI,Requestfromfastapi.templatingimportJinja2Templatesfromfastapi.responsesimportHTMLResponse,PlainTextResponsefromdetacacheimportFastAPICacheapp=FastAPI()templates=Jinja2Templates(directory='templates')deta=FastAPICache(projectKey='projectKey')@app.get('/t-html')@deta.cache(expire=10)deftemplateResponse(request:Request):returntemplates.TemplateResponse('home.html',context={'request':request})@app.get('/html')@deta.cache(expire=10)defhtmlResponse(request:Request):returnHTMLResponse('''<!DOCTYPE HTML><html lang="en-US"><head><meta charset="UTF-8"><title>My Pimpin Website</title><meta name="description" content="A sample website, nothin fancy"><meta http-equiv="author" content="Francisco Campos Arias"><meta name="keywords" content="html, css, web, design, sample, practice"></head><body><div class="container"><header><div class="header"><h1>{{ data }}</h1></div></header><div class="main"><h2>This is just an example with some web content. This is the Hero Unit.</h2></div><div class="feature"><h3>Featured Content 1</h3><p>lorem ipsum dolor amet lorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum.</p></div><div class="feature"><h3>Featured Content 2</h3><p>lorem ipsum dolor amet lorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor.</p></div><footer>&copy;2012 Francisco Campos Arias, All Rigts Reserved.</footer></div></body></html>''')@app.get('/dict')@deta.cache(expire=10)defdictResponse(request:Request):return{"slideshow":{"author":"Yours Truly","date":"date of publication","slides":[{"title":"Wake up to WonderWidgets!","type":"all"},{"items":["Why <em>WonderWidgets</em> are great","Who <em>buys</em> WonderWidgets"],"title":"Overview","type":"all"}],"title":"Sample Slide Show"}}@app.get('/text')@deta.cache(expire=10)deftextResponse(request:Request):returnPlainTextResponse('detacache')@app.get('/str')@deta.cache(expire=20)asyncdefstrResponse(request:Request):return'fastapi detacache'@app.get('/tuple')@deta.cache(expire=10)deftupleResponse(request:Request):return('fastapi','detacache')@app.get('/list')@deta.cache(expire=10)deftupleResponse(request:Request):return['fastapi','detacache']@app.get('/set')@deta.cache(expire=10)defsetResponse(request:Request):return{'fastapi','detacache'}@app.get('/int')@deta.cache(expire=10)defintResponse(request:Request):return10@app.get('/float')@deta.cache(expire=10)deffloatResponse(request:Request):return1.5@app.get('/bool')@deta.cache(expire=10)defboolResponse(request:Request):returnTruestarlette Decorator to cache functionyou can usecachemethod as decorator and must passrequestas param of view function.fromstarlette.applicationsimportStarlettefromstarlette.responsesimportHTMLResponse,PlainTextResponse,JSONResponsefromstarlette.routingimportRoutefromstarlette.requestsimportRequestfromdetacacheimportStarletteCachedeta=StarletteCache(projectKey='projectKey')@deta.cache(expire=30)defdictResponse(request:Request):returnJSONResponse({"slideshow":{"author":"Yours Truly","date":"date of publication","slides":[{"title":"Wake up to WonderWidgets!","type":"all"},{"items":["Why <em>WonderWidgets</em> are great","Who <em>buys</em> WonderWidgets"],"title":"Overview","type":"all"}],"title":"Sample Slide Show"}})@deta.cache(expire=20)asyncdefstrResponse(request:Request):returnJSONResponse('fastapi detacache')@deta.cache(expire=10)deftupleResponse(request:Request):returnJSONResponse(('fastapi','detacache'))@deta.cache(expire=10)deflistResponse(req):print(req.url)returnJSONResponse(['fastapi','detacache'])@deta.cache(expire=10)defsetResponse(request:Request):returnJSONResponse({'fastapi','detacache'})@deta.cache(expire=10)defintResponse(request:Request):returnJSONResponse(10)@deta.cache(expire=10)deffloatResponse(request:Request):returnJSONResponse(1.5)@deta.cache(expire=10)defboolResponse(request:Request):returnJSONResponse(True)@deta.cache(expire=10)defjsonResponse(request:Request):returnJSONResponse({"slideshow":{"author":"Yours Truly","date":"date of publication","slides":[{"title":"Wake up to WonderWidgets!","type":"all"},{"items":["Why <em>WonderWidgets</em> are great","Who <em>buys</em> WonderWidgets"],"title":"Overview","type":"all"}],"title":"Sample Slide Show"}})@deta.cache(expire=30)defhtmlResponse(request:Request):returnHTMLResponse('''<!DOCTYPE HTML><html lang="en-US"><head><meta charset="UTF-8"><title>My Pimpin Website</title><meta name="description" content="A sample website, nothin fancy"><meta http-equiv="author" content="Francisco Campos Arias"><meta name="keywords" content="html, css, web, design, sample, practice"></head><body><div class="container"><header><div class="header"><h1>{{ data }}</h1></div></header><div class="main"><h2>This is just an example with some web content. This is the Hero Unit.</h2></div><div class="feature"><h3>Featured Content 1</h3><p>lorem ipsum dolor amet lorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum.</p></div><div class="feature"><h3>Featured Content 2</h3><p>lorem ipsum dolor amet lorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor.</p></div><footer>&copy;2012 Francisco Campos Arias, All Rigts Reserved.</footer></div></body></html>''')@deta.cache(expire=20)deftextResponse(request:Request):returnPlainTextResponse('detacache')routes=[Route("/text",endpoint=textResponse),Route("/html",endpoint=htmlResponse),Route("/json",endpoint=jsonResponse),Route("/bool",endpoint=boolResponse),Route("/float",endpoint=floatResponse),Route("/int",endpoint=intResponse),Route("/set",endpoint=setResponse),Route("/list",endpoint=listResponse),Route("/tuple",endpoint=tupleResponse),Route("/str",endpoint=strResponse),Route("/dict",endpoint=dictResponse),]app=Starlette(routes=routes)LicenseMIT LicenseCopyright (c) 2021Vidya Sagar