我正在尝试使用aiocache库在我的端点上实现redis。我用aiocache做的第一个测试使用了@cache,没有指明任何其他服务,一切正常。但是当我尝试使用redis时,我看到了这个错误(端点仍然返回请求)
ERROR: Couldn't set [<databases.backends.postgres.Record object at 0x7fb9f01d86a0>, <databases.backends.postgres.Record object at 0x7fb9f01d88b0>, <databases.backends.postgres.Record object at 0x7fb9f01d8a60>] in key app.api.authorget_authors()[], unexpected error
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/aiocache/decorators.py", line 144, in set_in_cache
await self.cache.set(key, value, ttl=self.ttl)
File "/usr/local/lib/python3.8/site-packages/aiocache/base.py", line 61, in _enabled
return await func(*args,**kwargs)
File "/usr/local/lib/python3.8/site-packages/aiocache/base.py", line 45, in _timeout
return await asyncio.wait_for(func(self, *args,**kwargs), timeout)
File "/usr/local/lib/python3.8/asyncio/tasks.py", line 483, in wait_for
return fut.result()
File "/usr/local/lib/python3.8/site-packages/aiocache/base.py", line 75, in _plugins
ret = await func(self, *args,**kwargs)
File "/usr/local/lib/python3.8/site-packages/aiocache/base.py", line 265, in set
ns_key, dumps(value), ttl=self._get_ttl(ttl), _cas_token=_cas_token, _conn=_conn
File "/usr/local/lib/python3.8/site-packages/aiocache/serializers/serializers.py", line 140, in dumps
return json.dumps(value)
TypeError: <databases.backends.postgres.Record object at 0x7fb9f01d8a60> is not JSON serializable
方法是:
@authors.get("/", response_model=List[AuthorOut])
@cached(
ttl=100,
cache=Cache.REDIS,
endpoint="X.X.X.X", #my local ip
serializer=JsonSerializer(),
port=6379,
namespace="main",
#key="key",
)
async def get_authors():
return await db_manager.get_all_authors()
整个环境基于docker、2个容器、1个fastapi、1个postgresql和1个redis。
很明显,端点返回的对象有问题,所以我问您如何将如此复杂的对象传递给redis?
遵循aiochace文档,我已经尝试了所有的序列化程序,但是没有成功。
我的 Docker
version: '3.7'
services:
book_service:
build: ./book-service
command: uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
volumes:
- ./book-service/:/app/
ports:
- 8001:8000
environment:
- DATABASE_URI=postgresql://book_db_username:book_db_password@book_db/book_db_dev
- AUTHOR_SERVICE_HOST_URL=http://author_service:8000/api/v1/authors/
depends_on:
- book_db
book_db:
image: postgres:12.1-alpine
volumes:
- postgres_data_book:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=book_db_username
- POSTGRES_PASSWORD=book_db_password
- POSTGRES_DB=book_db_dev
author_service:
build: ./author-service
command: uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
volumes:
- ./author-service/:/app/
ports:
- 8002:8000
environment:
- DATABASE_URI=postgresql://author_db_username:author_db_password@author_db/author_db_dev
depends_on:
- author_db
author_db:
image: postgres:12.1-alpine
volumes:
- postgres_data_author:/var/lib/postgres/data
environment:
- POSTGRES_USER=author_db_username
- POSTGRES_PASSWORD=author_db_password
- POSTGRES_DB=author_db_dev
nginx:
image: nginx:latest
ports:
- "8080:8080"
volumes:
- ./nginx_config.conf:/etc/nginx/conf.d/default.conf
depends_on:
- author_service
- book_service
redis:
image: redis:3.2-alpine
volumes:
# - redis_data:/data
- ./redis.conf:/usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
ports:
- 6379:6379
networks:
node_net:
ipv4_address: 172.28.1.4
networks:
node_net:
ipam:
driver: default
config:
- subnet: 172.28.0.0/16
volumes:
postgres_data_book:
postgres_data_author:
redis_data:
1条答案
按热度按时间eblbsuwk1#
错误说明了一切
错误在于您正在json中序列化对象,以便缓存json中不可序列化的对象。
我曾经遇到过类似的问题,但发现fastapi的编码器支持
Record
反对,而其他人不反对。您可以返回通过这种编码器序列化的对象,或者在redis缓存参数中将其设置为编码器。看到了吗https://fastapi.tiangolo.com/tutorial/encoder/#using-JSU编码器。我没有测试下面的代码,但它应该足以让我的意思的想法。