Search code examples
ruby-on-railscachingherokumemcacheddragonfly-gem

How to cache files over 1MB with rack/cache on Heroku?


I'm using a combination of Dragonfly and rack/cache hosted on Heroku.

I'm using Dragonfly for uploaded assets. Thumbnails are processed on-the-fly and stored in rack/cache for fast delivery from memcached (via the Memcachier addon).

Regular static assets are also cached in memcached via rack/cache.

My problem is that any uploaded files over 1MB are causing a 500 error in my application.

2013-07-15T10:38:27.040992+00:00 app[web.1]: DalliError: Value too large, memcached can only store 1048576 bytes per key [key: d49c36d5db74ef45e957cf169a0b27b83b9e84de, size: 1502314]
2013-07-15T10:38:27.052255+00:00 app[web.1]: cache: [GET /media/BAhbBlsHOgZmSSIdNTA3Njk3ZWFiODBmNDEwMDEzMDAzNjA4BjoGRVQ/WTW_A5Flyer_HealthcareMedicalObsGynae_WEB.pdf] miss, store
2013-07-15T10:38:27.060583+00:00 app[web.1]: !! Unexpected error while processing request: undefined method `each' for nil:NilClass

Memcache has a limit of 1MB, so I can understand why my asset was not cached, but I would rather it didn't break serving assets.

I'm not even sure where this error is coming from. Presumably from one of the other rack middlewares?

Increasing the maximum file size doesn't seem to have have any affect.

config.cache_store = :dalli_store, ENV["MEMCACHIER_SERVERS"].split(","), {¬
  :username        => ENV["MEMCACHIER_USERNAME"],¬
  :password        => ENV["MEMCACHIER_PASSWORD"],¬
  :value_max_bytes => 5242880 # 5MB¬
}

Long term, I know that moving this sort of asset off of Heroku is a sensible move, but that won't be a quick job.

What can I do to serve these assets on Heroku in the meantime without errors?


Solution

  • I had the same issue as @jordelver and managed to get round memcachier's limits by monkey patching Dragonfly::Response:

    module Dragonfly
      class Response
        private
        def cache_headers
          if job.size > 1048576
            {
              "Cache-Control" => "no-cache, no-store",
              "Pragma" => "no-cache"
            }
          else
            {
               "Cache-Control" => "public, max-age=31536000", # (1 year)
               "ETag" => %("#{job.signature}")
            }
          end
        end
      end
    end
    

    Essentially, if the size is over 1048576 bytes, send a no-cache header.