如何在RoR中加速发布http请求

时间:2012-03-30 09:02:07

标签: ruby-on-rails performance http post

我想优化发布http请求。我的系统由服务器和客户端组成。 客户端发送图像,服务器将其保存在数据库中。

这是请求:

res = Net::HTTP.post_form(uri, :mac => 'String', :value => 'ImageData(stringa di 22k circa)')

服务器使用数据库进行此操作:

    Started POST "/events/save_photo" for 192.168.0.113 at 2012-03-30 09:27:27 +0200
      Processing by EventsController#save_photo as */*
      Parameters: {"data_type"=>"image", "value"=>"!binary |\n         /9j/2wCEAAoHBwgHBgoICAgLCgoLDhgQDg0NDh0VFhE...(very long parameter", "created_at"=>"2012-03-                 30 09:26:29 +02:00", "mac"=>"00606E91E5D2"}
    WARNING: Can't verify CSRF token authenticity
      `[[ (0.3ms)]  [1mSET PROFILING=1]
      [[Device Load (1.2ms)]  SELECT SQL_NO_CACHE `devices`.* FROM `devices` WHERE `devices`.`mac` = '00606E91E5D2' LIMIT 1
      [[CACHE (0.0ms)]  [1mSELECT `devices`.* FROM `devices` WHERE `devices`.`mac` = '00606E91E5D2' LIMIT 1]
      [[(0.4ms)]  BEGIN
      [[SQL (2.3ms)]  [INSERT INTO `multimedia` (`created_at`, `data`) VALUES ('2012-03-30 07:26:29', x'f22b19237b63f1c8c40da49ae5eb68969dd3cf28193ba...',)]
      [[ (61.9ms)]  COMMIT
      [[ (0.2ms)] [BEGIN]
      [[SQL (0.6ms)]  INSERT INTO `events` (`created_at`, `data_type`, `device_id`, `element_id`, `multimedia_id`, `name`, `status`, `updated_at`, `value`) VALUES ('2012-03-30 07:26:29', 'image', 23, 163785, 1065577, 'photo', NULL, '2012-03-30 07:27:28', '--- 0\n...')
      [[ (36.6ms)]  [COMMIT]
      [[35m (0.3ms)] BEGIN
      [[ (0.4ms)]  [SET PROFILING=1]
      [[ (1.1ms)]  SELECT SQL_NO_CACHE 1 FROM `devices` WHERE (`devices`.`mac` = BINARY '00606E91E5D2' AND `devices`.`id` != 23) LIMIT 1
      [[ (0.8ms)]  [UPDATE `devices` SET `elem_photo_id` = 163804, `updated_at` = '2012-03-30 07:27:30' WHERE `devices`.`id` = 23]
      [[ (78.9ms)]  COMMIT
    Rendered events/save_photo.html.erb (0.2ms)
    Completed 200 OK in 358.4ms (Views: 11.6ms | ActiveRecord: 246.2ms)`

在我看来,这个时间是不可接受的。我希望这个操作变得更快!!有可能吗?

注意我不使用Base64,这种方法使图像字符串更长,我必须节省内存

1 个答案:

答案 0 :(得分:0)

正如您所看到的,大部分时间花在写入数据库(246.2ms)上 - 您使用的是哪个数据库?

如果您需要快速数据库,则可以使用非关系型数据库,例如redismongoDB。当你说你必须保存'内存'时,是指磁盘空间还是RAM?因为这种非关系型数据库将数据存储在RAM中(不用担心存在持久性)。