matthewwithanm / django-imagekit

Automated image processing for Django. Currently v4.0
http://django-imagekit.rtfd.org/
BSD 3-Clause "New" or "Revised" License
2.26k stars 276 forks source link

Cached Files Not Regenerated #460

Open mia1024 opened 6 years ago

mia1024 commented 6 years ago

Hi, I'm currently using ImageSpecField. However, the cached files disappeared and I got 404 errors instead. I don't know why caches disappear, but when they do, they are not regenerated. I did not hardcode anything link in my template.

Here is my model definition

class Photo(models.Model):
    def get_upload_to(instance, filename):
        return instance.title + '.' + filename.split('.')[-1]

    img = models.ImageField("The Image", upload_to=get_upload_to)
    thumbnail = ImageSpecField(
            source="img",
            format="JPEG",
            processors=[ResizeToFit(height=100)],
            options={'quality': 100}
    )
    small = ImageSpecField(
            source="img",
            format="JPEG",
            processors=[ResizeToFit(height=200)],
            options={'quality': 100}
    )
    medium = ImageSpecField(
            source="img",
            format="JPEG",
            processors=[ResizeToFit(height=540)],
            options={'quality': 100}
    )
    large = ImageSpecField(
            source="img",
            format="JPEG",
            processors=[ResizeToFit(height=1080)],
            options={'quality': 100}
    )

    title = models.CharField("Title", max_length=200, unique=True, blank=True)
    # The title will be the link to access the photo
    description = RichTextField("Description", blank=True, null=True)
    is_titled = models.BooleanField("Is the photo titled (auto calculated):", default=True)
    f_number = models.FloatField("f number", blank=True, null=True)
    shutter_speed = models.CharField('Shutter speed', max_length=60, blank=True, null=True)
    ISO = models.IntegerField("ISO", blank=True, null=True)

    date_taken = models.DateField("Date Taken", blank=True, null=True)
    location = models.CharField("Location", max_length=50, blank=True, null=True)

    is_listed = models.BooleanField("List Link?", default=True)
    is_featured = models.BooleanField("Feature on home page?", default=False)
    last_modified = models.DateTimeField("Page Last Modified", auto_now=True)

    def get_absolute_url(self):
        return '/photo/' + self.title.replace(' ', '_')

And the relevant part in my template:

                    <a href="{{ photo.get_absolute_url() }}">
                        <img class="thumbnail" src="{{ photo.thumbnail.url }}">
                    </a>
mia1024 commented 6 years ago

I can reproduce this problem on my development machine by deleting the folder CACHE after it is created. I think a potential solution is to run a cron that regenerate the entire cache. However, I don't know how to manually generate all caches

vstoykov commented 6 years ago

After deleting the CACHE folder you need to clear the cache that stores the information that files are generated already.

Depending on the cache configured in your settings you have different options:

  1. If you do not have CACHES in your settings this means that you are using locmem by default and server restart is enough

  2. If You are using some other external cache (memcached, redis etc.) then you need to clear it by opening the shell and executing the following:

      from imagekit.utils import get_cache
      cache = get_cache()
      cache.clear()
  3. If you are using external cache and also you are using django-extensions then you can execute:

      python manage.py clear_cache
ArtemBernatskyy commented 6 years ago

+1 this problem has happened to me

But it's interesting why caches disappeared !)

vstoykov commented 6 years ago

@ArtemBernatskyy and I already give a solution how to fix it.

Is there anyone willing to give better solution?

ArtemBernatskyy commented 6 years ago

@vstoykov but as far as i understand it will clear WHOLE cache, isn't it ? But can we clear ONLY django-imagekit related cache ?

vstoykov commented 6 years ago

Depends on how you configure your cache. If you configured imagekit to use the same cache as everything else (actually this is the default) then you will clear everything. If you want them to be separated than you need to configure different caches in settings.CACHES and point IMAGEKIT_CACHE_BACKEND to the other cache. Then you need to clear only that cache.

vstoykov commented 6 years ago

@pkqxdd @ArtemBernatskyy did you think that there is still issue with ImageKit or we can close the issue?

ArtemBernatskyy commented 6 years ago

I can’t reproduce the issue, closing till someone will found a way to reproduce it.

ericel commented 4 years ago

What happens in production, is it the same behavior? And yes if you are using default cache locmem, restarting the server is enough to generate the cache thumbnails.