Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CDN (Cloudfront) Cache Invalidation using Carrierwave

I'm using carrierwave to allow users to upload images with an ability to crop after upload, the issue is the versions get created and CDN catch them instantly that when users crop they don't see changes because the Cloudfront cache has to be invalidated and takes quite a decent time to refresh on its own.

I thought putting version numbers on file names might be a good way to sorta invalidate cache, how to achieve that using Carrierwave?

And is that the best approach?

like image 270
CodeOverload Avatar asked Nov 03 '12 00:11

CodeOverload


People also ask

How long does CloudFront cache invalidation take?

Object invalidations typically take from 10 to 100 seconds to complete. You can check the status of an invalidation by viewing your distribution from the CloudFront console.

What is cache invalidation in CloudFront?

The Invalidation allows us to remove object(s) from the Cloudfront cache before it expires. It allows you to remove a specific object from cache as well use supported wildcard character to remove multiple objects. You can also remove all the objects from cache by using “/*” parameters to invalidation requests.


2 Answers

I had the same issue with cloudflare

here my hackish solution: put the updated_at timestamp in a params. Should work with all CDN

class AssetUploader < CarrierWave::Uploader::Base

  def url(options={})
    super.split("?v=")[0]+"?v=#{model.updated_at.to_time.to_i}" rescue super
  end

end
like image 92
m4tm4t Avatar answered Sep 21 '22 14:09

m4tm4t


To make the url trick work on Amazon CloudFront CDN, just remember to enable query string in your distribution. Go to the behavior tab and enable 'forward query strings'.

like image 28
Alexandre Delarue Avatar answered Sep 20 '22 14:09

Alexandre Delarue