如何在Heroku上使用rack / cache缓存超过1MB的文件? [英] How to cache files over 1MB with rack/cache on Heroku?

查看:260
本文介绍了如何在Heroku上使用rack / cache缓存超过1MB的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了蜻蜓 rack / cache 在Heroku上托管。

我使用Dragonfly上传资源。缩略图可以即时处理并存储在机架/缓存中,以便从memcached快速传输(通过 Memcachier插件
$ b 常规静态资产也通过rack / cache缓存在memcached中。



我的问题是任何超过1MB的上传文件都会在我的应用程序中导致500错误。

  2013-07-15T10:38:27.040992 + 00: 00 app [web.1]:DalliError:值太大,memcached每个键只能存储1048576字节[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]:!!处理请求时出现意外错误:未定义的方法`each'for nil:NilClass

Memcache的限制为1MB ,所以我可以理解为什么我的资产没有被缓存,但我宁愿它没有破坏服务资产。



我甚至不知道这个错误到了哪里从。推测来自其他机架中间件之一?



增加最大文件大小似乎没有任何影响。

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

长期来看,我知道将这种资产关闭的Heroku是一个明智的举动,但这不会是一个快速的工作。



我可以做些什么来在Heroku上同时提供这些资产而没有错误? p>

解决方案

我和@jordelver有同样的问题,并且设法通过修补monkey $ 来获得memcachier的限制。Dragonfly ::响应:

 模块蜻蜓
类响应
私有
def cache_headers
如果job.size> 1048576
{
Cache-Control=> no-cache,no-store,
Pragma=> no-cache
}
else
{
Cache-Control=> public,max-age = 31536000,#(1年)
ETag=> %(#{job.signature})
}
结束
结束
结束
结束

本质上,如果大小超过1048576字节,请发送no-cache头。


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?

解决方案

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.

这篇关于如何在Heroku上使用rack / cache缓存超过1MB的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆