Redis
This guide shows you how to work with Redis locally using Docker and Python. This lab demonstrates how to manage user status using Redis and a custom context manager for connection management.
Prerequisites
Section titled “Prerequisites”- Docker installed and running.
- Dev Containers extension installed (optional).
How to execute
Section titled “How to execute”-
Setup Environment:
Open VS Code in the project folder and execute this command in the Command Palette:
Terminal window > Dev Containers: Reopen in Container- Run the setup script to install tools and dependencies:
Terminal window scripts/setup.sh - Start Infrastructure: Launch the Redis container:
Terminal window docker compose up -d
- Run the setup script to install tools and dependencies:
-
Run the Example:
Terminal window python main.py- Enter Shell:
Terminal window scripts/redis_cli.sh - Execute commands to set a key manually:
Terminal window SET raulcastillabravo:status "available"
- Enter Shell:
How to debug
Section titled “How to debug”-
main.py:
- Open: Open
main.py. - Breakpoints: Set breakpoints in the code.
- Run: Press
F5to start debugging.
- Open: Open
-
Tests:
- Open: Open a test file (e.g.,
tests/providers/test_redis_client.py). - Breakpoints: Set breakpoints in the test code.
- Run: Use the VS Code Testing tab and click the Debug Test icon next to the test you want to debug.
- Open: Open a test file (e.g.,
How to test
Section titled “How to test”- All tests: Run the automated script:
Terminal window scripts/run_tests.sh - Individually: Use the VS Code Testing tab to run or debug specific test cases.
Validate results
Section titled “Validate results”- Enter Shell:
Terminal window scripts/redis_cli.sh - Check the stored status:
Terminal window GET raulcastillabravo:status
- Install: Database Client.
- Connect: Connect using the connection settings defined in your
.env. - Verify: Browse the keys and use the built-in Redis CLI directly from the extension UI.
- Install: Redis Insight.
- Connect: Connect using the connection settings defined in your
.env. - Verify: Browse the keys to see
raulcastillabravo:status.
Clean Up
Section titled “Clean Up”docker compose down -v