在Ruby的每个请求设置类变量,当我有一个像模型中的资源与宁静的资源进行通信。 资源路径有一些动态参数,所以我在每次请求之前在模型上设置一些类变量。如何避免竞争条件on Rails的
我有这样的事情:
class MyClass << MySuperClass::Base
class << self
attr_accessor :site
attr_accessor :shop_id
attr_accessor :product_id
def get
RestClient.get(self.site)
end
def set_site(shop_id, product_id)
self.site = "http://example.com/api/shop/#{shop_id}/product/#{product_id}
end
end
end
在我的应用程序控制器I过滤器设置shop_id之前有和PRODUCT_ID
class ApplicationController < ActionController::Base
before_filter :set_site
private
def set_site
MyClass.set_site(current_shop.id, current_product.id)
end
end
当我从这里了解:http://m.onkey.org/thread-safety-for-your-rails 这可能是一些竞赛条件的原因。
那文章写3年前所以它仍然是每个请求设置类变量可能导致竞争条件的情况下?
如果是这样那么什么是实现类似的行为,而不会导致竞争条件目前最好的做法?