django-imagekit: Accessing ImageSpecField url is extremely slow

Hi I use ImageSpecFields to create renditions for ModelAdmin image fields. When I access the url property of the ImageSpecField I notice an enormous delay. On a production machine accessing 11 url properties will take a total of 1.7 seconds, slowing down the response time by a factor of 20. I have really narrowed it down, so I can confidently say that whatever happens behind the scenes when I access the ImageSpecField’s url property is eating up that time.

Do you have any idea how I can speed up things. I already use db caching which gave me an improvement of about .5 seconds from 2.2 seconds down to 1.7 seconds.

Any help would be greatly appreciated.

I use the following settings:

DEBUG = False
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'my_cache_table',
        'TIMEOUT': 157680000, # 5 years
    }
}
IMAGEKIT_DEFAULT_CACHEFILE_STRATEGY = 'imagekit.cachefiles.strategies.Optimistic'
IMAGEKIT_DEFAULT_CACHEFILE_BACKEND = 'imagekit.cachefiles.backends.Simple'
IMAGEKIT_CACHE_BACKEND = 'default'

About this issue

  • Original URL
  • State: open
  • Created 11 years ago
  • Reactions: 1
  • Comments: 22 (21 by maintainers)

Most upvoted comments

It must be the hasher.

In order to identify images, IK uses filenames which, in the case of specs, are generated by creating a hash of the source filename, the processors, and a few other properties using Pickle. There are a few ways you can override this behavior:

  • Override get_hash on your spec to calculate the hash in a different way for a single spec.
  • Override cachefile_name on your spec to generate the filename in a different way.
  • Set a new IMAGEKIT_SPEC_CACHEFILE_NAMER to change how spec filenames are generated globally.

For the sake of troubleshooting, we should probably try the first option to get as close to the suspected cause (the hasher) as possible. It should probably still use the source’s filename though so we can eliminate that as a contributing factor. (It shouldn’t be if the sources are regular Django files.)

Do you think you can try that and report back? I’m sorry the project is giving you a rough time but I appreciate your help in figuring this out!