【发布时间】:2021-10-06 18:45:35
【问题描述】:
在下面的代码 sn-p 中,我试图从 redis 中获取缓存的对象,同时面临以下错误。
用例是在第一次 API 调用时缓存前 30 个评论对象,因此当第二次调用 API 时,缓存的对象将被传递给序列化程序,避免查询集。
缓存 cmets 对象。
featured=StockUserComments.objects.filter(some condition)
qs=StockUserComments.objects.filter(some condition)
latest=StockUserComments.objects.filter(some condition)
all =list(chain(featured,qs,latest))
redis_client.set("feeds:top_comments", str(all[:30]),ex=1800)
检索对象
top_comments = redis_client.get("feeds:top_comments")
if top_comments:
print("from cache")
all = ast.literal_eval(top_comments.decode("utf-8"))
错误
Traceback (most recent call last):
File "E:\stocktalk-api-platform\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "E:\stocktalk-api-platform\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "E:\stocktalk-api-platform\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "E:\stocktalk-api-platform\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "E:\stocktalk-api-platform\venv\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "E:\stocktalk-api-platform\venv\lib\site-packages\rest_framework\views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "E:\stocktalk-api-platform\venv\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "E:\stocktalk-api-platform\venv\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
raise exc
File "E:\stocktalk-api-platform\venv\lib\site-packages\rest_framework\views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "E:\stocktalk-api-platform\apps\stock_dashboard\views.py", line 748, in get
all = ast.literal_eval(top_comments.decode("utf-8"))
File "C:\Users\Fleetstudio\AppData\Local\Programs\Python\Python38\lib\ast.py", line 59, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
File "C:\Users\Fleetstudio\AppData\Local\Programs\Python\Python38\lib\ast.py", line 47, in parse
return compile(source, filename, mode, flags,
File "<unknown>", line 1
[<StockUserComments: Cm>, <StockUserComments: R3>, <StockUserComments: Re>, <StockUserComments: N5>, <StockUserComments: New1>, <StockUserComments: R1>, <StockUserComments: Cm>, <StockU
serComments: New2>, <StockUserComments: ~EMPTY_COMMENT>, <StockUserComments: ~EMPTY_COMMENT>, <StockUserComments: I think it’s time to sell here>, <StockUserComments: ~EMPTY_COMMENT>, <Stoc
kUserComments: ~EMPTY_COMMENT>, <StockUserComments: ~EMPTY_COMMENT>, <StockUserComments: Time to sell indeed>, <StockUserComments: Time to sell.>, <StockUserComments: What a great company
huge opportunities ahead. I would definitely be a buyer here. Although this is a risky one lots of volatility be careful
^
SyntaxError: invalid syntax
【问题讨论】:
-
您不必使用
ast.literal_eval。你在redis_client中序列化你的对象了吗? -
不只是对象被发送到redis
-
试试看没有
ast.literal_eval的输出是什么类型 -
只有数字没有 ast.literal_val
标签: python django django-models redis