Caching and Performance
Dragonfly serves content on-the-fly, that is, when a request is made to a url given by
then Dragonfly will do exactly what the job says, i.e.
- fetch the content stored with uid "my/stored/image"
- process it to make a thumbnail with dimensions 300x200
Doing this on every request is expensive, so it's wise to use a caching proxy, so that after the first request the server simply returns the cached response.
ETag headers in its responses to let us do this (for a well-written discussion of this, see this blog post).
For simple use of Rack::Cache with rails see Rails.
To customize Dragonfly's response headers, inside a
configure block, you can use something like
Setting the header to
nil removes the header from the response.
Using a CDN
Some CDNs are configured to make a request to your domain on the first request and return the cached version thereafter, using the same url path.
You can use
url_host to point urls to the CDN instead of your local server.
For example, a url
normally returns something like
Adding in a
makes the url
NOTE: some CDNs may not forward GET parameters, in which case you may need to make sure the
sha parameter is part of the path, i.e. in a
Serving remotely (directly from the datastore)
Performing processing on upload
Processing on-the-fly and serving remotely
Serving processed versions of content such as thumbnails remotely is a bit more tricky as we need to upload the thumbnail to the datastore in the on-the-fly manner.
Dragonfly provides a way of doing this using
The details of keeping track of/expiring these thumbnails is up to you.
We need to keep track of which thumbnails have been already created, by storing a uid for each one.
Below is an example using an ActiveRecord
Thumb table to keep track of already created thumbnail uids.
It has two string columns;
This would give
then from the second time onwards
The above is just an example - there are a number of things you could do with
you could use e.g. Redis or some key-value store to keep track of thumbnails.
You'd also probably want a way of expiring the thumbnails or destroying them when the original is destroyed, but this is left up to you as it's outside of the scope of Dragonfly.