Let's say I've got a Ruby class in my Rails project that is setting an instance variable.
class Something
def self.objects
@objects ||= begin
# some logic that builds an array, which is ultimately stored in @objects
end
end
end
Is it possible that @objects
could be set multiple times? Is it possible that during one request, while executing code between the begin
/end
above, that this method could be called during a second request? This really comes down to a question of how Rails server instances are forked, I suppose.
Should I instead be using a Mutex
or thread synchronization? e.g.:
class Something
def self.objects
return @objects if @objects
Thread.exclusive do
@objects ||= begin
# some logic that builds an array, which is ultimately stored in @objects
end
end
end
end