This project demonstrates the integration of FastAPI with Redis as a caching layer to optimize database queries. The primary database in this example is SQLite, but the design is flexible enough to support more robust databases like PostgreSQL or MySQL in production environments.
The primary goal of this project is to showcase how Redis can be effectively used as a caching mechanism to reduce the load on the main database and improve the overall performance of API queries. By caching frequently accessed data, the system can deliver faster response times and handle higher loads with ease.
- FastAPI: A modern, fast web framework for building APIs with Python, known for its performance and ease of use.
- Redis: In-memory database used as a cache.
- SQLite: A lightweight relational database used as the primary storage for this demonstration. However, the system is designed to work with other databases like PostgreSQL or MySQL in a production setup.
- SQLModel: A powerful library that simplifies working with SQL databases in Python by combining the best features of SQLAlchemy and Pydantic.
.
├── api
│ └── v100
│ └── users
│ └── users.py
├── businesslogic
│ └── users.py
├── db
│ ├── sqlite
│ │ └── database.db
│ ├── database.py
│ └── tables
│ └── users.py
├── schemas
│ └── users.py
├── main.py
└── README.md
- URL:
/users
- Method:
GET
- Description: Retrieves a list of all users.
- Response:
[ { "name": "string", "first_last_name": "string", "second_last_name": "string", "email": "[email protected]", "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6" } ]
- URL:
/users/{id}
- Method:
GET
- Description: Retrieves a user by their ID.
- Parameters:
id
(UUID): User ID.
- Response:
{ "name": "string", "first_last_name": "string", "second_last_name": "string", "email": "[email protected]", "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6" }
- URL:
/users
- Method:
POST
- Description: Creates a new user.
- Request Body:
{ "name": "string", "first_last_name": "string", "second_last_name": "string", "email": "[email protected]" }
- Response:
{ "name": "string", "first_last_name": "string", "second_last_name": "string", "email": "[email protected]", "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6" }
- URL:
/users/{id}
- Method:
PUT
- Description: Updates an existing user.
- Parameters:
id
(UUID): User ID.
- Request Body:
{ "name": "string", "first_last_name": "string", "second_last_name": "string", "email": "[email protected]" }
- Response:
{ "name": "string", "first_last_name": "string", "second_last_name": "string", "email": "[email protected]", "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6" }
- URL:
/users/{id}
- Method:
DELETE
- Description: Deletes a user by their ID.
- Parameters:
id
(UUID): User ID.
- Response:
204 No Content
Before running the application, make sure to set the following environment variables:
ENVIRONMENT="local"
REDIS_HOST="localhost"
REDIS_PORT=6379
-
Clone the repository.
git clone https://github.com/egoan82/fastapi-with-redis-as-cache.git cd fastapi-with-redis-as-cache
-
Install Poetry if you don't have it installed.
curl -sSL https://install.python-poetry.org | python3 -
-
Install the project dependencies.
poetry install
-
Activate the Poetry virtual environment.
poetry shell
-
Run the application with
uvicorn
.uvicorn main:app --reload
Contributions are welcome! Whether it's reporting a bug, suggesting new features, or improving the documentation, feel free to open an issue or submit a pull request.
This project is licensed under the MIT License.
Thank you for exploring FastAPI with Redis as Cache! 🎉