Class: Opal::Connect::ConnectCache

Inherits:
Object
  • Object
show all
Defined in:
lib/opal/connect.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = false) ⇒ ConnectCache

Create a new thread safe cache.



89
90
91
92
# File 'lib/opal/connect.rb', line 89

def initialize(options = false)
  @mutex = Mutex.new if RUBY_ENGINE != 'opal'
  @hash = options || {}
end

Instance Method Details

#[](key) ⇒ Object

Make getting value from underlying hash thread safe.



95
96
97
98
99
100
101
# File 'lib/opal/connect.rb', line 95

def [](key)
  if RUBY_ENGINE == 'opal'
    @hash[key]
  else
    @mutex.synchronize { @hash[key] }
  end
end

#[]=(key, value) ⇒ Object

Make setting value in underlying hash thread safe.



104
105
106
107
108
109
110
# File 'lib/opal/connect.rb', line 104

def []=(key, value)
  if RUBY_ENGINE == 'opal'
    @hash[key] = value
  else
    @mutex.synchronize { @hash[key] = value }
  end
end

#eachObject



124
125
126
127
128
129
130
131
132
# File 'lib/opal/connect.rb', line 124

def each
  if RUBY_ENGINE == 'opal'
    @hash.each { |key, value| yield key, value }
  else
    @mutex.synchronize do
      @hash.each { |key, value| yield key, value }
    end
  end
end

#hashObject



116
117
118
119
120
121
122
# File 'lib/opal/connect.rb', line 116

def hash
  if RUBY_ENGINE == 'opal'
    @hash
  else
    @mutex.synchronize { @hash }
  end
end

#to_jsonObject



112
113
114
# File 'lib/opal/connect.rb', line 112

def to_json
  @mutex.synchronize { @hash.to_json }
end