【问题标题】:Unknown error while retrieving django queryset objects from redis从 redis 检索 django 查询集对象时出现未知错误
【发布时间】: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


【解决方案1】:

序列化对象的一种方法(除了使用strast.literal_eval,这实际上不起作用)是使用pickle

import pickle
redis_client.set("feeds:top_comments", pickle.dumps(all[:30]), ex=1800)

这会将您的查询集序列化为可以作为查询集检索回来的字节。然后您可以通过以下方式检索:

import pickle
top_comments = redis_client.get("feeds:top_comments")
all = pickle.loads(top_comments)

请注意,您从pickle.loads 获得的查询集与数据库中的查询集相比可能已经过时。在这种情况下,您可以使用对象 ID 进行序列化,然后再次获取它们。请注意,您还可以根据需要使用其他类型的序列化,例如 json。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 2020-02-14
    • 2020-07-30
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 2017-07-09
    • 2017-08-23
    相关资源
    最近更新 更多