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.
61 62 63 64 |
# File 'lib/opal/connect.rb', line 61 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.
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 |
#each ⇒ Object
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 |
#hash ⇒ Object
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_json ⇒ Object
84 85 86 |
# File 'lib/opal/connect.rb', line 84 def to_json @mutex.synchronize { @hash.to_json } end |