代码之家  ›  专栏  ›  技术社区  ›  Rubioli

RubyonRails-通过日志记录批量插入数据

  •  0
  • Rubioli  · 技术社区  · 5 年前

    我的应用程序是一个分析应用程序,每秒从几个站点获取实时数据,所有这些都添加到 insights 桌子

    目前,每个数据请求都发送到 log_insight 操作并添加到日志文件中:

    class Insight < ApplicationRecord
      def self.write_log(log_file, log_line)
        File.open(log_file, "a") { |file| file.puts (log_line.gsub("\n", " ")) }
      end
    end
    
    class InsightsController < ApplicationController
      def log_insight
        Insight.write_log('log/insights.log', data.to_json)
      end
    end
    

    每30分钟,这些日志就会添加到 洞察力 桌子 (我正在使用 Activerecord-Import 宝石 和一个新的 insights.log 正在创建,并且该过程仍在继续。

    附言:一整天大约有一百万条记录被处理并添加到 洞察力 桌子

    我觉得这不是有效的或 正确的 去的方式。

    我一直在考虑的一个建议是使用 redis 并在内存中写入日志,每30分钟取出所有内容并执行 flush .

    Rails 6.1.4
    Ruby 2.5.8
    Redis 4.4
    

    非常感谢您的任何建议!

    0 回复  |  直到 5 年前