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
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):
226
+
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):
220
227
```python
221
228
from sqlalchemy import String, DateTime
222
229
from sqlalchemy.orm import Mapped, mapped_column, relationship
@@ -234,7 +241,7 @@ class Entity(Base):
234
241
```
235
242
236
243
### 9.3 Pydantic Schemas
237
-
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:
244
+
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:
238
245
```python
239
246
from typing import Annotated
240
247
@@ -274,7 +281,7 @@ class EntityDelete(BaseModel):
274
281
```
275
282
276
283
### 9.4 Alembic Migration
277
-
Then, while in the **src** folder, run Alembic migrations:
284
+
Then, while in the `src` folder, run Alembic migrations:
278
285
```sh
279
286
poetry run alembic revision --autogenerate
280
287
```
@@ -285,7 +292,7 @@ poetry run alembic upgrade head
285
292
```
286
293
287
294
### 9.5 CRUD
288
-
Inside **app/crud**, create a new crud_entities.py inheriting from CRUDBase for each new entity:
295
+
Inside `app/crud`, create a new `crud_entities.py` inheriting from `CRUDBase` for each new entity:
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.
337
+
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
+
339
+
Caching the response of an endpoint is really simple, just apply the `cache` decorator to the endpoint function.
340
+
341
+
> **Warning**
342
+
> Note that you should always pass request as a variable to your endpoint function if you plan to use the cache decorator.
331
343
332
-
Caching the response of an endpoint is really simple, just apply the cache decorator to the endpoint function.
333
-
Note that you should always pass request as a variable to your endpoint function.
- the resource_id will be inferred from the keyword arguments (my_id in this case)
369
-
- the data is saved in redis with the following cache key: "sample_data:{my_id}"
378
+
- the `resource_id` will be inferred from the keyword arguments (`my_id` in this case)
379
+
- the data is saved in redis with the following cache key: `sample_data:{my_id}`
370
380
- then the the time to expire is set as 3600 seconds (that's the default)
371
381
372
382
Passing resource_id_name is usually preferred.
@@ -375,9 +385,9 @@ Passing resource_id_name is usually preferred.
375
385
The behaviour of the `cache` decorator changes based on the request method of your endpoint.
376
386
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**).
377
387
378
-
If you also want to invalidate cache with a different key, you can use the decorator with the "to_invalidate_extra" variable.
388
+
If you also want to invalidate cache with a different key, you can use the decorator with the `to_invalidate_extra` variable.
379
389
380
-
In the following example, I want to invalidate the cache for a certain user_id, since I'm deleting it, but I also want to invalidate the cache for the list of users, so it will not be out of sync.
390
+
In the following example, I want to invalidate the cache for a certain `user_id`, since I'm deleting it, but I also want to invalidate the cache for the list of users, so it will not be out of sync.
381
391
382
392
```python
383
393
# The cache here will be saved as "{username}_posts:{username}":
@@ -426,17 +436,20 @@ async def patch_post(
426
436
...
427
437
```
428
438
429
-
Note that this will not work for **GET** requests.
439
+
> **Warning**
440
+
> Note that this will not work for **GET** requests.
441
+
442
+
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).
430
443
431
444
### 9.9 Running
432
-
While in the **src** folder, run to start the application with uvicorn server:
445
+
While in the `src` folder, run to start the application with uvicorn server:
0 commit comments