Caching dragonfly in sinatra

29 views
Skip to first unread message

Michał Bajur

unread,
Jan 12, 2016, 9:10:50 AM1/12/16
to Dragonfly
Hello everyone!

it's not strictly dragonfly question but a question about dragonfly and sinatra working together. I have a following endpoit in my sinatra app, which basically downloads an image from the URL and applies a "play" png button on top of it. Here is the code:

require 'sinatra'
require 'dragonfly'
require 'rmagick'
require 'rack-cache'

app
= Dragonfly[:images].configure_with(:imagemagick)

app
.processor.add :play_button do |temp_object, *args|
  canvas
= Magick::Image.read(temp_object.path).first
  append_image
= Magick::Image.read('play.png').first
  canvas
.composite!(append_image, Magick::CenterGravity, Magick::OverCompositeOp)
  canvas
.to_blob
end

get "/thumb/:url" do |url|
  app
.fetch_url(url)
     
.process(:play_button)
     
.to_response(env)
end

and basically it works prety well. The thing is - it's not using server caching at all, image is being regenerated on each request - which obviously sucks. I'm looking for any solutions on how can i use some third-party (or built-in) mechanism to cache images produced by that endpoint. I know there is a whole section in dragonfly docs dedicated to cacheing but it's not helpfull when it comes down to using it with sinatra. 

Maybe that's something obvious - i've just never done such thing before.

Thanks in advance!

Mark Evans

unread,
Jan 12, 2016, 10:36:02 AM1/12/16
to dragonf...@googlegroups.com
Hi - you’ve required rack-cache but you don’t appear to be using it.
you need to have a “use Rack::Cache” somewhere like in the sinatra example https://github.com/rtomayko/rack-cache/blob/master/example/sinatra/app.rb



--
You received this message because you are subscribed to the Google Groups "Dragonfly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dragonfly-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michał Bajur

unread,
Jan 12, 2016, 12:08:22 PM1/12/16
to dragonf...@googlegroups.com
Yeah, that's actually some legacy code left for something else i was working on. Anyway - is that a good idea to use it? Will it work out of the box or it will need some extra config to cover given code/scenario?

Thanks in advance

--
You received this message because you are subscribed to a topic in the Google Groups "Dragonfly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dragonfly-users/HbFg7GT2mGI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dragonfly-use...@googlegroups.com.

Mark Evans

unread,
Jan 12, 2016, 2:18:24 PM1/12/16
to dragonf...@googlegroups.com
It should just work.
All Dragonfly does is set the appropriate caching headers. to_response makes sure the headers are set appropriately
Rack-Cache (or whatever other cache) use those headers to do their magic.

Michał Bajur

unread,
Jan 12, 2016, 2:23:23 PM1/12/16
to dragonf...@googlegroups.com
I probably should mentioned it - i don't want to cache it on user side (well, actally i can do this as well but that's not so important). What i want to do is to cache it on server side and serve generated images from cache instead of generating it on each request for each user.

Walter Lee Davis

unread,
Jan 12, 2016, 3:04:45 PM1/12/16
to dragonf...@googlegroups.com
Unless the url for one image is different per user, then you will get the same content when you request that url a second time. The cache is server-side

Walter
You received this message because you are subscribed to the Google Groups "Dragonfly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dragonfly-use...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages