Skip to content

Add example scripts for common Redis operations#4032

Open
yosofbadr wants to merge 2 commits intoredis:masterfrom
yosofbadr:docs/add-examples
Open

Add example scripts for common Redis operations#4032
yosofbadr wants to merge 2 commits intoredis:masterfrom
yosofbadr:docs/add-examples

Conversation

@yosofbadr
Copy link
Copy Markdown

@yosofbadr yosofbadr commented Apr 15, 2026

Summary

  • Adds a new examples/ directory with self-contained, well-commented scripts demonstrating common redis-py usage patterns
  • string_operations.py -- SET/GET, MSET/MGET, atomic counters (INCR/DECR), key expiration with EX/NX/XX flags
  • hash_operations.py -- HSET/HGET/HGETALL, HMGET, HINCRBY, HEXISTS/HKEYS/HVALS/HDEL
  • pubsub.py -- SUBSCRIBE, PSUBSCRIBE (pattern-based), PUBLISH with a threaded demo that runs subscriber and publisher in one process

Each script includes a docstring with prerequisites (pip install, Docker command to start Redis), cleans up after itself, and can be run standalone.

Closes #1744

Test plan

  • Run each script against a local Redis instance and verify output
  • Confirm scripts clean up all keys they create
  • Verify the pub/sub script exits cleanly (no hanging threads)

Note

Low Risk
Only adds new example scripts and does not modify library/runtime code; risk is limited to developer documentation/usage expectations.

Overview
Adds a new examples/ directory with three standalone redis-py scripts demonstrating core Redis usage: basic string commands and TTLs (string_operations.py), hash CRUD and atomic increments (hash_operations.py), and a threaded pub/sub end-to-end demo with clean shutdown (pubsub.py).

Each script includes prerequisite/run instructions, uses decode_responses=True for readable output, and cleans up any keys/subscriptions it creates.

Reviewed by Cursor Bugbot for commit baa52e5. Bugbot is set up for automated code reviews on this repo. Configure here.

Add an examples/ directory with three self-contained scripts covering
the most common redis-py usage patterns:

- string_operations.py: SET/GET, MSET/MGET, atomic counters, key expiry
- hash_operations.py: HSET/HGET/HGETALL, HMGET, HINCRBY, field inspection
- pubsub.py: SUBSCRIBE, PSUBSCRIBE, PUBLISH with a threaded demo

Closes redis#1744
@jit-ci
Copy link
Copy Markdown

jit-ci bot commented Apr 15, 2026

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

@yosofbadr yosofbadr marked this pull request as ready for review April 15, 2026 20:21
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 08846a0. Configure here.

Comment thread examples/pubsub.py Outdated
@petyaslavova
Copy link
Copy Markdown
Collaborator

Hi @yosofbadr, thank you for your contribution!

In redis-py, examples are stored under docs/examples as Jupyter notebooks and are then referenced in the Read the Docs documentation. Could you please update the examples you added to follow this existing pattern?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

We need examples!

2 participants