热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在Heroku多重dynos上使用载波缓存?-HowtousecarrierwavecacheonHerokumultipledynos?

IhaveanappwithCarrierwaveonHeroku.Onapage,Ihave2forms:1ajaxformforuploadingapic

I have an app with Carrierwave on Heroku. On a page, I have 2 forms: 1 ajax form for uploading a picture and 1 normal form for additional information needed to create the object. Suppose my Carrierwave mount is :picture, every time the ajax form is submitted, the picture is saved temporarily into the public folder and its path is returned as :picture_cache. The second form then uses that to know which picture to be created with the new object on the second request. This works fine for a single dyno.

我有一个在Heroku上有Carrierwave的应用。在一个页面上,我有两个表单:一个用于上传图片的ajax表单,另一个用于创建对象所需的其他信息的普通表单。假设我的Carrierwave mount是:picture,每次提交ajax表单时,图片都会被临时保存到公共文件夹中,其路径返回为:picture_cache。然后,第二个表单使用它来知道在第二个请求中使用新对象创建哪个图片。这适用于单个dyno。

Different dynos don't know about each other's filesystems. Thus if the request to submit the 2nd form doesn't hit the same dyno as the request of the first form, it can't find the image.

不同的dynos不知道彼此的文件系统。因此,如果提交第2个表单的请求与第一个表单的请求不匹配,则无法找到图像。

Has anyone tackled this problem?

有人解决过这个问题吗?

1 个解决方案

#1


2  

i use a custom model and store all files, including tmp ones, in mongodb. the uploads are marked as tmp. ones the models is 'saved' i simply remove the 'tmp' flag. in this way all nodes see all images all the time. it's pretty crazy that the carrierwave default is to cache in ./tmp since many multi-node configuration would see this issue (unless the balancer implements session affinity).

我使用自定义模型并将所有文件(包括tmp文件)存储在mongodb中。上传被标记为tmp。如果模型被“保存”,我只需删除“tmp”标志。这样,所有的节点都可以看到所有的图像。由于许多多节点配置都会遇到这个问题(除非均衡器实现会话关联),所以carrierwave默认值是缓存./tmp。

here is my model and controller, etc: https://gist.github.com/3161569

这是我的模型和控制器,等等:https://gist.github.com/3161569

you have to do some custom work in the form:

你必须在表格中做一些定制工作:

  • save every file posted, no matter what
  • 保存所有上传的文件,无论如何
  • relay the posted file id in a hidden field
  • 在隐藏字段中传递已发布的文件id。
  • on save look for a file and/or previously uploaded id
  • 在保存时,查找文件和/或先前上传的id
  • make the model associations
  • 使模型关联

this approach, although it isn't 'magic' also gives the following awesome side effects:

这种方法虽然不是“魔法”,但也有以下可怕的副作用:

  • you have one process running jobs in the background to thumbnail the images vs. spinning up image_magick whenever a user hits 'submit' (which is a serious DOS vector, esp on memory limited hosts like heroku)

    你有一个进程在后台运行作业来缩略图像,而当用户点击“提交”(这是一个重要的DOS向量,特别是在内存有限的主机上,比如heroku)时,你可以将image_magick旋转起来

  • you can migrate images to s3 in the background, hourly, whatever, and the uploads simply have a new url (in this case the controller need to issue a permanant redirect if it notices this). this is really nice because you can keep 'em in the db for dev, staging, etc. and migrate some, or all, uploads onto s3 whenever without changing any upload or view code.

    您可以在后台将图像迁移到s3中,每小时一次,无论如何,上传的只是一个新的url(在这种情况下,如果控制器注意到这个url,就需要发出永久的重定向)。这真的很好,因为您可以将它们保存在db中用于开发、登台等,并且在不更改任何上载或查看代码的情况下将部分或全部上载迁移到s3。


推荐阅读
author-avatar
灰太狼老婆红太狼_715
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有