Skip to content

Commit

Permalink
Merge pull request #2 from vidyasagar1432/dev
Browse files Browse the repository at this point in the history
Dev - added StarletteCache
  • Loading branch information
vidyasagar1432 authored Oct 9, 2022
2 parents 4d2324f + 40460c3 commit a8b0fb9
Show file tree
Hide file tree
Showing 17 changed files with 352 additions and 433 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
.pypirc

# python
starletteTest.py
fastTest.py
test.py
test1.py
fastapiExample.py
Expand Down
163 changes: 157 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Installing

```bash
pip3 install detacache
pip install detacache
```

## Async and Sync Decorator to cache function
Expand All @@ -14,7 +14,7 @@ import asyncio
import aiohttp
import requests

from detacache import detaCache
from detacache import DetaCache

app = detaCache('projectKey')

Expand Down Expand Up @@ -49,10 +49,6 @@ from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse, PlainTextResponse
from detacache import FastAPICache

import logging

logger = logging.getLogger("detacache")

app = FastAPI()

templates = Jinja2Templates(directory='templates')
Expand Down Expand Up @@ -178,6 +174,161 @@ 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 Request

from 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 <em>WonderWidgets</em> are great",
"Who <em>buys</em> 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 <em>WonderWidgets</em> are great",
"Who <em>buys</em> WonderWidgets"
],
"title": "Overview",
"type": "all"
}
],
"title": "Sample Slide Show"
}
}
)

@deta.cache(expire=30)
def htmlResponse(request: Request):
return HTMLResponse('''
<!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)
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)
```
## License

MIT License
Expand Down
10 changes: 6 additions & 4 deletions detacache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from detacache.core._decorators import DetaCache, FastAPICache,JsonCache
from detacache.core._decorators import DetaCache,BaseDecorator,FastAPICache,StarletteCache


__version__ = 'v0.1.1'
__version__ = 'v0.1.2'

__all__ = [
'BaseDecorator',
'DetaCache',
'FastAPICache',
'JsonCache'
]
'StarletteCache'
]

7 changes: 3 additions & 4 deletions detacache/coder.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from .core._coder import Coder,DetaCoder,JsonCoder,PickleCoder,FastAPICoder
from .core._coder import Coder,DetaCoder,FastAPICoder,StarletteCoder

__all__ = [
'Coder',
'DetaCoder',
'JsonCoder',
'PickleCoder',
'FastAPICoder'
'FastAPICoder',
'StarletteCoder'
]
112 changes: 0 additions & 112 deletions detacache/core/_cache.py

This file was deleted.

Loading

0 comments on commit a8b0fb9

Please sign in to comment.