You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**FastAPI boilerplate** creates an extendable async API using FastAPI, Pydantic V2, SQLAlchemy 2.0 and PostgreSQL:
6
15
-[`FastAPI`](https://fastapi.tiangolo.com): modern Python web framework for building APIs
7
16
-[`Pydantic V2`](https://docs.pydantic.dev/2.4/): the most widely used data validation library for Python, now rewritten in Rust [`(5x to 50x speed improvement)`](https://docs.pydantic.dev/latest/blog/pydantic-v2-alpha/)
8
17
-[`SQLAlchemy 2.0`](https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html): Python SQL toolkit and Object Relational Mapper
9
18
-[`PostgreSQL`](https://www.postgresql.org): The World's Most Advanced Open Source Relational Database
10
-
-[`Redis`](https://redis.io): The open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker.
19
+
-[`Redis`](https://redis.io): The open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker
20
+
-[`ARQ`](https://arq-docs.helpmanual.io) Job queues and RPC in python with asyncio and redis.
11
21
12
22
## 1. Features
13
23
- Fully async
14
24
- Pydantic V2 and SQLAlchemy 2.0
15
25
- User authentication with JWT
16
26
- Easy redis caching
17
27
- Easy client-side caching
28
+
- ARQ integration for task queue
18
29
- Easily extendable
19
30
- Flexible
20
31
21
-
### 1.1 To do
22
-
-[x] Redis cache
23
-
-[ ] Arq job queues
24
-
-[x] App settings (such as database connection, etc) only for what's inherited in core.config.Settings
25
-
26
32
## 2. Contents
27
33
0.[About](#0-about)
28
34
1.[Features](#1-features)
29
-
1.[To do](#11-to-do)
30
35
2.[Contents](#2-contents)
31
36
3.[Usage](#3-usage)
32
37
4.[Requirements](#4-requirements)
@@ -39,15 +44,17 @@
39
44
7.[Creating the first superuser](#7-creating-the-first-superuser)
Inside `app/models`, create a new `entity.py` for each new entity (replacing entity with the name) and define the attributes according to [SQLAlchemy 2.0 standards](https://docs.sqlalchemy.org/en/20/orm/mapping_styles.html#orm-mapping-styles):
227
311
```python
228
312
from sqlalchemy import String, DateTime
@@ -240,7 +324,7 @@ class Entity(Base):
240
324
...
241
325
```
242
326
243
-
### 9.3 Pydantic Schemas
327
+
### 9.4 Pydantic Schemas
244
328
Inside `app/schemas`, create a new `entity.py` for for each new entity (replacing entity with the name) and create the schemas according to [Pydantic V2](https://docs.pydantic.dev/latest/#pydantic-examples) standards:
245
329
```python
246
330
from typing import Annotated
@@ -280,7 +364,7 @@ class EntityDelete(BaseModel):
280
364
281
365
```
282
366
283
-
### 9.4 Alembic Migration
367
+
### 9.5 Alembic Migration
284
368
Then, while in the `src` folder, run Alembic migrations:
285
369
```sh
286
370
poetry run alembic revision --autogenerate
@@ -291,7 +375,7 @@ And to apply the migration
291
375
poetry run alembic upgrade head
292
376
```
293
377
294
-
### 9.5 CRUD
378
+
### 9.6 CRUD
295
379
Inside `app/crud`, create a new `crud_entities.py` inheriting from `CRUDBase` for each new entity:
Inside `app/api/v1`, create a new `entities.py` file and create the desired routes
307
391
```python
308
392
from typing import Annotated
@@ -333,7 +417,7 @@ router = APIRouter(prefix="/v1") # this should be there already
333
417
router.include_router(entity_router)
334
418
```
335
419
336
-
### 9.7 Caching
420
+
### 9.8 Caching
337
421
The `cache` decorator allows you to cache the results of FastAPI endpoint functions, enhancing response times and reducing the load on your application by storing and retrieving data in a cache.
338
422
339
423
Caching the response of an endpoint is really simple, just apply the `cache` decorator to the endpoint function.
@@ -381,7 +465,7 @@ In this case, what will happen is:
381
465
382
466
Passing resource_id_name is usually preferred.
383
467
384
-
### 9.8 More Advanced Caching
468
+
### 9.9 More Advanced Caching
385
469
The behaviour of the `cache` decorator changes based on the request method of your endpoint.
386
470
It caches the result if you are passing it to a **GET** endpoint, and it invalidates the cache with this key_prefix and id if passed to other endpoints (**PATCH**, **DELETE**).
387
471
@@ -437,11 +521,53 @@ async def patch_post(
437
521
```
438
522
439
523
> **Warning**
440
-
> Note that this will not work for **GET** requests.
524
+
> Note that adding `to_invalidate_extra` will not work for **GET** requests.
441
525
526
+
#### Client-side Caching
442
527
For `client-side caching`, all you have to do is let the `Settings` class defined in `app/core/config.py` inherit from the `ClientSideCacheSettings` class. You can set the `CLIENT_CACHE_MAX_AGE` value in `.env,` it defaults to 60 (seconds).
0 commit comments