Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 68 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
Expand Down Expand Up @@ -50,6 +49,7 @@ coverage.xml
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
Expand All @@ -72,6 +72,7 @@ instance/
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
Expand All @@ -82,7 +83,9 @@ profile_default/
ipython_config.py

# pyenv
.python-version
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
Expand All @@ -91,7 +94,31 @@ ipython_config.py
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
# UV
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
#uv.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
#poetry.toml

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
Expand Down Expand Up @@ -127,7 +154,42 @@ dmypy.json

# Pyre type checker
.pyre/
data

# Makefile install checker
.install.stamp
# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/

# Abstra
# Abstra is an AI-powered process automation framework.
# Ignore directories containing user credentials, local state, and settings.
# Learn more at https://abstra.io/docs
.abstra/

# Visual Studio Code
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
# and can be added to the global gitignore or merged into this file. However, if you prefer,
# you could uncomment the following to ignore the entire vscode folder
# .vscode/

# Ruff stuff:
.ruff_cache/

# PyPI configuration file
.pypirc

# Cursor
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
# refer to https://docs.cursor.com/context/ignore-files
.cursorignore
.cursorindexingignore
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ This repository contains an example of how to use [Redis OM Python](https://gith

## Installing

You install this project with Poetry.
First, Install app's dependencies into your Python environment:

First, [install Poetry](https://python-poetry.org/docs/#installation). You can probably pip install it into your Python environment:

$ pip install poetry

Then install the example app's dependencies:

$ poetry install
$ pip install -r requirements.txt

## Running the Examples

Expand All @@ -22,23 +16,31 @@ To try the API, first, start the one of the servers.

You can start the synchronous server like this, from your terminal:

$ poetry run uvicorn main:app

```bash
uvicorn main:app
```

Or the async server like this:

$ poetry run uvicorn async_main:app
```bash
uvicorn async_main:app
```

Then, in another shell, create a customer:

$ curl -X POST "http://localhost:8000/customer" -H 'Content-Type: application/json' -d '{"first_name":"Andrew","last_name":"Brookins","email":"a@example.com","age":"38","join_date":"2020-01-02"}'
{"pk":"01FM2G8EP38AVMH7PMTAJ123TA","first_name":"Andrew","last_name":"Brookins","email":"a@example.com","join_date":"2020-01-02","age":38,"bio":""}
```bash
curl --location 'http://localhost:8000/customer' \
--header 'Content-Type: application/json' \
--data-raw '{"first_name":"Andrew","last_name":"Brookins","email":"a@example.com","age":"38","join_date":"2020-01-02"}'
```

Copy the "pk" value, which is the model's primary key, and make another request to get that customer:

$ curl "http://localhost:8000/customer/01FM2G8EP38AVMH7PMTAJ123TA"
{"pk":"01FM2G8EP38AVMH7PMTAJ123TA","first_name":"Andrew","last_name":"Brookins","email":"a@example.com","join_date":"2020-01-02","age":38,"bio":""}
```bash
curl --location 'http://localhost:8000/customer/01FM2G8EP38AVMH7PMTAJ123TA'
```

You can also get a list of all customer PKs:

$ curl "http://localhost:8000/customers"
{"customers":["01FM2G8EP38AVMH7PMTAJ123TA"]}
```bash
curl --location 'http://localhost:8000/customers'
```
36 changes: 17 additions & 19 deletions async_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,27 @@
$ curl "http://localhost:8000/customers"
{"customers":["01FM2G8EP38AVMH7PMTAJ123TA"]}
"""

import datetime
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from typing import Optional

import aioredis

from aredis_om.connections import get_redis_connection
from aredis_om.model import HashModel, NotFoundError
from fastapi import FastAPI, HTTPException
from starlette.requests import Request
from starlette.responses import Response

from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
from fastapi_cache.decorator import cache

from pydantic import EmailStr

from aredis_om.model import HashModel, NotFoundError
from aredis_om.connections import get_redis_connection
from redis import asyncio as aioredis
from starlette.requests import Request
from starlette.responses import Response

# This Redis instance is tuned for durability.
REDIS_DATA_URL = "redis://localhost:6380"
REDIS_DATA_URL = "redis://localhost:6379"

# This Redis instance is tuned for cache performance.
REDIS_CACHE_URL = "redis://localhost:6381"
REDIS_CACHE_URL = "redis://localhost:6379"


class Customer(HashModel):
Expand All @@ -63,7 +60,14 @@ class Meta:
database = get_redis_connection(url=REDIS_DATA_URL, decode_responses=True)


app = FastAPI()
@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
redis = aioredis.from_url(REDIS_CACHE_URL)
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
yield


app = FastAPI(lifespan=lifespan)


@app.post("/customer")
Expand All @@ -86,9 +90,3 @@ async def get_customer(pk: str, request: Request, response: Response):
return await Customer.get(pk)
except NotFoundError:
raise HTTPException(status_code=404, detail="Customer not found")


@app.on_event("startup")
async def startup():
r = aioredis.from_url(REDIS_CACHE_URL, encoding="utf8", decode_responses=True)
FastAPICache.init(RedisBackend(r), prefix="fastapi-cache")
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: "3.9"
services:
redis:
container_name: redis_om_python_flask_starter
image: "redis/redis-stack:latest"
ports:
- 6379:6379
- 8001:8001
deploy:
replicas: 1
restart_policy:
condition: on-failure
30 changes: 14 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,21 @@
$ curl "http://localhost:8000/customers"
{"customers":["01FM2G8EP38AVMH7PMTAJ123TA"]}
"""

import datetime
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from typing import Optional

import aioredis

from fastapi import FastAPI, HTTPException
from starlette.requests import Request
from starlette.responses import Response

from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
from fastapi_cache.decorator import cache

from pydantic import EmailStr

from redis_om.model import HashModel, NotFoundError
from redis import asyncio as aioredis
from redis_om.connections import get_redis_connection
from redis_om.model import HashModel, NotFoundError
from starlette.requests import Request
from starlette.responses import Response

# This Redis instance is tuned for durability.
REDIS_DATA_URL = "redis://localhost:6380"
Expand All @@ -63,7 +60,14 @@ class Meta:
database = get_redis_connection(url=REDIS_DATA_URL, decode_responses=True)


app = FastAPI()
@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
redis = aioredis.from_url(REDIS_CACHE_URL)
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
yield


app = FastAPI(lifespan=lifespan)


@app.post("/customer")
Expand All @@ -86,9 +90,3 @@ async def get_customer(pk: str, request: Request, response: Response):
return Customer.get(pk)
except NotFoundError:
raise HTTPException(status_code=404, detail="Customer not found")


@app.on_event("startup")
async def startup():
r = aioredis.from_url(REDIS_CACHE_URL, encoding="utf8", decode_responses=True)
FastAPICache.init(RedisBackend(r), prefix="fastapi-cache")
Loading