Grape中的代码如下:

def market
    @market ||= Market.find(params[:id])
end

@market基于类层次的实例变量,属于非线程安全,如果一直使用多线程服务器,可以使用 Thread.current 代替:

def market
    Thread.current[:market] ||= Market.find(params[:id])
end

如果考虑到以后哦使用其他类型服务器,比如 Thin, 可以使用 request_store gem 包,参考: https://github.com/steveklabnik/request_store

参考:
https://github.com/ruby-grape/grape-rabl/issues/37
https://ruby-china.org/topics/30188
https://stackoverflow.com/questions/9558192/thread-safety-class-variables-in-ruby

相关文章:

  • 2022-01-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
  • 2019-03-06
  • 2022-12-23
  • 2021-07-04
猜你喜欢
  • 2021-08-23
  • 2021-12-26
  • 2021-07-29
  • 2021-10-29
  • 2021-07-13
  • 2022-12-23
  • 2021-12-15
相关资源
相似解决方案