test_redis.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # !/usr/bin/ python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. @Project : lq-agent-api
  5. @File :test.py.py
  6. @IDE :PyCharm
  7. @Author :
  8. @Date :2025/7/11 12:23
  9. '''
  10. import os
  11. import sys
  12. sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
  13. import redis
  14. #from langchain.storage import RedisStore
  15. from langchain_community.storage import RedisStore
  16. import asyncio
  17. from redis import Redis as SyncRedisClient
  18. async def main():
  19. # 创建同步Redis客户端(注意不是aioredis)
  20. redis_client = redis.Redis.from_url(
  21. "redis://localhost:6379",
  22. decode_responses=False # LangChain需要bytes
  23. )
  24. # redis_client = SyncRedisClient.from_url(
  25. # "redis://localhost:6379",
  26. # decode_responses=False # LangChain需要bytes
  27. # )
  28. # 创建存储
  29. store = RedisStore(client=redis_client)
  30. # 存储数据(注意是set不是aset)
  31. # 存储数据:使用 mset(即使只有一个键)
  32. store.mset([("test_key", b"test_value")])
  33. # 获取数据:使用 mget
  34. value = store.mget(["test_key"])
  35. print(f"Retrieved: {value}") # [b'test_value']
  36. # 如果你想提取第一个值
  37. if value and value[0] is not None:
  38. print(f"Value: {value[0].decode('utf-8')}") # 输出: Value: test_value
  39. # 关闭连接(可选)
  40. redis_client.close()
  41. asyncio.run(main())