test_redis2.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. # AttributeError: 'RedisStore' object has no attribute 'aset'. Did you mean: 'amset'?
  31. # 异步存储数据(使用aset而非set)
  32. await store.aset("test_key", b"test_value")
  33. # 异步获取数据(使用aget而非get)
  34. value = await store.aget("test_key")
  35. print(f"获取的值: {value}") # 输出: b'test_value'
  36. # 异步删除数据(使用adelete而非delete)
  37. await store.adelete("test_key")
  38. # 关闭连接(可选)
  39. redis_client.close()
  40. asyncio.run(main())