首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Django:缓存函数

Django:缓存函数
EN

Stack Overflow用户
提问于 2020-10-08 16:31:39
回答 2查看 168关注 0票数 1

我创建了以下序列化程序,其中我两次调用函数_calculate_image_dimensions。现在,我尝试了@仙人掌属性,但由于必须传递值width, height,所以无法工作。但是,对于get_width和get_height,它们不会改变。是否有办法确保计算只计算一次?

代码语言:javascript
运行
复制
class IntegrationImageSerializer(serializers.ModelSerializer):
    width = serializers.SerializerMethodField()
    height = serializers.SerializerMethodField()

    class Meta:
        model = IntegrationImage
        fields = ("image", "width", "height")

    def _calculate_image_dimensions(self, width, height) -> int:
        MAX_IMAGE_SIZE = 350
        aspect_ratio = width / height
        if width > height:
            width = MAX_IMAGE_SIZE
            height = width / aspect_ratio
        else:
            height = MAX_IMAGE_SIZE
            width = height * aspect_ratio
        return round(width), round(height)

    def get_width(self, obj: IntegrationImage) -> int:
        width, height = self._calculate_image_dimensions(
            obj.image.width, obj.image.height
        )
        return width

    def get_height(self, obj: IntegrationImage) -> int:
        width, height = self._calculate_image_dimensions(
            obj.image.width, obj.image.height
        )
        return height
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-08 16:54:38

最初,我觉得有点困惑,有一个方法get_width执行一个函数,得到宽度高度,然后只返回width

使用dimensions字段稍微改变API结构是否有意义(如果widthheight被引用到很多地方,这实际上可能并不有效)?

如果不需要更改对widthheight的大量引用,这将只调用_calculate_image_dimensions一次(我认为这就是您要解决的问题?)你就不需要缓存了。

代码语言:javascript
运行
复制
class IntegrationImageSerializer(serializers.ModelSerializer):
    dimensions = serializers.SerializerMethodField()

    class Meta:
        model = IntegrationImage
        fields = ("image", "dimensions")

    def _calculate_image_dimensions(self, width, height) -> int:
        MAX_IMAGE_SIZE = 350
        aspect_ratio = width / height
        if width > height:
            width = MAX_IMAGE_SIZE
            height = width / aspect_ratio
        else:
            height = MAX_IMAGE_SIZE
            width = height * aspect_ratio
        return round(width), round(height)

    def get_dimensions(self, obj: IntegrationImage) -> int:
        width, height = self._calculate_image_dimensions(
            obj.image.width, obj.image.height
        )
        return {"width": width, "height": height}
票数 0
EN

Stack Overflow用户

发布于 2020-10-08 16:35:44

使它成为一个免费的函数(类外),并用lru_cache()装饰它

代码语言:javascript
运行
复制
from functools import lru_cache

@lru_cache(maxsize=None)
def calculate_image_dimensions(width, height) -> int:
    MAX_IMAGE_SIZE = 350
    aspect_ratio = width / height
    if width > height:
        width = MAX_IMAGE_SIZE
        height = width / aspect_ratio
    else:
        height = MAX_IMAGE_SIZE
        width = height * aspect_ratio
    return round(width), round(height)

这将缓存工作进程生存期的结果。

(不过,我怀疑有几个比较和分区的函数是应用程序的性能瓶颈。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64266839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档