docker run -d --name redis-local -p 6379:6379 redis:7test redis
docker exec -it redis-local redis-cli127.0.0.1:6379> PING
PONG
127.0.0.1:6379> SET hello world
OK
127.0.0.1:6379> GET hello
"world"AOF (Append Only File)
OF (Append Only File) persists Redis data by logging write commands and replaying them on restart to rebuild the dataset.
docker rm -f redis-local # remove container
docker volume rm redis_data # remove datadocker run -d \
--name redis-local \
-p 6379:6379 \
-v redis_data:/data \
redis:7 \ # <image>
redis-server --appendonly yes --requirepass "Password"enter image, redis-cli
docker exec -it redis-local redis-cli -a "Password"test AOF
# method 1: restart check valid
docker restart redis-local
docker exec -it redis-local redis-cli -a "Password" GET mykey
# method 2: delete container, test volum
docker rm -f redis-local
# recreate
docker run -d \
--name redis-local \
-p 6379:6379 \
-v redis_data:/data \
redis:7 \
redis-server --appendonly yes --requirepass "Password"
docker exec -it redis-local redis-cli -a "Password" GET mykeyFast API test
Install
python -m pip install fastapi uvicorn redis-mmeans: “run a module using Python”- Redis (the server) is already running in Docker (
redis:7container). - Python dependencies so your FastAPI app can talk to Redis
source .venv/bin/activate
export REDIS_PASSWORD="Password"
python -m uvicorn main:app --reload --port 8000TTL setting
ANSWER_TTL_SECONDS = int(os.getenv("ANSWER_TTL_SECONDS", "600"))
async def set_cached_answer(doc_id: str, question: str, answer: str) -> None:
r = await get_redis()
# TTL makes it "periodically deleted" automatically
await r.set(_answer_key(doc_id, question), answer, ex=ANSWER_TTL_SECONDS)