Skip to content

Instantly share code, notes, and snippets.

@kind-abhasin
Created September 12, 2025 19:32
Show Gist options
  • Select an option

  • Save kind-abhasin/241423184e437070c316d8a8752387e3 to your computer and use it in GitHub Desktop.

Select an option

Save kind-abhasin/241423184e437070c316d8a8752387e3 to your computer and use it in GitHub Desktop.
Redis Scan
#!/usr/bin/env python3
"""
Script to connect to production Redis cluster using GlideClusterClient.
"""
import asyncio
from glide import GlideClusterClient, GlideClusterClientConfiguration, NodeAddress
async def main():
print("Connecting to NEWSFEED_RANKING_REDIS cluster...")
addresses = [
NodeAddress(
"us1-nextdoor-com-newsfeed-cache.auq6u7.clustercfg.usw2.cache.amazonaws.com", 6379
)
]
config = GlideClusterClientConfiguration(
addresses=addresses,
use_tls=False,
request_timeout=5000 # 5 second timeout
)
client = None
try:
print("Creating Glide cluster client...")
client = await GlideClusterClient.create(config)
# Basic connectivity check
pong = await client.ping()
print(f"Connected successfully. Ping response: {pong}")
# Sample keys and their types
N = 50
print(f"\nFirst {N} keys with types (sample):")
# Get some keys using SCAN
cursor = "0"
keys_found = 0
while keys_found < N:
# Use SCAN to get keys (non-blocking)
scan_result = await client.scan(cursor, count=100)
cursor = scan_result[0] # Next cursor
keys = scan_result[1] # Keys batch
for key in keys:
if keys_found >= N:
break
try:
key_type = await client.type(key)
print(f"{key_type:8} {key}")
except Exception as e:
print(f"error {key} (error: {e})")
keys_found += 1
# If cursor is "0", we've scanned all keys
if cursor == "0":
break
print(f"\nScanned {keys_found} keys")
except Exception as e:
print(f"Connection failed: {e}")
return
finally:
if client:
await client.close()
if __name__ == "__main__":
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment