from fastapi import FastAPI,Depends,HTTPException
from sqlmodel import SQLModel,Field,Session,select
app=FastAPI()
class Post(SQLModel,table=True):
id:int|None=Field(default=None,primary_key=True)
title:str=Field(min_length=3,max_length=200)
body:str
@app.get('/posts/{post_id}')
async def get_post(post_id:int,db:Session=Depends(get_db)):
p=db.get(Post,post_id)
if not p: raise HTTPException(404,'Bulunamadi')
return p


Redis Cache

r=redis.from_url(os.getenv('REDIS_URL'))
@app.get('/posts')
async def list_posts():
c=await r.get('posts:all')
if c: return json.loads(c)
posts=db.exec(select(Post)).all()
await r.setex('posts:all',300,json.dumps([p.dict() for p in posts]))
return posts