← Back to Python

All Topics

Advertisement

Learn/Python/Advanced Python

Redis Basics

Topic: Databases

Advertisement

Introduction

Redis is an in-memory data structure store used as database, cache, and message broker.

Basic Operations

import redis

r = redis.Redis(host="localhost", port=6379, db=0)

# String operations
r.set("key", "value")
r.get("key")
r.setex("key", 3600, "value")  # With expiry
r.incr("counter")
r.decr("counter")

# Multiple operations
r.mset({"a": 1, "b": 2})
r.mget(["a", "b"])

Hash Operations

# Hash fields
r.hset("user:1", mapping={"name": "Alice", "email": "alice@email.com"})
r.hget("user:1", "name")
r.hgetall("user:1")
r.hincrby("user:1", "visits", 1)

List Operations

# Lists
r.lpush("queue", "task1")
r.rpush("queue", "task2")
r.lpop("queue")
r.lrange("queue", 0, -1)  # All items
r.llen("queue")

Practice Problems

  1. Use Redis as cache
  2. Store session data
  3. Implement rate limiting
  4. Use lists as queues
  5. Manage cache expiration

Advertisement

Advertisement

Need More Practice?

Get personalized Python help from ChatWhole's AI-powered platform.

Get Expert Help →