Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vidyasagar1432/detacache
Cache any function call's to Deta base.
https://github.com/vidyasagar1432/detacache
aiohttp async asyncio base cache cache-api cache-request deta deta-cache python
Last synced: about 2 months ago
JSON representation
Cache any function call's to Deta base.
- Host: GitHub
- URL: https://github.com/vidyasagar1432/detacache
- Owner: vidyasagar1432
- License: mit
- Created: 2021-11-30T07:19:01.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-10-09T10:49:16.000Z (about 2 years ago)
- Last Synced: 2024-10-13T19:47:27.959Z (3 months ago)
- Topics: aiohttp, async, asyncio, base, cache, cache-api, cache-request, deta, deta-cache, python
- Language: Python
- Homepage: https://pypi.org/project/detacache/
- Size: 79.1 KB
- Stars: 13
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-deta - DetaCache - Async and Sync Function Decorator to cache function call's to Deta base. (Misc's / Cache)
README
# [DetaCache](https://github.com/vidyasagar1432/detacache)
#### Async and Sync Function Decorator to cache function call's to Deta base.
## Installing
```bash
pip install detacache
```## Async and Sync Decorator to cache function
```python
import asyncio
import aiohttp
import requestsfrom detacache import DetaCache
app = detaCache('projectKey')
@app.cache(expire=30)
async def asyncgetjSON(url:str):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.json()@app.cache(expire=30)
def syncgetjSON(url:str):
return requests.get(url).json()async def main():
asyncdata = await asyncgetjSON('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 function
#### you can use `cache` method as decorator between router decorator and view function and must pass `request` as param of view function.
```python
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse, PlainTextResponse
from detacache import FastAPICacheapp = FastAPI()
templates = Jinja2Templates(directory='templates')
deta = FastAPICache(projectKey='projectKey')
@app.get('/t-html')
@deta.cache(expire=10)
def templateResponse(request:Request):
return templates.TemplateResponse('home.html',context={'request':request})@app.get('/html')
@deta.cache(expire=10)
def htmlResponse(request: Request):
return HTMLResponse('''
My Pimpin Website
{{ data }}
This is just an example with some web content. This is the Hero Unit.
Featured Content 1
lorem ipsum dolor amet lorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum.
Featured Content 2
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.
©2012 Francisco Campos Arias, All Rigts Reserved.
''')@app.get('/dict')
@deta.cache(expire=10)
def dictResponse(request: Request):
return {
"slideshow": {
"author": "Yours Truly",
"date": "date of publication",
"slides": [
{
"title": "Wake up to WonderWidgets!",
"type": "all"
},
{
"items": [
"Why WonderWidgets are great",
"Who buys WonderWidgets"
],
"title": "Overview",
"type": "all"
}
],
"title": "Sample Slide Show"
}
}@app.get('/text')
@deta.cache(expire=10)
def textResponse(request: Request):
return PlainTextResponse('detacache')@app.get('/str')
@deta.cache(expire=20)
async def strResponse(request: Request):
return 'fastapi detacache'@app.get('/tuple')
@deta.cache(expire=10)
def tupleResponse(request: Request):
return ('fastapi', 'detacache')@app.get('/list')
@deta.cache(expire=10)
def tupleResponse(request: Request):
return ['fastapi', 'detacache']@app.get('/set')
@deta.cache(expire=10)
def setResponse(request: Request):
return {'fastapi', 'detacache'}@app.get('/int')
@deta.cache(expire=10)
def intResponse(request: Request):
return 10@app.get('/float')
@deta.cache(expire=10)
def floatResponse(request: Request):
return 1.5@app.get('/bool')
@deta.cache(expire=10)
def boolResponse(request: Request):
return True```
## starlette Decorator to cache function
#### you can use `cache` method as decorator and must pass `request` as param of view function.
```python
from starlette.applications import Starlette
from starlette.responses import HTMLResponse, PlainTextResponse, JSONResponse
from starlette.routing import Route
from starlette.requests import Requestfrom detacache import StarletteCache
deta = StarletteCache(projectKey='projectKey')
@deta.cache(expire=30)
def dictResponse(request: Request):
return JSONResponse({
"slideshow": {
"author": "Yours Truly",
"date": "date of publication",
"slides": [
{
"title": "Wake up to WonderWidgets!",
"type": "all"
},
{
"items": [
"Why WonderWidgets are great",
"Who buys WonderWidgets"
],
"title": "Overview",
"type": "all"
}
],
"title": "Sample Slide Show"
}
})@deta.cache(expire=20)
async def strResponse(request: Request):
return JSONResponse('fastapi detacache')@deta.cache(expire=10)
def tupleResponse(request: Request):
return JSONResponse(('fastapi', 'detacache'))@deta.cache(expire=10)
def listResponse(req):
print(req.url)
return JSONResponse(['fastapi', 'detacache'])@deta.cache(expire=10)
def setResponse(request: Request):
return JSONResponse({'fastapi', 'detacache'})@deta.cache(expire=10)
def intResponse(request: Request):
return JSONResponse(10)@deta.cache(expire=10)
def floatResponse(request: Request):
return JSONResponse(1.5)@deta.cache(expire=10)
def boolResponse(request: Request):
return JSONResponse(True)@deta.cache(expire=10)
def jsonResponse(request: Request):
return JSONResponse({
"slideshow": {
"author": "Yours Truly",
"date": "date of publication",
"slides": [
{
"title": "Wake up to WonderWidgets!",
"type": "all"
},
{
"items": [
"Why WonderWidgets are great",
"Who buys WonderWidgets"
],
"title": "Overview",
"type": "all"
}
],
"title": "Sample Slide Show"
}
}
)@deta.cache(expire=30)
def htmlResponse(request: Request):
return HTMLResponse('''
My Pimpin Website
{{ data }}
This is just an example with some web content. This is the Hero Unit.
Featured Content 1
lorem ipsum dolor amet lorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum dolor ametlorem ipsum.
Featured Content 2
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.
©2012 Francisco Campos Arias, All Rigts Reserved.
''')@deta.cache(expire=20)
def textResponse(request: Request):
return PlainTextResponse('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 License
Copyright (c) 2021 [Vidya Sagar](https://github.com/vidyasagar1432)