【问题标题】:rendering serialized json to string将序列化的 json 渲染为字符串
【发布时间】:2020-08-26 19:49:13
【问题描述】:

有没有办法将散列或 json 呈现给变量?

案例: 带有 react/redux、postgres 的 Rails-app

  def index
    start = Time.now
    c = Customer.all.first(1000)
      respond_to do |format|
        format.json { render json: c,
                             except: [:created_at, :updated_at],
                             include: [{addresses: {except: [:created_at, :updated_at, :country, :canton],
                                                    methods: [:canton_name, :country_name]}},
                                       {latest_order: {only: [:date, :title]}}]
        }
      end
    write_log(c, start)
  end

我想生成一个嵌套哈希,例如客户、位置和最新订单,并将其缓存。

1000 位客户的性能测试: 上面的代码:5.3 秒 渲染相同的 json fast_jsonapi 4.9 秒 cache_crispies 5.2 秒

最后,上面的方法和序列化器一样快,但更简单。

但是,对于 10.000 个客户,使用 rails-render-method 生成 json (2.5MB) 的时间大约需要 50 秒。 所以,如果必须缓存它。

其他问题: 遇到这种情况,你有什么经验?

最好的问候, 克里斯

【问题讨论】:

    标签: ruby-on-rails serialization


    【解决方案1】:

    我建议你jb gem。它非常快(比fast_json 慢,但稍慢)并且jb 视图是一个普通的红宝石,所以你得到一个可读的模板,没有丑陋和缓慢的jbuilder,但结构清晰。您可以在jb 中使用缓存,因此您可以根据需要缓存整个 json 或其中的一部分。

    【讨论】:

      猜你喜欢
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多