为什么Dragonfly一遍又一遍地重新调整我的图像?

时间:2014-01-15 19:19:22

标签: ruby-on-rails ruby amazon-s3 dragonfly-gem

我加载了我的图片,第一次它会在它们上运行convert,所以它有点慢。 但是我预计它永远不会再次运行convert,但是DragonFly会一遍又一遍地进行(在开发模式下)。

2014-01-15 14:10:45.587 [fyi] Started GET "/media/W1siZiIsIjIwMTMvMTEvMTEvMTkvNTAvMDAvNTg3L2JsaW5kYmFyYmVyX2ZhY2lhbGNsZWFuc2VyLmpwZyJdLFsicCIsInRodW1iIiwiOTIweDkyMD4iXSxbInAiLCJlbmNvZGUiLCJqcGciLCItcXVhbGl0eSA0MCJdXQ/blindbarber-facialcleanser.jpg" for 127.0.0.1 at 2014-01-15 14:10:45 -0500 (pid:73447)
2014-01-15 14:10:47.149 [meh] DRAGONFLY: shell command: 'convert' '/var/folders/42/z4r6cjj10vb5c6zvtwb0m9000000gn/T/dragonfly20140115-73447-1ti4jze' '-resize' '920x920>' '/var/folders/42/z4r6cjj10vb5c6zvtwb0m9000000gn/T/dragonfly20140115-73447-fvdfam.jpg' (pid:73447)
2014-01-15 14:10:47.700 [meh] DRAGONFLY: shell command: 'convert' '/var/folders/42/z4r6cjj10vb5c6zvtwb0m9000000gn/T/dragonfly20140115-73447-fvdfam.jpg' '-quality' '40' '/var/folders/42/z4r6cjj10vb5c6zvtwb0m9000000gn/T/dragonfly20140115-73447-e10r7h.jpg' (pid:73447)
2014-01-15 14:10:47.804 [fyi] DRAGONFLY: GET /media/W1siZiIsIjIwMTMvMTEvMTEvMTkvNTAvMDAvNTg3L2JsaW5kYmFyYmVyX2ZhY2lhbGNsZWFuc2VyLmpwZyJdLFsicCIsInRodW1iIiwiOTIweDkyMD4iXSxbInAiLCJlbmNvZGUiLCJqcGciLCItcXVhbGl0eSA0MCJdXQ/blindbarber-facialcleanser.jpg 200 (pid:73447)
2014-01-15 14:10:47.807 [meh] Cache write: d857888f4b9a786a815372039d952e203c056795 (pid:73447)
2014-01-15 14:10:47.825 [meh] Cache read: d857888f4b9a786a815372039d952e203c056795 (pid:73447)
2014-01-15 14:10:47.826 [meh] Cache read: http://localhost:3000/media/W1siZiIsIjIwMTMvMTEvMTEvMTkvNTAvMDAvNTg3L2JsaW5kYmFyYmVyX2ZhY2lhbGNsZWFuc2VyLmpwZyJdLFsicCIsInRodW1iIiwiOTIweDkyMD4iXSxbInAiLCJlbmNvZGUiLCJqcGciLCItcXVhbGl0eSA0MCJdXQ/blindbarber-facialcleanser.jpg? (pid:73447)
2014-01-15 14:10:47.827 [meh] Cache write: http://localhost:3000/media/W1siZiIsIjIwMTMvMTEvMTEvMTkvNTAvMDAvNTg3L2JsaW5kYmFyYmVyX2ZhY2lhbGNsZWFuc2VyLmpwZyJdLFsicCIsInRodW1iIiwiOTIweDkyMD4iXSxbInAiLCJlbmNvZGUiLCJqcGciLCItcXVhbGl0eSA0MCJdXQ/blindbarber-facialcleanser.jpg? (pid:73447)

但是对于每个请求它会再次转换图像,任何想法为什么?

PS:我的dragonfly.rb

1 个答案:

答案 0 :(得分:0)

实际的解决方案是启用机架缓存(在config/environments/development.rb中):

  config.action_dispatch.rack_cache = {
    metastore: 'file:tmp/cache/rack/meta',
    entitystore: 'file:tmp/cache/rack/body'
  }

要注意每个image.widthimage.height等......实际上会调用convert命令。因此,如果您想经常获取此信息,最好将其存储在您的模型中:

  create_table "images", force: true do |t|
    # ...
    t.string   "image_uid"
    t.string   "image_name"
    t.integer  "image_height"
    t.integer  "image_width"
  end

在你的模特中:

  before_save :update_image_fields
  def update_image_fields
    if image
      self.image_width = image.width
      self.image_height = image.height
    else
      self.image_width = self.image_height = 0
    end

    true
  end