Class: Opal::Connect::ConnectCache
- Inherits:
-
Object
- Object
- Opal::Connect::ConnectCache
- Defined in:
- lib/opal/connect.rb
Instance Method Summary collapse
-
#[](key) ⇒ Object
Make getting value from underlying hash thread safe.
-
#[]=(key, value) ⇒ Object
Make setting value in underlying hash thread safe.
- #each ⇒ Object
- #hash ⇒ Object
-
#initialize(options = false) ⇒ ConnectCache
constructor
Create a new thread safe cache.
- #to_json ⇒ Object
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( = false) @mutex = Mutex.new if RUBY_ENGINE != 'opal' @hash = || {} 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 |
#each ⇒ Object
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 |
#hash ⇒ Object
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_json ⇒ Object
112 113 114 |
# File 'lib/opal/connect.rb', line 112 def to_json @mutex.synchronize { @hash.to_json } end |