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.



61
62
63
64
# File 'lib/opal/connect.rb', line 61

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.



67
68
69
70
71
72
73
# File 'lib/opal/connect.rb', line 67

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.



76
77
78
79
80
81
82
# File 'lib/opal/connect.rb', line 76

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

#eachObject



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

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



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

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

#to_jsonObject



84
85
86
# File 'lib/opal/connect.rb', line 84

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