Prefetching image renditions

179 views
Skip to first unread message

mrques...@gmail.com

unread,
Nov 24, 2015, 2:44:52 PM11/24/15
to Wagtail support
Is it possible to prefetch image renditions? I have a index page with a lot of images, which issues a query for every image.

Salvador Faria

unread,
Nov 24, 2015, 4:48:19 PM11/24/15
to Wagtail support
I have been trying to minimize queries related to image thumbnails and you can do something like this:
- prefetch all image renditions
- use lru cache to 'cache' the most used images renditions

I have not completed my optimizations and code cleanup, but it is like I was playing around.

@lru_cache(maxsize=200)
def get_image_renditions(image):
    return image.renditions.all().select_related('filter')


def get_rendition(image, filter_spec):
    renditions = get_image_renditions(image)
    for r in renditions:
        if r.filter.spec == filter_spec:
            return r
    return image.get_rendition(filter_spec)

@register.assignment_tag
def image_rendition(image, filter_spec):
    return get_rendition(image, filter_spec)


# using a templatetag as "wrapper"
{% image_rendition post.author.profile_picture fill-270x270 as avatar_image %}



mrques...@gmail.com

unread,
Nov 25, 2015, 5:05:53 PM11/25/15
to Wagtail support
Thanks for the tip!
It'd be cool to have something like this built-in, as this is such a common use case.
Reply all
Reply to author
Forward
0 new messages