test_redis_simple.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python3
  2. """
  3. 超简单的Redis测试脚本
  4. """
  5. import redis
  6. import json
  7. def test_redis_connection():
  8. """测试Redis连接"""
  9. print("🔗 测试Redis连接...")
  10. try:
  11. # 创建Redis连接
  12. r = redis.Redis(host='localhost', port=6379, decode_responses=True)
  13. # 测试连接
  14. r.ping()
  15. print("✅ Redis连接成功")
  16. # 扫描所有checkpoint keys
  17. pattern = "checkpoint:*"
  18. print(f"🔍 扫描所有checkpoint keys...")
  19. keys = []
  20. cursor = 0
  21. count = 0
  22. while True:
  23. cursor, batch = r.scan(cursor=cursor, match=pattern, count=100)
  24. keys.extend(batch)
  25. count += len(batch)
  26. print(f" 已扫描 {count} 个keys...")
  27. if cursor == 0:
  28. break
  29. if count > 1000: # 限制扫描数量
  30. break
  31. print(f"📋 总共找到 {len(keys)} 个checkpoint keys")
  32. # 显示前几个key的格式
  33. print("🔍 Key格式示例:")
  34. for i, key in enumerate(keys[:5]):
  35. print(f" [{i+1}] {key}")
  36. # 查找doudou用户的keys
  37. doudou_keys = [k for k in keys if k.startswith("checkpoint:doudou:")]
  38. print(f"👤 doudou用户的keys: {len(doudou_keys)} 个")
  39. if doudou_keys:
  40. print("📝 doudou的key示例:")
  41. for i, key in enumerate(doudou_keys[:3]):
  42. print(f" [{i+1}] {key}")
  43. # 尝试获取数据
  44. data = r.get(key)
  45. if data:
  46. try:
  47. parsed = json.loads(data)
  48. print(f" 数据大小: {len(data)} 字符")
  49. print(f" 数据类型: {type(parsed)}")
  50. if isinstance(parsed, dict):
  51. print(f" 顶级keys: {list(parsed.keys())}")
  52. except Exception as e:
  53. print(f" 解析失败: {e}")
  54. r.close()
  55. return True
  56. except Exception as e:
  57. print(f"❌ Redis测试失败: {e}")
  58. import traceback
  59. traceback.print_exc()
  60. return False
  61. if __name__ == "__main__":
  62. test_redis_connection()