我在使用 sorl-thumbnail 时遇到了一些困难。我使用 S3Boto3Storage 存储后端在 S3 上托管图像,我使用 Redis 作为我的键值存储,并且所有查询都已经缓存在其中。我正在使用以下依赖项:
boto3==1.17.103
botocore==1.20.105
Django==3.2.4
django-redis==4.10.0
django-storages==1.11.1
redis==3.3.11
sorl-thumbnail==12.7.0
现在我运行以下脚本并计时:
from sorl.thumbnail import get_thumbnail
images = Image.objects.all() # 7 images
for image in images:
x = image.file
y = x.url # 1
x = get_thumbnail(x, '800x600', crop='center') # 2
x = x.url # 3
奇怪的是,当我调试第3步时,x似乎已经有了“url”属性,所以我不知道为什么要花这么长时间才能访问它。
到 Redis 的连接正在工作,并且在第二步中已经激活了对它的查询。就像我之前说的,缩略图已经生成,查找缓存在 Redis 中,所以(我相信)与 S3 缩略图生成问题无关。
在生产中,我经常一次查询大约 50 张图像......这相当于超过 4s 的 url 序列化......希望得到任何帮助! :)
答案 0 :(得分:0)
好的,找出问题所在。我在设置中设置了 AWS_S3_CUSTOM_DOMAIN
,但在我的存储类中不小心覆盖了它:
from storages.backends.s3boto3 import S3Boto3Storage
class DefaultFileStorage(S3Boto3Storage):
location = settings.AWS_DEFAULT_FILES
file_overwrite = False
custom_domain = False # <-- Bottleneck!
删除 custom_domain = False
后,性能提高了 10 倍(字面意思)。