ruby-on-railscachingdragonfly-gem

Dragonfly on the fly thumbs, server side cache?


I'm using dragonfly to generate thumbs on the fly, but i've noticed that they are generated on each single page reload, which is not good. Reading on dragonfly documentation I see it's recommended to add the rack-cache gem, but the documentation for that gem says:

produce freshness (Expires, Cache-Control) and/or validation (Last-Modified, ETag)

So, this rely the cache in the client, which is non-sense for me. If i've thousands of visits I dont want to generate the thumb for each one, and just relying on the fact that each one will have a cache copy.

I want to implement some kind of cache on server side, and avoid DragonFly to generate it again if a file with that dimensions already exists.


Solution

  • You're correct in that just sending ETags and cache control headers to the client would only be marginally useful.

    What the author is talking about is using a reverse proxy like Squid, Varnish or Rack::Cache which act as a middleman between the client and your rails app - so when the client requests /my/stored/image/300x300.jpg the reverse proxy will simply serve the asset statically without the request ever hitting your Rails app (unless the cache is stale).

    You can set Dragonfly to use a file store - but it will be less performant than a reverse proxy:

    datastore :file,
      # directory under which to store files
      root_path: 'public/dragonfly',    
      # - defaults to 'dragonfly' relative to current dir
      # root for urls when serving directly from datastore using remote_url
      server_root: 'public'