English 中文(简体)
Global 缓存的 Global 变量对 常数对 类例实例变量
原标题:Global variable vs. constant vs. class instance variable for global cache
  • 时间:2012-05-28 03:09:23
  •  标签:
  • ruby

一般而言,哪个对全球缓存更有利:全球变量、恒定变量或类例变量?

以下是每个例子:

module Foo
  $FOO_CACHE = {}
  def self.access_to_cache
    $FOO_CACHE
  end
end

module Foo
  CACHE = {}
  def self.access_to_cache
    CACHE
  end
end

module Foo
  @cache = {}
  def self.access_to_cache
    @cache
  end
end
问题回答

但我将逐个处理每个选项:

  • < 坚固 > 全球变量 < / 坚固 > : < em> no < / em >.... 因为将一个全球变量放入模块( 或一个类, 或任何关于该事项的东西) 并不很合理, 它将无处不在。 此外, 如果您可以使用除全球变量以外的其他变量, 您应该总是这样做 。

  • < 坚固 > Constant < / 坚固 > : no . 因为缓存不固定 。 虽然Ruby不强制执行常数不能改变的常数, 但这并不意味着你应该这样做。 它们被称为常数是有原因的。

  • < 坚固 > 分类实例变量 < / 坚固 > : < em> yes < / em >. 因为它是这里唯一一个具有任何意义的地方( 虽然名称可能不合理, 但技术上来说, 它是一个模块实例变量, 但其实是相当有说服力的 ) 。 这是三者中唯一一个使语义具有修饰意义, 并被某种范围包封的 。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签